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

a2d89906b675f1f24c39e794529bb3bef586ae39
[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 string       = { match  = string.match,
20                        format = string.format }
21 local tonumber     = tonumber
22
23 local setmetatable = setmetatable
24
25 -- File system disk space usage
26 -- lain.widgets.fs
27 local fs = {}
28
29 local fs_notification  = nil
30 fs_notification_preset = { fg = beautiful.fg_normal }
31
32 function fs:hide()
33     if fs_notification ~= nil then
34         naughty.destroy(fs_notification)
35         fs_notification = nil
36     end
37 end
38
39 function fs:show(t_out)
40     fs:hide()
41
42     naughty.notify({text=fs_notification_preset.screen})
43
44     local f = io.popen(helpers.scripts_dir .. "dfs")
45     ws = f:read("*all"):gsub("\n*$", "")
46     f:close()
47
48     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 settings  = args.settings or function() end
63
64     fs.widget = wibox.widget.textbox('')
65
66     helpers.set_map(partition, false)
67
68     function update()
69         fs_info = {}
70         fs_now  = {}
71         local f = assert(io.popen("LC_ALL=C df -kP"))
72
73         for line in f:lines() do -- Match: (size) (used)(avail)(use%) (mount)
74             local s     = string.match(line, "^.-[%s]([%d]+)")
75             local u,a,p = string.match(line, "([%d]+)[%D]+([%d]+)[%D]+([%d]+)%%")
76             local m     = string.match(line, "%%[%s]([%p%w]+)")
77
78             if u and m then -- Handle 1st line and broken regexp
79                 fs_info[m .. " size_mb"]  = string.format("%.1f", tonumber(s) / unit["mb"])
80                 fs_info[m .. " size_gb"]  = string.format("%.1f", tonumber(s) / unit["gb"])
81                 fs_info[m .. " used_p"]   = tonumber(p)
82                 fs_info[m .. " avail_p"]  = 100 - tonumber(p)
83             end
84         end
85
86         f:close()
87
88         fs_now.used      = tonumber(fs_info[partition .. " used_p"])  or 0
89         fs_now.available = tonumber(fs_info[partition .. " avail_p"]) or 0
90         fs_now.size_mb   = tonumber(fs_info[partition .. " size_mb"]) or 0
91         fs_now.size_gb   = tonumber(fs_info[partition .. " size_gb"]) or 0
92
93         widget = fs.widget
94         settings()
95
96         if fs_now.used >= 99 and not helpers.get_map(partition)
97         then
98             naughty.notify({
99                 title = "warning",
100                 text = partition .. " ran out!\nmake some room",
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
111     fs.widget:connect_signal('mouse::enter', function () fs:show(0) end)
112     fs.widget:connect_signal('mouse::leave', function () fs:hide() end)
113
114     helpers.newtimer(partition, timeout, update)
115
116     return setmetatable(fs, { __index = fs.widget })
117 end
118
119 return setmetatable(fs, { __call = function(_, ...) return worker(...) end })