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

totally reworked widgets
[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           = io
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 = { notification_preset = {} }
28 local notification = nil
29
30 function fs:hide()
31     if notification ~= nil then
32         naughty.destroy(notification)
33         notification = nil
34     end
35 end
36
37 function fs:show(t_out)
38     fs:hide()
39
40     local f = io.popen(helpers.scripts_dir .. "dfs")
41     ws = f:read("*all"):gsub("\n*$", "")
42     f:close()
43
44     notification = naughty.notify({
45         preset = fs.notification_preset,
46         text = ws,
47         timeout = t_out
48     })
49 end
50
51 -- Units definitions
52 local unit = { ["mb"] = 1024, ["gb"] = 1024^2 }
53
54 local function worker(args)
55     local args      = args or {}
56     local partition = args.partition or "/"
57     local timeout   = args.timeout or 600
58     local settings  = args.settings or function() end
59
60     widget = wibox.widget.textbox('')
61
62     helpers.set_map("fs", false)
63
64     function update()
65         fs_info = {} 
66
67         local f = io.popen("LC_ALL=C df -kP")
68
69         for line in f:lines() do -- Match: (size) (used)(avail)(use%) (mount)
70             local s     = string.match(line, "^.-[%s]([%d]+)")
71             local u,a,p = string.match(line, "([%d]+)[%D]+([%d]+)[%D]+([%d]+)%%")
72             local m     = string.match(line, "%%[%s]([%p%w]+)")
73
74             if u and m then -- Handle 1st line and broken regexp
75                 fs_info[m .. " size_mb"]  = string.format("%.1f", tonumber(s) / unit["mb"])
76                 fs_info[m .. " size_gb"]  = string.format("%.1f", tonumber(s) / unit["gb"])
77                 fs_info[m .. " used_p"]   = tonumber(p)
78                 fs_info[m .. " avail_p"]  = 100 - tonumber(p)
79             end
80         end
81
82         f:close()
83
84         -- chosen partition easy stuff
85         -- you can however check whatever partition else
86         used = fs_info[partition .. " used_p"]
87         available = fs_info[partition .. " avail_p"]
88         size_mb = fs_info[partition .. " size_mb"]
89         size_gb = fs_info[partition .. " size_gb"]
90
91         notification_preset = { fg = beautiful.fg_normal }
92
93         settings()
94
95         fs.notification_preset = notification_preset
96
97         if used >= 99 and not helpers.get_map("fs")
98         then
99             naughty.notify({ 
100                 title = "warning",
101                 text = partition .. " ran out!\nmake some room",
102                 timeout = 8,
103                 fg = "#000000",
104                 bg = "#FFFFFF"
105             })
106             helpers.set_map("fs", true)
107         else
108             helpers.set_map("fs", false)
109         end
110     end
111
112     helpers.newtimer("fs " .. partition, timeout, update)
113
114     widget:connect_signal('mouse::enter', function () fs:show(0) end)
115     widget:connect_signal('mouse::leave', function () fs:hide() end)
116
117     output = {
118         widget = widget,
119         show = function(t_out)
120                    update()
121                    fs:show(t_out)
122                end
123     }
124
125     return setmetatable(output, { __index = output.widget })
126 end
127
128 return setmetatable(fs, { __call = function(_, ...) return worker(...) end })