aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/mytop.py
blob: 6dc141562ca10f998f3404cabddeb1ca6f93536f (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
# -*- python -*-
from common import *

class MyTop(Entry):
    def __init__(self, *args, **kwargs):
        self.show_pid = True
        self.show_cpu = True
        self.show_label = True
        self.show_nic = None
        self.top_cmd = pdeath('HUP', 'top', '-b', '-n', '1', '-o', '%CPU', '-w', '10000')
        self.refresh()
        Entry.__init__(self, *args, **kwargs)
        xasync(lambda : watch(5, t(self.refresh)), name = 'top')
    
    def action(self, col, button, x, y):
        if button == LEFT_BUTTON:
            self.show_pid = not self.show_pid
        elif button == MIDDLE_BUTTON:
            self.show_label = not self.show_label
        elif button == RIGHT_BUTTON:
            self.show_cpu = not self.show_cpu
        else:
            return
        self.refresh()
        self.invalidate()
    
    def refresh(self):
        top = spawn_read(*self.top_cmd).split('\n')
        top = [line for line in top if not line.startswith('%')][6]
        top = [col for col in top.replace('\t', ' ').split(' ') if not col == '']
        text = 'Top: ' if self.show_label else ''
        if self.show_pid:
            text += top[0] + ' '
        if self.show_cpu:
            text += top[6] + ' '
        text += ' '.join(top[10:])
        self.text = text
    
    def function(self):
        return self.text