My configuration files for Debian/Ubuntu applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

97 lines
3.0 KiB

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