Update .bashrc
[dotfiles/.git] / awesome / themes / powerarrow / binclock.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2017, Luca CPZ
5       * (c) 2013, romockee
6
7 --]]
8
9 local gears  = require("gears")
10 local wibox  = require("wibox")
11 local dpi    = require("beautiful.xresources").apply_dpi
12 local date   = os.date
13 local ipairs = ipairs
14 local math   = math
15 local select = select
16 local string = string
17
18 local binclock = {}
19
20 function binclock.dec2bin(num, bits)
21     local bits, t = bits or select(2, math.frexp(num)), {}
22     for b = bits, 1, -1 do
23         t[b] = math.fmod(num, 2)
24         num = (num - t[b]) / 2
25     end
26     return t
27 end
28
29 function binclock.paintdot(cr, val, shift)
30     local height = 0
31     for _, bit in ipairs(binclock.dec2bin(val, 4)) do
32         if bit >= 1 then
33             cr:set_source(gears.color(binclock.color_active))
34         else
35             cr:set_source(gears.color(binclock.color_inactive))
36         end
37         cr:rectangle(shift, height, binclock.dotsize, binclock.dotsize)
38         cr:fill()
39         height = height + binclock.dotsize + binclock.step
40     end
41 end
42
43 local function factory(args)
44     local args = args or {}
45
46     binclock.width          = args.width or dpi(42)
47     binclock.height         = args.height or dpi(18)
48     binclock.show_seconds   = args.show_seconds or false
49     binclock.color_active   = args.color_active or "#CCCCCC"
50     binclock.color_inactive = args.color_inactive or "#444444"
51     binclock.dotsize        = math.floor(binclock.height / 5)
52     binclock.step           = math.floor(binclock.dotsize / 3)
53
54     binclock.widget = wibox.widget {
55         fit = function(self, context, width, height)
56             return binclock.width, binclock.height
57         end,
58         draw = function(self, context, cr, width, height)
59             local t = date("*t")
60
61             local hour = string.format("%02d", t.hour)
62             local min  = string.format("%02d", t.min)
63             local sec  = string.format("%02d", t.sec)
64
65             local col_count = 4
66             if binclock.show_seconds then
67                 col_count = 6
68             end
69             local step = math.floor((binclock.width - col_count * binclock.dotsize) / 8)
70
71             binclock.paintdot(cr, string.sub(hour, 1, 1), step, 2)
72             binclock.paintdot(cr, string.sub(hour, 2, 2), binclock.dotsize + 2 * step)
73
74             binclock.paintdot(cr, string.sub(min, 1, 1), binclock.dotsize * 2 + 4 * step)
75             binclock.paintdot(cr, string.sub(min, 2, 2), binclock.dotsize * 3 + 5 * step)
76
77             if binclock.show_seconds then
78                 binclock.paintdot(cr, string.sub(sec, 1, 1), binclock.dotsize * 4 + 7 * step)
79                 binclock.paintdot(cr, string.sub(sec, 2, 2), binclock.dotsize * 5 + 8 * step)
80             end
81         end,
82         layout = wibox.widget.base.make_widget
83     }
84
85     binclock.timer = gears.timer {
86         autostart  = true,
87         timeout    = binclock.show_seconds and 1 or 60,
88         callback   = function()
89             binclock.widget:emit_signal("widget::redraw_needed")
90         end
91     }
92
93     return binclock
94 end
95
96 return factory