helpers.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. --[[
  2. Awesome WM Widget Helper
  3. Distopico Vegan <distopico [at] riseup [dot] net>
  4. Licensed under GPL3
  5. Original from: https://github.com/mrzapp/awesomerc
  6. --]]
  7. local wibox = require("wibox")
  8. local helpers = {}
  9. function helpers:set_draw_method(imagebox, scale)
  10. local wmargin = wibox.layout.margin()
  11. wmargin:set_margins(4)
  12. wmargin:set_widget(imagebox)
  13. return wmargin
  14. -- function imagebox:draw(wibox, cr, width, height)
  15. -- if not self._image then return end
  16. -- cr:save()
  17. -- local w = self._image:get_width()
  18. -- local h = self._image:get_height()
  19. -- local aspect = width / w
  20. -- local aspect_h = height / h
  21. -- if aspect > aspect_h then aspect = aspect_h end
  22. -- aspect = aspect * 0.5
  23. -- local offset_x = w * 0.5
  24. -- local offset_y = h * 0.5
  25. -- cr:scale(aspect, aspect)
  26. -- cr:set_source_surface(self._image, offset_x, offset_y)
  27. -- cr:paint()
  28. -- cr:restore()
  29. -- end
  30. end
  31. function helpers:run(command)
  32. local prog = io.popen(command)
  33. local result = prog:read('*all')
  34. prog:close()
  35. return result
  36. end
  37. function helpers:delay(func, time)
  38. local timer = timer({timeout = time or 0})
  39. timer:connect_signal("timeout", function()
  40. func()
  41. timer:stop()
  42. end)
  43. timer:start()
  44. end
  45. function helpers:listen(widget, interval)
  46. widget:update()
  47. if widget._timer ~= nil then
  48. widget._timer:stop()
  49. end
  50. -- Timer
  51. local timer = timer({timeout = interval or 30})
  52. widget._timer = timer
  53. timer:connect_signal("timeout", function()
  54. widget:update()
  55. end)
  56. timer:start()
  57. end
  58. function helpers:test(cmd)
  59. local test = io.popen(cmd)
  60. local result = test:read() ~= nil
  61. test:close()
  62. return result
  63. end
  64. function helpers:exists(path)
  65. if path == nil then
  66. return false
  67. end
  68. local content = io.open(path, "rb")
  69. if content then
  70. content:close()
  71. end
  72. return content ~= nil
  73. end
  74. -- {{{ Expose path as a Lua table
  75. function helpers:pathtotable(dir)
  76. return setmetatable({ _path = dir },
  77. { __index = function(table, index)
  78. local path = table._path .. '/' .. index
  79. local f = io.open(path)
  80. if f then
  81. local s = f:read("*all")
  82. f:close()
  83. if s then
  84. return s
  85. else
  86. local o = { _path = path }
  87. setmetatable(o, getmetatable(table))
  88. return o
  89. end
  90. end
  91. end
  92. })
  93. end
  94. -- }}}
  95. return helpers