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

Merge pull request #296 from 2009/patch-2
[etc/awesome.git] / widgets / fs.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013, Luke Bonham                     
6                                                   
7 --]]
8
9 local helpers      = require("lain.helpers")
10
11 local shell        = require("awful.util").shell
12 local focused      = require("awful.screen").focused
13 local wibox        = require("wibox")
14 local naughty      = require("naughty")
15
16 local string       = string
17 local tonumber     = tonumber
18
19 local setmetatable = setmetatable
20
21 -- File system disk space usage
22 -- lain.widgets.fs
23 local fs = { unit  = { ["mb"] = 1024, ["gb"] = 1024^2 } }
24
25 function fs.hide()
26     if not fs.notification then return end
27     naughty.destroy(fs.notification)
28     fs.notification = nil
29 end
30
31 function fs.show(seconds, scr)
32     fs.update()
33
34     fs.hide()
35
36     if fs.followtag then
37         fs.notification_preset.screen = focused()
38     elseif scr then
39         fs.notification_preset.screen = scr or 1
40     end
41
42     fs.notification = naughty.notify({
43         preset      = fs.notification_preset,
44         timeout     = seconds or 5
45     })
46 end
47
48 local function worker(args)
49     local args             = args or {}
50     local timeout          = args.timeout or 600
51     local partition        = args.partition or "/"
52     local showpopup        = args.showpopup or "on"
53     local notify           = args.notify or "on"
54     local settings         = args.settings or function() end
55
56     fs.options             = args.options
57     fs.followtag           = args.followtag or false
58     fs.notification_preset = args.notification_preset
59
60     if not fs.notification_preset then
61         fs.notification_preset = {
62             font = "Monospace 10",
63             fg   = "#FFFFFF",
64             bg   = "#000000"
65         }
66     end
67
68     fs.widget = wibox.widget.textbox()
69
70     helpers.set_map(partition, false)
71
72     function fs.update()
73         fs_info, fs_now  = {}, {}
74         helpers.async({ shell, "-c", "LC_ALL=C df -k --output=target,size,used,avail,pcent" }, function(f)
75             for line in string.gmatch(f, "\n[^\n]+") do
76                 local m,s,u,a,p = string.match(line, "(/.-%s).-(%d+).-(%d+).-(%d+).-([%d]+)%%")
77                 m = m:gsub(" ", "") -- clean target from any whitespace
78
79                 fs_info[m .. " size_mb"]  = string.format("%.1f", tonumber(s) / fs.unit["mb"])
80                 fs_info[m .. " size_gb"]  = string.format("%.1f", tonumber(s) / fs.unit["gb"])
81                 fs_info[m .. " used_mb"]  = string.format("%.1f", tonumber(u) / fs.unit["mb"])
82                 fs_info[m .. " used_gb"]  = string.format("%.1f", tonumber(u) / fs.unit["gb"])
83                 fs_info[m .. " used_p"]   = p
84                 fs_info[m .. " avail_mb"] = string.format("%.1f", tonumber(a) / fs.unit["mb"])
85                 fs_info[m .. " avail_gb"] = string.format("%.1f", tonumber(a) / fs.unit["gb"])
86                 fs_info[m .. " avail_p"]  = string.format("%d", 100 - tonumber(p))
87             end
88
89             fs_now.size_mb      = fs_info[partition .. " size_mb"]  or "N/A"
90             fs_now.size_gb      = fs_info[partition .. " size_gb"]  or "N/A"
91             fs_now.used         = fs_info[partition .. " used_p"]   or "N/A"
92             fs_now.used_mb      = fs_info[partition .. " used_mb"]  or "N/A"
93             fs_now.used_gb      = fs_info[partition .. " used_gb"]  or "N/A"
94             fs_now.available    = fs_info[partition .. " avail_p"]  or "N/A"
95             fs_now.available_mb = fs_info[partition .. " avail_mb"] or "N/A"
96             fs_now.available_gb = fs_info[partition .. " avail_gb"] or "N/A"
97
98             notification_preset = fs.notification_preset
99             widget = fs.widget
100             settings()
101
102             if notify == "on" and tonumber(fs_now.used) >= 99 and not helpers.get_map(partition) then
103                 naughty.notify({
104                     preset = naughty.config.presets.critical,
105                     title  = "Warning",
106                     text   = partition .. " is empty",
107                 })
108                 helpers.set_map(partition, true)
109             else
110                 helpers.set_map(partition, false)
111             end
112         end)
113
114         local notifycmd = (fs.options and string.format("dfs %s", fs.options)) or "dfs"
115         helpers.async(helpers.scripts_dir .. notifycmd, function(ws)
116             fs.notification_preset.text = ws:gsub("\n*$", "")
117         end)
118     end
119
120     if showpopup == "on" then
121        fs.widget:connect_signal('mouse::enter', function () fs.show(0) end)
122        fs.widget:connect_signal('mouse::leave', function () fs.hide() end)
123     end
124
125     helpers.newtimer(partition, timeout, fs.update)
126
127     return fs
128 end
129
130 return setmetatable(fs, { __call = function(_, ...) return worker(...) end })