]> git.madduck.net Git - etc/awesome.git/blob - widget/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:

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