awesome-wm-widgets/word-clock-widget/word-clock.lua

137 lines
4.2 KiB
Lua
Raw Normal View History

2020-11-19 03:49:23 +01:00
-------------------------------------------------
-- Text Clock Widget for Awesome Window Manager
-- Shows current time in words, e.g. 11.54 -> eleven fifty four
-- More details could be found here:
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/text-clock-widget
-- @author Pavel Makhov
-- @copyright 2020 Pavel Makhov
-------------------------------------------------
local wibox = require("wibox")
local beautiful = require("beautiful")
local gears = require("gears")
local function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
2020-11-22 22:13:19 +01:00
local function split(string_to_split, separator)
if separator == nil then separator = "%s" end
local t = {}
for str in string.gmatch(string_to_split, "([^".. separator .."]+)") do
table.insert(t, str)
end
return t
end
2020-11-19 03:49:23 +01:00
local function convertNumberToName(num)
local lowNames = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen"};
local tensNames = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
local tens, ones, result
if num < tablelength(lowNames) then
result = lowNames[num + 1];
else
tens = math.floor(num / 10);
ones = num % 10;
if (tens <= 9) then
result = tensNames[tens - 2 + 1];
if (ones > 0) then
result = result .. " " .. lowNames[ones + 1];
end
else
result = "unknown"
end
end
return result;
end
local text_clock = {}
local function worker(args)
local args = args or {}
2020-11-22 22:13:19 +01:00
local main_color = args.main_color or beautiful.fg_normal
local accent_color = args.accent_color or beautiful.fg_urgent
local font = args.font or beautiful.font
2020-11-25 15:33:35 +01:00
local is_human_readable = args.is_human_readable
2020-11-22 22:13:19 +01:00
local military_time = args.military_time
2020-11-25 15:33:35 +01:00
local with_spaces = args.with_spaces
2020-11-22 22:13:19 +01:00
if military_time == nil then military_time = false end
2020-11-25 15:33:35 +01:00
if with_spaces == nil then with_spaces = false end
if is_human_readable == nil then is_human_readable = false end
2020-11-22 22:13:19 +01:00
2020-11-19 03:49:23 +01:00
text_clock = wibox.widget {
{
2020-11-22 22:13:19 +01:00
id = 'clock',
font = font,
2020-11-19 03:49:23 +01:00
widget = wibox.widget.textbox,
},
layout = wibox.layout.align.horizontal,
2020-11-22 22:13:19 +01:00
set_text = function(self, time)
local t = split(time)
local res = ''
for i, v in ipairs(t) do
2020-11-25 15:33:35 +01:00
res = res .. '<span color="' .. ((i % 2 == 0) and accent_color or main_color) .. '">' .. v .. '</span>' .. (with_spaces and ' ' or '')
2020-11-22 22:13:19 +01:00
end
self:get_children_by_id('clock')[1]:set_markup(res)
2020-11-19 03:49:23 +01:00
end
}
gears.timer {
timeout = 1,
call_now = true,
autostart = true,
callback = function()
2020-11-22 22:13:19 +01:00
local time = os.date((military_time and '%H' or '%I') .. ':%M')
2020-11-19 03:49:23 +01:00
local h,m = time:match('(%d+):(%d+)')
2020-11-22 22:13:19 +01:00
local min = tonumber(m)
local hour = tonumber(h)
2020-11-25 15:33:35 +01:00
if is_human_readable then
2020-11-22 22:13:19 +01:00
2020-11-25 15:33:35 +01:00
if min == 0 then
text_clock:set_text(convertNumberToName(hour) .. " o'clock")
2020-11-22 22:13:19 +01:00
else
2020-11-25 15:33:35 +01:00
local mm
if min == 15 or min == 45 then
mm = 'quater'
elseif min == 30 then
mm = 'half'
else
mm = convertNumberToName((min < 31) and min or 60 - min)
end
local to_past
if min < 31 then
to_past = 'past'
else
to_past = 'to'
hour = hour + 1
end
text_clock:set_text(mm .. ' ' .. to_past .. ' ' .. convertNumberToName(hour))
2020-11-22 22:13:19 +01:00
end
else
text_clock:set_text(convertNumberToName(hour) .. ' ' .. convertNumberToName(min))
end
2020-11-19 03:49:23 +01:00
end
}
return text_clock
end
return setmetatable(text_clock, { __call = function(_, ...)
return worker(...)
end })