From 9fa1f8845cb6914caffe796d264e67361c87919d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Urba=C5=84czyk?= Date: Sat, 18 Jan 2020 13:08:30 +0100 Subject: [PATCH] added cpuload block --- i3-dstatus.conf | 14 +++--- i3dstatus/__main__.py | 0 i3dstatus/generators/cpuload | 84 ++++++++++++++++++++++++++++++++++++ setup.py | 6 --- 4 files changed, 93 insertions(+), 11 deletions(-) delete mode 100644 i3dstatus/__main__.py create mode 100755 i3dstatus/generators/cpuload diff --git a/i3-dstatus.conf b/i3-dstatus.conf index 1378d59..710ba2c 100644 --- a/i3-dstatus.conf +++ b/i3-dstatus.conf @@ -5,10 +5,8 @@ # string to use the markup features --- general: - generators: [ focused-window, scratchpad, playerctl, clipboard, netifaces, disk, battery, clock ] - separator-block-width: 25 -clock: - format: '%h %d %H:%M' + generators: [ github-repos, scratchpad, playerctl, netifaces, disk, cpuload, clock ] + separator-block-width: 15 disk: '/': prefix: 'custom' @@ -31,9 +29,15 @@ check-http: format-up: '' format-down: '%site is down (status: %status, %reason)' github-repos: - users: [ altdesktop ] + users: [ robertu ] interval: 600 format: '%stars ?%issues' battery: name: 'BAT0' # defaults to the first battery found format: '%name %percentage%' +clock: + format: '%d %h %Y [ %H:%M ] ' + +cpuload: + interval: 10 + format: 'cpu %cpuload %bar' diff --git a/i3dstatus/__main__.py b/i3dstatus/__main__.py deleted file mode 100644 index e69de29..0000000 diff --git a/i3dstatus/generators/cpuload b/i3dstatus/generators/cpuload new file mode 100755 index 0000000..7820c01 --- /dev/null +++ b/i3dstatus/generators/cpuload @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 + +from i3dstatus.block import Block + +import time +import atexit +from requests.exceptions import ConnectionError +import os +import asyncio + +block = Block(os.path.basename(__file__)) + +# display aggregate info on your users github repos + +block_name = 'cpuload' + +INTERVAL = 0.1 + +def getTimeList(): + """ + Fetches a list of time units the cpu has spent in various modes + Detailed explanation at http://www.linuxhowtos.org/System/procstat.htm + """ + cpuStats = open("/proc/stat", "r").readline() + columns = cpuStats.replace("cpu", "").split(" ") + return map(int, filter(None, columns)) + +val = "▁▂▃▄▅▆▇█" +current = "▁▁▁load▁" + +def deltaTime(interval): + """ + Returns the difference of the cpu statistics returned by getTimeList + that occurred in the given time delta + """ + timeList1 = getTimeList() + time.sleep(interval) + timeList2 = getTimeList() + return [(t2-t1) for t1, t2 in zip(timeList1, timeList2)] + +def getCpuLoad(): + """ + Returns the cpu load as a value from the interval [0.0, 1.0] + """ + dt = list(deltaTime(INTERVAL)) + idle_time = float(dt[3]) + total_time = sum(dt) + load = 1-(idle_time/total_time) + return load + + + +async def show_block(block_format): + global current, val + + load = getCpuLoad() + step = int(load*17) + if step > 7: + step = 7 + current = (current+val[step])[1:] + context = { + 'cpuload': f"{'%3.2f' % (load*100.0)}%", + 'bar': current, + } + await block.show(block_format, context=context, markup='pango') + + +async def main(): + + await block.connect() + block_format = "%cpuload %bar" + interval = 5 + if 'format' in block.config: + block_format = block.config['format'] + if 'interval' in block.config: + interval = int(block.config['interval']) + + while True: + await show_block(block_format) + await asyncio.sleep(interval) + + +loop = asyncio.get_event_loop() +loop.run_until_complete(main()) diff --git a/setup.py b/setup.py index 5c4e17f..2e8ead4 100644 --- a/setup.py +++ b/setup.py @@ -22,11 +22,5 @@ scripts=['i3-dstatus'], - entry_points={ - 'console_scripts': [ - 'i3-dstatus = i3dstatus.__main__:main' - ] - }, - packages=find_packages(), )