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

Create acw.lua
[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 = {}
28
29 local notification  = nil
30 fs_notification_preset = { fg = beautiful.fg_normal }
31
32 function fs:hide()
33     if notification ~= nil then
34         naughty.destroy(notification)
35         notification = nil
36     end
37 end
38
39 function fs:show(t_out)
40     fs:hide()
41
42     local f = io.popen(helpers.scripts_dir .. "dfs")
43     ws = f:read("*all"):gsub("\n*$", "")
44     f:close()
45
46     notification = naughty.notify({
47         preset = fs_notification_preset,
48         text = ws,
49         timeout = t_out
50     })
51 end
52
53 -- Units definitions
54 local unit = { ["mb"] = 1024, ["gb"] = 1024^2 }
55
56 local function worker(args)
57     local args      = args or {}
58     local timeout   = args.timeout or 600
59     local partition = args.partition or "/"
60     local settings  = args.settings or function() end
61
62     fs.widget = wibox.widget.textbox('')
63
64     helpers.set_map("fs", false)
65
66     function update()
67         fs_info = {}
68         fs_now  = {}
69
70         local f = io.popen("LC_ALL=C df -kP")
71
72         for line in f:lines() do -- Match: (size) (used)(avail)(use%) (mount)
73             local s     = string.match(line, "^.-[%s]([%d]+)")
74             local u,a,p = string.match(line, "([%d]+)[%D]+([%d]+)[%D]+([%d]+)%%")
75             local m     = string.match(line, "%%[%s]([%p%w]+)")
76
77             if u and m then -- Handle 1st line and broken regexp
78                 fs_info[m .. " size_mb"]  = string.format("%.1f", tonumber(s) / unit["mb"])
79                 fs_info[m .. " size_gb"]  = string.format("%.1f", tonumber(s) / unit["gb"])
80                 fs_info[m .. " used_p"]   = tonumber(p)
81                 fs_info[m .. " avail_p"]  = 100 - tonumber(p)
82             end
83         end
84
85         f:close()
86
87         -- chosen partition easy stuff
88         -- you can however check whatever partition else
89         fs_now.used      = tonumber(fs_info[partition .. " used_p"])  or 0
90         fs_now.available = tonumber(fs_info[partition .. " avail_p"]) or 0
91         fs_now.size_mb   = tonumber(fs_info[partition .. " size_mb"]) or 0
92         fs_now.size_gb   = tonumber(fs_info[partition .. " size_gb"]) or 0
93
94         widget = fs.widget
95         settings()
96
97         if fs_now.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(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     return setmetatable(fs, { __index = fs.widget })
118 end
119
120 return setmetatable(fs, { __call = function(_, ...) return worker(...) end })