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

ad5b204e48d0e2c8ffb00dae2e627443461d9f0a
[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, fs_args)
39     fs.hide()
40
41     local cmd = (fs_args and string.format("dfs %s", fs_args)) or "dfs"
42     local ws = helpers.read_pipe(helpers.scripts_dir .. cmd):gsub("\n*$", "")
43
44     if fs.followmouse then
45         fs.notification_preset.screen = mouse.screen
46     end
47
48     fs_notification = naughty.notify({
49         preset  = fs.notification_preset,
50         text    = ws,
51         timeout = t_out
52     })
53 end
54
55 -- Unit definitions
56 local unit = { ["mb"] = 1024, ["gb"] = 1024^2 }
57
58 local function worker(args)
59     local args             = args or {}
60     local timeout          = args.timeout or 600
61     local partition        = args.partition or "/"
62     local showpopup        = args.showpopup or "on"
63     local notify           = args.notify or "on"
64     local settings         = args.settings or function() end
65
66     fs.followmouse         = args.followmouse or false
67     fs.notification_preset = args.notification_preset or { fg = beautiful.fg_normal }
68
69     fs.widget = wibox.widget.textbox('')
70
71     helpers.set_map(partition, false)
72
73     function update()
74         fs_info = {}
75         fs_now  = {}
76         local f = assert(io.popen("LC_ALL=C df -kP"))
77
78         for line in f:lines() do -- Match: (size) (used)(avail)(use%) (mount)
79             local s     = string.match(line, "^.-[%s]([%d]+)")
80             local u,a,p = string.match(line, "([%d]+)[%D]+([%d]+)[%D]+([%d]+)%%")
81             local m     = string.match(line, "%%[%s]([%p%w]+)")
82
83             if u and m then -- Handle 1st line and broken regexp
84                 fs_info[m .. " size_mb"]  = string.format("%.1f", tonumber(s) / unit["mb"])
85                 fs_info[m .. " size_gb"]  = string.format("%.1f", tonumber(s) / unit["gb"])
86                 fs_info[m .. " used_p"]   = tonumber(p)
87                 fs_info[m .. " avail_p"]  = 100 - tonumber(p)
88             end
89         end
90
91         f:close()
92
93         fs_now.used      = tonumber(fs_info[partition .. " used_p"])  or 0
94         fs_now.available = tonumber(fs_info[partition .. " avail_p"]) or 0
95         fs_now.size_mb   = tonumber(fs_info[partition .. " size_mb"]) or 0
96         fs_now.size_gb   = tonumber(fs_info[partition .. " size_gb"]) or 0
97
98         notification_preset = fs.notification_preset
99         widget = fs.widget
100         settings()
101
102         if notify == "on" and fs_now.used >= 99 and not helpers.get_map(partition)
103         then
104             naughty.notify({
105                 title = "warning",
106                 text = partition .. " ran out!\nmake some room",
107                 timeout = 8,
108                 fg = "#000000",
109                 bg = "#FFFFFF",
110             })
111             helpers.set_map(partition, true)
112         else
113             helpers.set_map(partition, false)
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, update)
123
124     return setmetatable(fs, { __index = fs.widget })
125 end
126
127 return setmetatable(fs, { __call = function(_, ...) return worker(...) end })