3 [Read here.](https://github.com/lcpz/lain/wiki/Widgets#usage)
7 Template for asynchronous watcher widgets.
9 Executes an input command and makes the user feed a `wibox.widget` with the output.
12 local mywatch = lain.widget.watch()
15 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.
19 Variable | Meaning | Type | Default
21 `widget` | Widget to feed | `wibox.widget` | `wibox.widget.textbox`
22 `timeout` | Refresh timeout seconds | number | 5
23 `cmd` | The command to execute | string **or** table | `nil`
24 `nostart` | Widget timer doesn't start immediately | boolean | false
25 `stoppable` | Widget timer is stoppable | boolean | false
26 `settings` | User settings | function | see [Default `settings` function](https://github.com/lcpz/lain/wiki/watch#default-settings-function)
28 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:
31 cmd = { awful.util.shell, "-c", "myactualcommand" }
34 `settings` can use the string `output`, which is the output of `cmd`.
36 ### Default `settings` function
39 settings = function() widget:set_text(output) end
43 Variable | Meaning | Type
45 `widget` | The widget | input widget type or `wibox.widget.textbox`
46 `update` | Update `widget` | function
47 `timer` | The widget timer | [`gears.timer`](https://awesomewm.org/doc/api/classes/gears.timer.html) or `nil`
49 The `update` function can be used to refresh the widget before `timeout` expires.
51 If `stoppable == true`, the widget will have an ad-hoc timer, which you can control though `timer` variable.
58 -- Bitcoin to USD current price, using Coinbase V1 API
59 local bitcoin = lain.widget.watch({
60 timeout = 43200, -- half day
62 cmd = "curl -m5 -s 'https://coinbase.com/api/v1/prices/buy'",
64 local btc, pos, err = require("lain.util").dkjson.decode(output, 1, nil)
65 local btc_price = (not err and btc and btc["subtotal"]["amount"]) or "N/A"
68 widget:set_text(btc_price)
77 local myrootfs = lain.widget.watch({
79 cmd = "btrfs filesystem df -g /",
81 local total, used = string.match(output, "Data.-total=(%d+%.%d+)GiB.-used=(%d+%.%d+)GiB")
82 local percent_used = math.ceil((tonumber(used) / tonumber(total)) * 100)
85 widget:set_text(" [/: " .. percent_used .. "%] ")
94 local cmus = lain.widget.watch({
97 cmd = "cmus-remote -Q",
106 for w in string.gmatch(output, "(.-)tag") do
107 a, b = w:match("(%w+) (.-)\n")
112 widget:set_text(cmus_now.artist .. " - " .. cmus_now.title)
120 -- checks whether there are files in the "new" directories of a mail dirtree
121 local mailpath = "~/Mail"
122 local mymaildir = lain.widget.watch({
125 cmd = { awful.util.shell, "-c", string.format("ls -1dr %s/*/new/*", mailpath) },
126 settings = function()
127 local inbox_now = { digest = "" }
129 for dir in output:gmatch(".-/(%w+)/new") do
131 for _ in output:gmatch(dir) do
132 inbox_now[dir] = inbox_now[dir] + 1
134 if #inbox_now.digest > 0 then inbox_now.digest = inbox_now.digest .. ", " end
135 inbox_now.digest = inbox_now.digest .. string.format("%s (%d)", dir, inbox_now[dir])
139 widget:set_text("mail: " .. inbox_now.digest)
147 -- infos from mpris clients such as spotify and VLC
148 -- based on https://github.com/acrisci/playerctl
149 local mpris = lain.widget.watch({
150 cmd = "playerctl status && playerctl metadata",
153 settings = function()
154 local escape_f = require("awful.util").escape
164 mpris_now.state = string.match(output, "Playing") or
165 string.match(output, "Paused") or "N/A"
167 for k, v in string.gmatch(output, "'[^:]+:([^']+)':[%s]<%[?'([^']+)'%]?>")
169 if k == "artUrl" then mpris_now.art_url = v
170 elseif k == "artist" then mpris_now.artist = escape_f(v)
171 elseif k == "title" then mpris_now.title = escape_f(v)
172 elseif k == "album" then mpris_now.album = escape_f(v)
173 elseif k == "albumArtist" then mpris_now.album_artist = escape_f(v)
178 widget:set_text(mpris_now.artist .. " - " .. mpris_now.title)
186 -- battery infos from freedesktop upower
187 local mybattery = lain.widget.watch({
189 cmd = { awful.util.shell, "-c", "upower -i /org/freedesktop/UPower/devices/battery_BAT | sed -n '/present/,/icon-name/p'" },
190 settings = function()
194 warninglevel = "N/A",
204 for k, v in string.gmatch(output, '([%a]+[%a|-]+):%s*([%a|%d]+[,|%a|%d]-)') do
205 if k == "present" then bat_now.present = v
206 elseif k == "state" then bat_now.state = v
207 elseif k == "warning-level" then bat_now.warninglevel = v
208 elseif k == "energy" then bat_now.energy = string.gsub(v, ",", ".") -- Wh
209 elseif k == "energy-full" then bat_now.energyfull = string.gsub(v, ",", ".") -- Wh
210 elseif k == "energy-rate" then bat_now.energyrate = string.gsub(v, ",", ".") -- W
211 elseif k == "voltage" then bat_now.voltage = string.gsub(v, ",", ".") -- V
212 elseif k == "percentage" then bat_now.percentage = tonumber(v) -- %
213 elseif k == "capacity" then bat_now.capacity = string.gsub(v, ",", ".") -- %
214 elseif k == "icon-name" then bat_now.icon = v
219 widget:set_text("Bat: " .. bat_now.percentage .. " " .. bat_now.state)