aboutsummaryrefslogtreecommitdiffstats
path: root/examples/plugins/cpu
blob: 5daf2905134f7488aa9ec348a814d0f56254ea8c (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
# -*- 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.cpu

import time
import threading

from plugins.cpu import CPU
from plugins.clock import Clock


OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 24, True


clock = Clock(sync_to = Clock.SECONDS / 2)

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


last_cpu_idle, last_cpu_total = 0, 0
last_cpus_idle, last_cpus_total = [], []

def redraw():
    global last_cpus_idle, last_cpus_total, last_cpu_idle, last_cpu_total
    
    cpu_ = CPU()
    now_cpu_idle, now_cpus_idle = cpu_.cpu[CPU.idle], [cpu[CPU.idle] for cpu in cpu_.cpus]
    now_cpu_total, now_cpus_total = sum(cpu_.cpu), [sum(cpu) for cpu in cpu_.cpus]
    
    def cpu_usage(now_idle, now_total, last_idle, last_total):
        total = now_total - last_total
        idle = now_idle - last_idle
        return None if total == 0 else (total - idle) * 100 / total
    
    def colourise(value):
        if value is None:
            return '--%'
        colour = '39'
        if value >= 5:   colour = '32'
        if value >= 50:  colour = '33'
        if value >= 90:  colour = '31'
        return '\033[%sm%2.0f\033[0m%%' % (colour, value)
    
    if len(now_cpus_idle) > len(last_cpus_idle):
        last_cpus_idle += now_cpus_idle[len(last_cpus_idle):]
        last_cpus_total += now_cpus_total[len(last_cpus_total):]
    
    cpus = zip(now_cpus_idle, now_cpus_total, last_cpus_idle, last_cpus_total)
    cpus = ' '.join([colourise(cpu_usage(*c)) for c in cpus])
    cpu = colourise(cpu_usage(now_cpu_idle, now_cpu_total, last_cpu_idle, last_cpu_total))
    cpu = 'Cpu: %s : %s' % (cpus, cpu)
    
    last_cpus_idle, last_cpus_total = now_cpus_idle, now_cpus_total
    last_cpu_idle, last_cpu_total = now_cpu_idle, now_cpu_total
    
    def s(title, value):
        if value is None:
            return ''
        elif isinstance(value, list):
            return ' │ %s: %s' % (title, ' '.join([str(v) for v in value]))
        else:
            return ' │ %s: %s' % (title, str(value))
    
    text = '%s │ Intr: %i │ Ctxt: %i │ Btime: %i │ Procs: %i%s%s%s%s'
    text %= (cpu, cpu_.intr_total, cpu_.ctxt, cpu_.btime, cpu_.processes,
             s('Run', cpu_.procs_running), s('Blck', cpu_.procs_blocked),
             s('Softirq', cpu_.softirq_total), s('Softirq', cpu_.softirq))
    bar.clear()
    bar.draw_coloured_text(0, 10, 0, 2, text)