]> 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:

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