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.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. ## Usage
  2. [Read here.](https://github.com/lcpz/lain/wiki/Widgets#usage)
  3. ### Description
  4. Template for asynchronous watcher widgets.
  5. Executes an input command and makes the user feed a `wibox.widget` with the output.
  6. ```lua
  7. local mywatch = lain.widget.watch()
  8. ```
  9. This has been implemented in Awesome 4.0 as [`awful.widget.watch`](https://awesomewm.org/doc/api/classes/awful.widget.watch.html). But while Awesome `watch` returns only the widget, Lain one returns a table including its timer and internal update function too.
  10. ## Input table
  11. Variable | Meaning | Type | Default
  12. --- | --- | --- | ---
  13. `widget` | Widget to feed | `wibox.widget` | `wibox.widget.textbox`
  14. `timeout` | Refresh timeout seconds | number | 5
  15. `cmd` | The command to execute | string **or** table | `nil`
  16. `nostart` | Widget timer doesn't start immediately | boolean | false
  17. `stoppable` | Widget timer is stoppable | boolean | false
  18. `settings` | User settings | function | see [Default `settings` function](https://github.com/lcpz/lain/wiki/watch#default-settings-function)
  19. If your command needs a shell, you need to set `cmd` as an array of 3 strings, where the first contains the shell, the second contains `-c`, and the third contains the actual command. Example:
  20. ```lua
  21. cmd = { awful.util.shell, "-c", "myactualcommand" }
  22. ```
  23. `settings` can use the string `output`, which is the output of `cmd`.
  24. ### Default `settings` function
  25. ```lua
  26. settings = function() widget:set_text(output) end
  27. ```
  28. ## Output table
  29. Variable | Meaning | Type
  30. --- | --- | ---
  31. `widget` | The widget | input widget type or `wibox.widget.textbox`
  32. `update` | Update `widget` | function
  33. `timer` | The widget timer | [`gears.timer`](https://awesomewm.org/doc/api/classes/gears.timer.html) or `nil`
  34. The `update` function can be used to refresh the widget before `timeout` expires.
  35. If `stoppable == true`, the widget will have an ad-hoc timer, which you can control though `timer` variable.
  36. ## Use case examples
  37. ### bitcoin
  38. ```lua
  39. -- Bitcoin to USD current price, using Coinbase V1 API
  40. local bitcoin = lain.widget.watch({
  41. timeout = 43200, -- half day
  42. stoppable = true,
  43. cmd = "curl -m5 -s 'https://coinbase.com/api/v1/prices/buy'",
  44. settings = function()
  45. local btc, pos, err = require("lain.util").dkjson.decode(output, 1, nil)
  46. local btc_price = (not err and btc and btc["subtotal"]["amount"]) or "N/A"
  47. -- customize here
  48. widget:set_text(btc_price)
  49. end
  50. })
  51. ```
  52. ### btrfs
  53. ```lua
  54. -- btrfs root df
  55. local myrootfs = lain.widget.watch({
  56. timeout = 600,
  57. cmd = "btrfs filesystem df -g /",
  58. settings = function()
  59. local total, used = string.match(output, "Data.-total=(%d+%.%d+)GiB.-used=(%d+%.%d+)GiB")
  60. local percent_used = math.ceil((tonumber(used) / tonumber(total)) * 100)
  61. -- customize here
  62. widget:set_text(" [/: " .. percent_used .. "%] ")
  63. end
  64. })
  65. ```
  66. ### cmus
  67. ```lua
  68. -- cmus audio player
  69. local cmus = lain.widget.watch({
  70. timeout = 2,
  71. stoppable = true,
  72. cmd = "cmus-remote -Q",
  73. settings = function()
  74. local cmus_now = {
  75. state = "N/A",
  76. artist = "N/A",
  77. title = "N/A",
  78. album = "N/A"
  79. }
  80. for w in string.gmatch(output, "(.-)tag") do
  81. a, b = w:match("(%w+) (.-)\n")
  82. cmus_now[a] = b
  83. end
  84. -- customize here
  85. widget:set_text(cmus_now.artist .. " - " .. cmus_now.title)
  86. end
  87. })
  88. ```
  89. ### maildir
  90. ```lua
  91. -- checks whether there are files in the "new" directories of a mail dirtree
  92. local mailpath = "~/Mail"
  93. local mymaildir = lain.widget.watch({
  94. timeout = 60,
  95. stoppable = true,
  96. cmd = { awful.util.shell, "-c", string.format("ls -1dr %s/*/new/*", mailpath) },
  97. settings = function()
  98. local inbox_now = { digest = "" }
  99. for dir in output:gmatch(".-/(%w+)/new") do
  100. inbox_now[dir] = 1
  101. for _ in output:gmatch(dir) do
  102. inbox_now[dir] = inbox_now[dir] + 1
  103. end
  104. if #inbox_now.digest > 0 then inbox_now.digest = inbox_now.digest .. ", " end
  105. inbox_now.digest = inbox_now.digest .. string.format("%s (%d)", dir, inbox_now[dir])
  106. end
  107. -- customize here
  108. widget:set_text("mail: " .. inbox_now.digest)
  109. end
  110. })
  111. ```
  112. ### mpris
  113. ```lua
  114. -- infos from mpris clients such as spotify and VLC
  115. -- based on https://github.com/acrisci/playerctl
  116. local mpris = lain.widget.watch({
  117. cmd = "playerctl status && playerctl metadata",
  118. timeout = 2,
  119. stoppable = true,
  120. settings = function()
  121. local escape_f = require("awful.util").escape
  122. local mpris_now = {
  123. state = "N/A",
  124. artist = "N/A",
  125. title = "N/A",
  126. art_url = "N/A",
  127. album = "N/A",
  128. album_artist = "N/A"
  129. }
  130. mpris_now.state = string.match(output, "Playing") or
  131. string.match(output, "Paused") or "N/A"
  132. for k, v in string.gmatch(output, "'[^:]+:([^']+)':[%s]<%[?'([^']+)'%]?>")
  133. do
  134. if k == "artUrl" then mpris_now.art_url = v
  135. elseif k == "artist" then mpris_now.artist = escape_f(v)
  136. elseif k == "title" then mpris_now.title = escape_f(v)
  137. elseif k == "album" then mpris_now.album = escape_f(v)
  138. elseif k == "albumArtist" then mpris_now.album_artist = escape_f(v)
  139. end
  140. end
  141. -- customize here
  142. widget:set_text(mpris_now.artist .. " - " .. mpris_now.title)
  143. end
  144. })
  145. ```
  146. ### upower
  147. ```lua
  148. -- battery infos from freedesktop upower
  149. local mybattery = lain.widget.watch({
  150. timeout = 30,
  151. cmd = { awful.util.shell, "-c", "upower -i /org/freedesktop/UPower/devices/battery_BAT | sed -n '/present/,/icon-name/p'" },
  152. settings = function()
  153. local bat_now = {
  154. present = "N/A",
  155. state = "N/A",
  156. warninglevel = "N/A",
  157. energy = "N/A",
  158. energyfull = "N/A",
  159. energyrate = "N/A",
  160. voltage = "N/A",
  161. percentage = "N/A",
  162. capacity = "N/A",
  163. icon = "N/A"
  164. }
  165. for k, v in string.gmatch(output, '([%a]+[%a|-]+):%s*([%a|%d]+[,|%a|%d]-)') do
  166. if k == "present" then bat_now.present = v
  167. elseif k == "state" then bat_now.state = v
  168. elseif k == "warning-level" then bat_now.warninglevel = v
  169. elseif k == "energy" then bat_now.energy = string.gsub(v, ",", ".") -- Wh
  170. elseif k == "energy-full" then bat_now.energyfull = string.gsub(v, ",", ".") -- Wh
  171. elseif k == "energy-rate" then bat_now.energyrate = string.gsub(v, ",", ".") -- W
  172. elseif k == "voltage" then bat_now.voltage = string.gsub(v, ",", ".") -- V
  173. elseif k == "percentage" then bat_now.percentage = tonumber(v) -- %
  174. elseif k == "capacity" then bat_now.capacity = string.gsub(v, ",", ".") -- %
  175. elseif k == "icon-name" then bat_now.icon = v
  176. end
  177. end
  178. -- customize here
  179. widget:set_text("Bat: " .. bat_now.percentage .. " " .. bat_now.state)
  180. end
  181. })
  182. ```