]> git.madduck.net Git - etc/awesome.git/blob - widgets/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:

312a5e3b68c3610a6f3a34343d25f81adc26acc2
[etc/awesome.git] / widgets / fs.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013, Luke Bonham                     
6                                                   
7 --]]
8
9 local helpers      = require("lain.helpers")
10
11 local shell        = require("awful.util").shell
12 local focused      = require("awful.screen").focused
13 local wibox        = require("wibox")
14 local naughty      = require("naughty")
15
16 local string       = string
17 local tonumber     = tonumber
18
19 local setmetatable = setmetatable
20
21 -- File system disk space usage
22 -- lain.widgets.fs
23 local fs = helpers.make_widget_textbox()
24
25 -- Unit definitions
26 fs.unit = { ["mb"] = 1024, ["gb"] = 1024^2 }
27
28 function fs.hide()
29     if not fs.notification then return end
30     naughty.destroy(fs.notification)
31     fs.notification = nil
32 end
33
34 function fs.show(seconds, scr)
35     fs.update()
36
37     fs.hide()
38
39     if fs.followtag then
40         fs.notification_preset.screen = focused()
41     elseif scr then
42         fs.notification_preset.screen = scr or 1
43     end
44
45     fs.notification = naughty.notify({
46         preset      = fs.notification_preset,
47         timeout     = seconds or 5
48     })
49 end
50
51 local function worker(args)
52     local args             = args or {}
53     local timeout          = args.timeout or 600
54     local partition        = args.partition or "/"
55     local showpopup        = args.showpopup or "on"
56     local notify           = args.notify or "on"
57     local settings         = args.settings or function() end
58
59     fs.options             = args.options
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     helpers.set_map(partition, false)
72
73     function fs.update()
74         fs_info, fs_now  = {}, {}
75         helpers.async({ shell, "-c", "LC_ALL=C df -k --output=target,size,used,avail,pcent" }, function(f)
76             for line in string.gmatch(f, "\n[^\n]+") do
77                 local m,s,u,a,p = string.match(line, "(/.-%s).-(%d+).-(%d+).-(%d+).-([%d]+)%%")
78                 m = m:gsub(" ", "") -- clean target from any whitespace
79
80                 fs_info[m .. " size_mb"]  = string.format("%.1f", tonumber(s) / fs.unit["mb"])
81                 fs_info[m .. " size_gb"]  = string.format("%.1f", tonumber(s) / fs.unit["gb"])
82                 fs_info[m .. " used_mb"]  = string.format("%.1f", tonumber(u) / fs.unit["mb"])
83                 fs_info[m .. " used_gb"]  = string.format("%.1f", tonumber(u) / fs.unit["gb"])
84                 fs_info[m .. " used_p"]   = p
85                 fs_info[m .. " avail_mb"] = string.format("%.1f", tonumber(a) / fs.unit["mb"])
86                 fs_info[m .. " avail_gb"] = string.format("%.1f", tonumber(a) / fs.unit["gb"])
87                 fs_info[m .. " avail_p"]  = string.format("%d", 100 - tonumber(p))
88             end
89
90             fs_now.size_mb      = fs_info[partition .. " size_mb"]  or "N/A"
91             fs_now.size_gb      = fs_info[partition .. " size_gb"]  or "N/A"
92             fs_now.used         = fs_info[partition .. " used_p"]   or "N/A"
93             fs_now.used_mb      = fs_info[partition .. " used_mb"]  or "N/A"
94             fs_now.used_gb      = fs_info[partition .. " used_gb"]  or "N/A"
95             fs_now.available    = fs_info[partition .. " avail_p"]  or "N/A"
96             fs_now.available_mb = fs_info[partition .. " avail_mb"] or "N/A"
97             fs_now.available_gb = fs_info[partition .. " avail_gb"] or "N/A"
98
99             notification_preset = fs.notification_preset
100             widget = fs.widget
101             settings()
102
103             if notify == "on" and tonumber(fs_now.used) >= 99 and not helpers.get_map(partition) then
104                 naughty.notify({
105                     preset = naughty.config.presets.critical,
106                     title  = "Warning",
107                     text   = partition .. " is empty",
108                 })
109                 helpers.set_map(partition, true)
110             else
111                 helpers.set_map(partition, false)
112             end
113         end)
114
115         local notifycmd = (fs.options and string.format("dfs %s", fs.options)) or "dfs"
116         helpers.async(helpers.scripts_dir .. notifycmd, function(ws)
117             fs.notification_preset.text = ws:gsub("\n*$", "")
118         end)
119     end
120
121     if showpopup == "on" then
122        fs.widget:connect_signal('mouse::enter', function () fs.show(0) end)
123        fs.widget:connect_signal('mouse::leave', function () fs.hide() end)
124     end
125
126     helpers.newtimer(partition, timeout, fs.update)
127
128     return fs
129 end
130
131 return setmetatable(fs, { __call = function(_, ...) return worker(...) end })