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

version 1.0
[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 markup       = require("lain.util.markup")
12 local helpers      = require("lain.helpers")
13
14 local beautiful    = require("beautiful")
15 local wibox        = require("wibox")
16 local naughty      = require("naughty")
17
18 local io           = io
19 local string       = { match = string.match }
20 local tonumber     = tonumber
21
22 local setmetatable = setmetatable
23
24 -- File system disk space usage
25 -- lain.widgets.fs
26 local fs = {}
27 local notification = nil
28
29 function fs:hide()
30     if notification ~= nil then
31         naughty.destroy(notification)
32         notification = nil
33     end
34 end
35
36 function fs:show(t_out)
37     fs:hide()
38
39     local f = io.popen(helpers.scripts_dir .. "dfs")
40     ws = f:read("*all"):gsub("\n*$", "")
41     f:close()
42
43     notification = naughty.notify({
44         text = ws,
45         timeout = t_out,
46         fg = beautiful.fg_focus,
47     })
48 end
49
50 -- Variable definitions
51 local unit = { ["mb"] = 1024, ["gb"] = 1024^2 }
52
53 local function worker(args)
54     local args = args or {}
55     local partition = args.partition or "/"
56     local refresh_timeout = args.refresh_timeout or 600
57     local header = args.header or " Hdd "
58     local header_color = args.header_color or beautiful.fg_normal or "#FFFFFF"
59     local color = args.color or beautiful.fg_focus or "#FFFFFF"
60     local footer = args.header or ""
61     local shadow = args.shadow or false
62
63     local myfs = wibox.widget.textbox()
64
65     helpers.set_map("fs", false)
66
67     local fsupdate = function()
68         local fs_info = {} -- Get data from df
69         local f = io.popen("LC_ALL=C df -kP")
70
71         local function set_text()
72             local info = fs_info['{' .. partition .. ' used_p}']
73             myfs:set_markup(markup(header_color, header)
74                             .. markup(color, info .. footer) .. " ")
75         end
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                 helpers.uformat(fs_info, m .. " used",  u, unit)
84                 fs_info["{" .. m .. " used_p}"]  = tonumber(p)
85             end
86         end
87
88         f:close()
89
90         if shadow
91         then
92             myfs:set_text('')
93         else
94             set_text()
95         end
96
97         local part = fs_info['{' .. partition .. ' used_p}']
98
99         if part >= 90  then
100             if part >= 99 and not helpers.get_map("fs") then
101                 naughty.notify({ title = "warning",
102                                  text = partition .. " ran out!\n"
103                                         .. "make some room",
104                                  timeout = 8,
105                                  position = "top_right",
106                                  fg = beautiful.fg_urgent,
107                                  bg = beautiful.bg_urgent })
108                 helpers.set_map("fs", true)
109             end
110             if shadow then set_text() end
111         end
112     end
113
114     local fstimer = timer({ timeout = refresh_timeout })
115     fstimer:connect_signal("timeout", fsupdate)
116     fstimer:start()
117     fstimer:emit_signal("timeout")
118
119     myfs:connect_signal('mouse::enter', function () fs:show(0) end)
120     myfs:connect_signal('mouse::leave', function () fs:hide() end)
121
122     local fs_out =
123     {
124         widget = myfs,
125         show = function(t_out)
126                    fsupdate()
127                    fs:show(t_out)
128                end
129     }
130
131     return setmetatable(fs_out, { __index = fs_out.widget })
132 end
133
134 return setmetatable(fs, { __call = function(_, ...) return worker(...) end })