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

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