aboutsummaryrefslogtreecommitdiffstats
path: root/examples/plugins/network
blob: 2288923ff6708e8dbae7f96adda74b95ddabb5d7 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- 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.network

import time
import threading

from plugins.network import Network
from plugins.clock import Clock


exclude = []


OUTPUT, HEIGHT, YPOS, TOP = 0, (len(Network(*exclude).devices) + 1) * 12, 24, True


clock = Clock(sync_to = Clock.SECONDS)

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


net_time = time.monotonic()
net_last = {}
def redraw():
    global net_time, net_last
    
    net_now = time.monotonic()
    net_tdiff, net_time = net_now - net_time, net_now
    net_ = Network(*exclude).devices
    
    def colourise(value):
        colour = '39'
        if value > 40:     colour = '32'
        if value > 8000:   colour = '33'
        if value > 60000:  colour = '31'
        return '\033[%sm%4.0f\033[0m' % (colour, value)
    
    def kbps(device, direction):
        direction += '_bytes'
        value = net_[device][direction]
        if device in net_last:
            value -= net_last[device][direction]
        else:
            value = 0
        value /= 128 * net_tdiff
        return colourise(value)
    
    def KBps(device, direction):
        direction += '_bytes'
        value = net_[device][direction]
        if device in net_last:
            value -= net_last[device][direction]
        else:
            value = 0
        value /= 1024 * net_tdiff
        return value
    
    def u(b):
        unit = 0
        units = ['', 'K', 'M', 'G', 'T', 'P', 'E']
        while (unit + 1 < len(units)) and (b >= 1024):
            b /= 1024
            unit += 1
        return '%.0f%s' % (b, units[unit])
    
    def total(device, direction):
        direction += '_bytes'
        value = net_[device][direction]
        return u(value) + 'B'
    
    def extra(device):
        rc = []
        table = net_[device]
        for key in table.keys():
            if key[3:] == 'bytes':
                continue
            if key.startswith('rx_'):
                if ('tx_' + key[3:]) in table:
                    rc.append('%s:%s↓%s↑' % (key[3:], u(table[key]), u(table['tx_' + key[3:]])))
                else:
                    rc.append('%s:%s↓' % (key[3:], u(table[key])))
            elif key.startswith('tx_'):
                if ('rx_' + key[3:]) not in table:
                    rc.append('%s:%s↑' % (key[3:], u(table[key])))
        return ' '.join(sorted(rc))
    
    devsum = {}
    for dev in net_.keys():
        table = net_[dev]
        for key in table.keys():
            if key not in devsum:
                devsum[key] = 0
            devsum[key] += table[key]
    net_['total'] = devsum
    
    net = [(dev,
            kbps(dev, 'rx'), KBps(dev, 'rx'), total(dev, 'rx'),
            kbps(dev, 'tx'), KBps(dev, 'tx'), total(dev, 'tx'),
            extra(dev))
           for dev in net_.keys()]
    text = ['%6s: %skbps(%3.0fKB/s %s)↓ %skbps(%3.0fKB/s %s)↑ %s' % x for x in net if not x[0] == 'total']
    text += ['%6s: %skbps(%3.0fKB/s %s)↓ %skbps(%3.0fKB/s %s)↑ %s' % x for x in net if x[0] == 'total']
    text = '\n'.join(text)
    net_last = net_
    
    bar.clear()
    bar.draw_coloured_text(0, 10, 0, 2, text)