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

391c26009ed3e9960d16bb9ca5e25895330bd486
[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 = {}
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.hide()
36
37     if fs.followtag then
38         fs.notification_preset.screen = focused()
39     elseif scr then
40         fs.notification_preset.screen = scr
41     end
42
43     local cmd = (fs.options and string.format("dfs %s", fs.options)) or "dfs"
44
45     helpers.async(helpers.scripts_dir .. cmd, function(ws)
46         fs.notification = naughty.notify({
47             preset      = fs.notification_preset,
48             text        = ws:gsub("\n*$", ""),
49             timeout     = seconds or 5,
50         })
51     end)
52 end
53
54 local function worker(args)
55     local args             = args or {}
56     local timeout          = args.timeout or 600
57     local partition        = args.partition or "/"
58     local showpopup        = args.showpopup or "on"
59     local notify           = args.notify or "on"
60     local settings         = args.settings or function() end
61
62     fs.options             = args.options
63     fs.followtag           = args.followtag or false
64     fs.notification_preset = args.notification_preset
65
66     if not fs.notification_preset then
67         fs.notification_preset      = naughty.config.defaults
68         fs.notification_preset.font = "Monospace 10"
69         fs.notification_preset.fg   = "#FFFFFF"
70         fs.notification_preset.bg   = "#000000"
71     end
72
73     fs.widget = wibox.widget.textbox()
74
75     helpers.set_map(partition, false)
76
77
78     function update()
79         fs_info, fs_now  = {}, {}
80         helpers.async(string.format("%s -c 'LC_ALL=C df -k --output=target,size,used,avail,pcent'", shell), function(f)
81             for line in string.gmatch(f, "\n[^\n]+") do
82                 local m,s,u,a,p = string.match(line, "(/.-%s).-(%d+).-(%d+).-(%d+).-([%d]+)%%")
83                 m = m:gsub(" ", "") -- clean target from any whitespace
84
85                 fs_info[m .. " size_mb"]  = string.format("%.1f", tonumber(s) / fs.unit["mb"])
86                 fs_info[m .. " size_gb"]  = string.format("%.1f", tonumber(s) / fs.unit["gb"])
87                 fs_info[m .. " used_mb"]  = string.format("%.1f", tonumber(u) / fs.unit["mb"])
88                 fs_info[m .. " used_gb"]  = string.format("%.1f", tonumber(u) / fs.unit["gb"])
89                 fs_info[m .. " used_p"]   = p
90                 fs_info[m .. " avail_mb"] = string.format("%.1f", tonumber(a) / fs.unit["mb"])
91                 fs_info[m .. " avail_gb"] = string.format("%.1f", tonumber(a) / fs.unit["gb"])
92                 fs_info[m .. " avail_p"]  = string.format("%d", 100 - tonumber(p))
93             end
94
95             fs_now.size_mb      = fs_info[partition .. " size_mb"]  or "N/A"
96             fs_now.size_gb      = fs_info[partition .. " size_gb"]  or "N/A"
97             fs_now.used         = fs_info[partition .. " used_p"]   or "N/A"
98             fs_now.used_mb      = fs_info[partition .. " used_mb"]  or "N/A"
99             fs_now.used_gb      = fs_info[partition .. " used_gb"]  or "N/A"
100             fs_now.available    = fs_info[partition .. " avail_p"]  or "N/A"
101             fs_now.available_mb = fs_info[partition .. " avail_mb"] or "N/A"
102             fs_now.available_gb = fs_info[partition .. " avail_gb"] or "N/A"
103
104             notification_preset = fs.notification_preset
105             widget = fs.widget
106             settings()
107
108             if notify == "on" and tonumber(fs_now.used) >= 99 and not helpers.get_map(partition) then
109                 naughty.notify({
110                     title   = "Warning",
111                     text    = partition .. " is empty!",
112                     timeout = 8,
113                     fg      = "#000000",
114                     bg      = "#FFFFFF"
115                 })
116                 helpers.set_map(partition, true)
117             else
118                 helpers.set_map(partition, false)
119             end
120         end)
121     end
122
123     if showpopup == "on" then
124        fs.widget:connect_signal('mouse::enter', function () fs.show(0) end)
125        fs.widget:connect_signal('mouse::leave', function () fs.hide() end)
126     end
127
128     helpers.newtimer(partition, timeout, update)
129
130     return setmetatable(fs, { __index = fs.widget })
131 end
132
133 return setmetatable(fs, { __call = function(_, ...) return worker(...) end })