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

quake: set default geometry only on first spawn; typo fixed
[etc/awesome.git] / widgets / fs.lua
1
2 --[[
3                                                       
4      Licensed under GNU General Public License v2     
5       * (c) 2013, Luke Bonham                         
6       * (c) 2010, Adrian C.      <anrxc@sysphere.org> 
7       * (c) 2009, Lucas de Vries <lucas@glacicle.com> 
8                                                       
9 --]]
10
11 local helpers      = require("lain.helpers")
12
13 local beautiful    = require("beautiful")
14 local wibox        = require("wibox")
15 local naughty      = require("naughty")
16
17 local io           = { popen  = io.popen }
18 local pairs        = pairs
19 local mouse        = mouse
20 local string       = { match  = string.match,
21                        format = string.format }
22 local tonumber     = tonumber
23
24 local setmetatable = setmetatable
25
26 -- File system disk space usage
27 -- lain.widgets.fs
28 local fs = {}
29 local fs_notification  = nil
30
31 function fs:hide()
32     if fs_notification ~= nil then
33         naughty.destroy(fs_notification)
34         fs_notification = nil
35     end
36 end
37
38 function fs:show(t_out)
39     fs:hide()
40
41     local ws = helpers.read_pipe(helpers.scripts_dir .. "dfs"):gsub("\n*$", "")
42
43     if fs.followmouse then
44         fs.notification_preset.screen = mouse.screen
45     end
46
47     fs_notification = naughty.notify({
48         preset  = fs.notification_preset,
49         text    = ws,
50         timeout = t_out
51     })
52 end
53
54 -- Unit definitions
55 local unit = { ["mb"] = 1024, ["gb"] = 1024^2 }
56
57 local function worker(args)
58     local args             = args or {}
59     local timeout          = args.timeout or 600
60     local partition        = args.partition or "/"
61     local showpopup        = args.showpopup or "on"
62     local notify           = args.notify or "on"
63     local settings         = args.settings or function() end
64
65     fs.followmouse         = args.followmouse or false
66     fs.notification_preset = args.notification_preset or { fg = beautiful.fg_normal }
67
68     fs.widget = wibox.widget.textbox('')
69
70     helpers.set_map(partition, false)
71
72     function update()
73         fs_info = {}
74         fs_now  = {}
75         local f = assert(io.popen("LC_ALL=C df -kP"))
76
77         for line in f:lines() do -- Match: (size) (used)(avail)(use%) (mount)
78             local s     = string.match(line, "^.-[%s]([%d]+)")
79             local u,a,p = string.match(line, "([%d]+)[%D]+([%d]+)[%D]+([%d]+)%%")
80             local m     = string.match(line, "%%[%s]([%p%w]+)")
81
82             if u and m then -- Handle 1st line and broken regexp
83                 fs_info[m .. " size_mb"]  = string.format("%.1f", tonumber(s) / unit["mb"])
84                 fs_info[m .. " size_gb"]  = string.format("%.1f", tonumber(s) / unit["gb"])
85                 fs_info[m .. " used_p"]   = tonumber(p)
86                 fs_info[m .. " avail_p"]  = 100 - tonumber(p)
87             end
88         end
89
90         f:close()
91
92         fs_now.used      = tonumber(fs_info[partition .. " used_p"])  or 0
93         fs_now.available = tonumber(fs_info[partition .. " avail_p"]) or 0
94         fs_now.size_mb   = tonumber(fs_info[partition .. " size_mb"]) or 0
95         fs_now.size_gb   = tonumber(fs_info[partition .. " size_gb"]) or 0
96
97         notification_preset = fs.notification_preset
98         widget = fs.widget
99         settings()
100
101         if notify == "on" and fs_now.used >= 99 and not helpers.get_map(partition)
102         then
103             naughty.notify({
104                 title = "warning",
105                 text = partition .. " ran out!\nmake some room",
106                 timeout = 8,
107                 fg = "#000000",
108                 bg = "#FFFFFF",
109             })
110             helpers.set_map(partition, true)
111         else
112             helpers.set_map(partition, false)
113         end
114     end
115
116     if showpopup == "on" then
117         fs.widget:connect_signal('mouse::enter', function () fs:show(0) end)
118         fs.widget:connect_signal('mouse::leave', function () fs:hide() end)
119     end
120
121     helpers.newtimer(partition, timeout, update)
122
123     return setmetatable(fs, { __index = fs.widget })
124 end
125
126 return setmetatable(fs, { __call = function(_, ...) return worker(...) end })