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

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