aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-03-01 13:11:06 +0100
committerMattias Andrée <maandree@operamail.com>2014-03-01 13:11:06 +0100
commit3360309b71a2e97ebcc4a1f425bc98b59ae9fdc9 (patch)
treee0cf37a290c9bc5de155550eeb14a4110ccb1f6f
parentadd uptime and idle time in seconds format (diff)
downloadxpybar-3360309b71a2e97ebcc4a1f425bc98b59ae9fdc9.tar.gz
xpybar-3360309b71a2e97ebcc4a1f425bc98b59ae9fdc9.tar.bz2
xpybar-3360309b71a2e97ebcc4a1f425bc98b59ae9fdc9.tar.xz
add df
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r--examples/plugin-test28
-rw-r--r--src/plugins/df.py116
2 files changed, 142 insertions, 2 deletions
diff --git a/examples/plugin-test b/examples/plugin-test
index 145fcec..c68065e 100644
--- a/examples/plugin-test
+++ b/examples/plugin-test
@@ -9,6 +9,7 @@ from plugins.loadavg import AverageLoad
from plugins.users import Users
from plugins.pacman import Pacman
from plugins.uname import Uname
+from plugins.df import Discs
OUTPUT, HEIGHT, YPOS, TOP = 0, 24, 24, True
@@ -82,7 +83,30 @@ def redraw():
uname = '%s %s %s'
uname %= (nodename, kernel_release, operating_system)
- text = '%s │ %s │ %s │ %s │ %s │ %s'
- text %= (time, uptime, idle, loadavg, users, uname)
+ discs_ = Discs()
+ discs = []
+ for fs in discs_.filesystems:
+ if '/' not in fs:
+ continue
+ disc = discs_.filesystems[fs]
+ mp = disc.mountpoint
+ if mp in ['/boot', '/rescue']:
+ continue
+ mp = mp.split('/')[-1]
+ if mp == '':
+ mp = '/'
+ use = disc.used * 100 / disc.blocks
+ colour, colour_ = '39', '39'
+ if use < 50: colour = '32'
+ if use > 75: colour = '33'
+ if use > 95: colour = '31'
+ if use > 99: colour, colour_ = '31', '31'
+ discs.append((mp, use, colour, colour_))
+ discs.sort(key = lambda d : d[0])
+ discs = ['\033[%sm%s:\033[%sm%.1f\033[0m%%' % (d[3], d[0], d[2], d[1]) for d in discs]
+ discs = 'df: %s' % ' '.join(discs)
+
+ text = '%s │ %s │ %s │ %s │ %s │ %s\n%s'
+ text %= (time, uptime, idle, loadavg, users, uname, discs)
bar.draw_coloured_text(0, 10, 0, 2, text)
diff --git a/src/plugins/df.py b/src/plugins/df.py
new file mode 100644
index 0000000..81ad5a2
--- /dev/null
+++ b/src/plugins/df.py
@@ -0,0 +1,116 @@
+# -*- python -*-
+'''
+xpybar – xmobar replacement written in python
+Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+'''
+
+from util import *
+
+
+class Discs:
+ '''
+ Retrieve information about mounted discs
+
+ @variable filesystems:dict<str, Disc> Map from filesystem to disc
+ @variable mountpoints:dict<str, Disc> Map from mountpoint to disc
+ '''
+
+
+ def __init__(self):
+ '''
+ Constructor
+ '''
+ self.filesystems = {}
+ self.mountpoints = {}
+ listed = {}
+
+ df = spawn_read('df', '-B1', '-T').split('\n')[1:]
+ dfi = spawn_read('df', '-i').split('\n')[1:]
+
+ # Disc usage information
+ for line in df:
+ line = line
+ buf = ''
+ cells = []
+ for c in line:
+ if (c == ' ') and (len(cells) < 6):
+ if not buf == '':
+ cells.append(buf)
+ buf = ''
+ else:
+ buf += c
+ cells.append(buf)
+ (fs, fstype, blocks, used, avail, _use, mount) = cells
+ listed[fs] = mount
+ disc = Disc()
+ self.filesystems[fs] = disc
+ self.mountpoints[mount] = disc
+ disc.filesystem = fs
+ disc.mountpoint = mount
+ disc.fstype = fstype
+ disc.blocks = int(blocks)
+ disc.used = int(used)
+ disc.available = int(avail)
+
+ # Inode usage information
+ for line in dfi:
+ line = line
+ buf = ''
+ cells = []
+ for c in line:
+ if (c == ' ') and (len(cells) < 5):
+ if not buf == '':
+ cells.append(buf)
+ buf = ''
+ else:
+ buf += c
+ cells.append(buf)
+ (fs, inodes, iused, ifree, _use, _mount) = cells
+ if fs in self.filesystems:
+ del listed[fs]
+ disc = self.filesystems[fs]
+ disc.inodes = int(inodes)
+ disc.iused = int(iused)
+ disc.ifree = int(ifree)
+
+ # Perhaps an umount appeared between `df -B1 -T` and `df -i`
+ for fs in listed.keys():
+ del self.filesystems[fs]
+ del self.mountpoints[listed[fs]]
+
+
+class Disc:
+ '''
+ Information about a single disc
+
+ @variable filesystem:str The filesystem, a device or API filesystem name
+ @variable mountpoint:str The filesystem mountpoint, the one with shortest name if there are multiple
+ @variable fstype:str The filesystem type
+ @variable blocks:int The total number of 1 byte blocks, the value is close or equal to `used + available`
+ @variable used:int The number of used bytes
+ @variable available:int The number of unused bytes
+ @variable inodes:int The total number of index nodes
+ @variable iused:int The number of used index nodes
+ @variable ifree:int The number of available index nodes
+ '''
+
+
+ def __init__(self):
+ '''
+ Constructor
+ '''
+ pass
+