aboutsummaryrefslogtreecommitdiffstats
path: root/examples/plugins/df
blob: be2fdf72477a216253595d95b9254e487274c294 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- python -*-
'''
xpybar – xmobar replacement written in python
Copyright © 2014, 2015, 2016, 2017, 2018, 2019  Mattias Andrée (m@maandreese)

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/>.
'''

# A xpybar configuration example testing the features of plugins.df

import time
import threading

from plugins.df import Discs
from plugins.clock import Clock


df_ = Discs()


OUTPUT, HEIGHT, YPOS, TOP = 0, len(df_.filesystems.keys()) * 12, 24, True


clock = Clock(sync_to = 5 * Clock.SECONDS)

start_ = start
def start():
    start_()
    xasync(lambda : clock.continuous_sync(lambda : bar.invalidate()))


def redraw():
    texts = []
    
    fs_ = max(len(df_.mountpoints[mp].filesystem) for mp in df_.mountpoints.keys())
    mp_ = max(len(df_.mountpoints[mp].mountpoint) for mp in df_.mountpoints.keys())
    ft_ = max(len(df_.mountpoints[mp].fstype)     for mp in df_.mountpoints.keys())
    
    def usage(value):
        colour = '39'
        if value < 50:  colour = '32'
        if value > 75:  colour = '33'
        if value > 95:  colour = '31'
        if value > 99:  colour = '41;33'
        return '\033[%sm%6.2f\033[m%%' % (colour, value)
    
    def u(b):
        unit = 0
        units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']
        while (unit + 1 < len(units)) and (b >= 1024):
            b /= 1024
            unit += 1
        return '%7.2f%s' % (b, units[unit])
    
    for fs in sorted(df_.filesystems.keys()):
        d = df_.filesystems[fs]
        text = 'fs: %s%s │ mount: %s%s │ type: %s%s │ size: %9s │ used: %9s │ free: %9s │ usage: %s │ inodes: %10i │ iused: %10i │ ifree: %10i │ iusage: %s'
        text %= (d.filesystem, ' ' * (fs_ - len(d.filesystem)),
                 d.mountpoint, ' ' * (mp_ - len(d.mountpoint)),
                 d.fstype,     ' ' * (ft_ - len(d.fstype)),
                 u(d.blocks), u(d.used), u(d.available), usage(100 * d.used / d.blocks),
                 d.inodes, d.iused, d.ifree, usage(100 * d.iused / d.inodes))
        texts.append(text)
    
    text = '\n'.join(texts)
    bar.partial_clear(0, bar.width, 10, 0, 2, text)
    bar.draw_coloured_text(0, 10, 0, 2, text)
    # TODO this is very flickery