aboutsummaryrefslogblamecommitdiffstats
path: root/examples/plugins/network
blob: 2bfb76bb74a0f822d54ca4214128db425c9ba23f (plain) (tree)















































































































                                                                                                              
# -*- python -*-

# 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_()
    async(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 < 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)