]> git.madduck.net Git - etc/awesome.git/blob - widget/fs.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

#424: assert that timeout seconds in every naughty notification are numbers
[etc/awesome.git] / widget / fs.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2018, Uli Schlacter
5       * (c) 2018, Otto Modinos
6       * (c) 2013, Luca CPZ
7
8 --]]
9
10 local helpers    = require("lain.helpers")
11 local Gio        = require("lgi").Gio
12 local focused    = require("awful.screen").focused
13 local wibox      = require("wibox")
14 local naughty    = require("naughty")
15 local math       = math
16 local string     = string
17 local tconcat    = table.concat
18 local type       = type
19 local tonumber   = tonumber
20 local query_size = Gio.FILE_ATTRIBUTE_FILESYSTEM_SIZE
21 local query_free = Gio.FILE_ATTRIBUTE_FILESYSTEM_FREE
22 local query_used = Gio.FILE_ATTRIBUTE_FILESYSTEM_USED
23 local query      = query_size .. "," .. query_free .. "," .. query_used
24
25 -- File systems info
26 -- lain.widget.fs
27
28 local function factory(args)
29     local fs = {
30         widget = wibox.widget.textbox(),
31         units = {
32             [1] = "Kb", [2] = "Mb", [3] = "Gb",
33             [4] = "Tb", [5] = "Pb", [6] = "Eb",
34             [7] = "Zb", [8] = "Yb"
35         }
36     }
37
38     function fs.hide()
39         if not fs.notification then return end
40         naughty.destroy(fs.notification)
41         fs.notification = nil
42     end
43
44     function fs.show(seconds, scr)
45         fs.hide(); fs.update()
46         fs.notification_preset.screen = fs.followtag and focused() or scr or 1
47         fs.notification = naughty.notify {
48             preset  = fs.notification_preset,
49             timeout = type(seconds) == "number" and seconds or 5
50         }
51     end
52
53     local args      = args or {}
54     local timeout   = args.timeout or 600
55     local partition = args.partition
56     local threshold = args.threshold or 99
57     local showpopup = args.showpopup or "on"
58     local settings  = args.settings or function() end
59
60     fs.followtag           = args.followtag or false
61     fs.notification_preset = args.notification_preset
62
63     if not fs.notification_preset then
64         fs.notification_preset = {
65             font = "Monospace 10",
66             fg   = "#FFFFFF",
67             bg   = "#000000"
68         }
69     end
70
71     function fs.update()
72         local notifytable = { [1] = string.format("%-10s %-5s %s\t%s\t\n", "path", "used", "free", "size") }
73         local pathlen = 10
74         fs_now = {}
75
76         for _, mount in ipairs(Gio.unix_mounts_get()) do
77             local path = Gio.unix_mount_get_mount_path(mount)
78             local root = Gio.File.new_for_path(path)
79             local info = root:query_filesystem_info(query)
80
81             if info then
82                 local size = info:get_attribute_uint64(query_size)
83                 local used = info:get_attribute_uint64(query_used)
84                 local free = info:get_attribute_uint64(query_free)
85
86                 if size > 0 then
87                     local units = math.floor(math.log(size)/math.log(1024))
88
89                     fs_now[path] = {
90                         units      = fs.units[units],
91                         percentage = math.floor(100 * used / size), -- used percentage
92                         size       = size / math.pow(1024, math.floor(units)),
93                         used       = used / math.pow(1024, math.floor(units)),
94                         free       = free / math.pow(1024, math.floor(units))
95                     }
96
97                     if fs_now[path].percentage > 0 then -- don't notify unused file systems
98                         notifytable[#notifytable+1] = string.format("\n%-10s %-5s %.2f\t%.2f\t%s", path,
99                         fs_now[path].percentage .. "%", fs_now[path].free, fs_now[path].size,
100                         fs_now[path].units)
101
102                         pathlen = math.max(pathlen, #path)
103                     end
104                 end
105             end
106         end
107
108         widget = fs.widget
109         settings()
110
111         if partition and fs_now[partition] and fs_now[partition].percentage >= threshold then
112             if not helpers.get_map(partition) then
113                 naughty.notify {
114                     preset = naughty.config.presets.critical,
115                     title  = "Warning",
116                     text   = string.format("%s is above %d%% (%d%%)", partition, threshold, fs_now[partition].percentage)
117                 }
118                 helpers.set_map(partition, true)
119             else
120                 helpers.set_map(partition, false)
121             end
122         end
123
124         if pathlen > 10 then -- formatting aesthetics
125             for i = 1, #notifytable do
126                 local pathspaces = notifytable[i]:match("/%w*[/%w*]*%s*") or notifytable[i]:match("path%s*")
127                 notifytable[i] = notifytable[i]:gsub(pathspaces, pathspaces .. string.rep(" ", pathlen - 10) .. "\t")
128             end
129         end
130
131         fs.notification_preset.text = tconcat(notifytable)
132     end
133
134     if showpopup == "on" then
135        fs.widget:connect_signal('mouse::enter', function () fs.show(0) end)
136        fs.widget:connect_signal('mouse::leave', function () fs.hide() end)
137     end
138
139     helpers.newtimer(partition or "fs", timeout, fs.update)
140
141     return fs
142 end
143
144 return factory