Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import math
- import sys
- import time
- icon = ''
- interval = 1 / 4
- previous_bytes = (0, 0)
- def glyphs_for_util(a, b):
- # TODO: Figure out way to make this the correct-width whitespace.
- glyphs = [' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█']
- i = int(min(a, 1.0) * (len(glyphs) - 1))
- j = int(min(b, 1.0) * (len(glyphs) - 1))
- # FIXME: Hack to pad right glyph to correct offset.
- if j != 0:
- i = max(1, i)
- return glyphs[i] + glyphs[j]
- def scale(val):
- MAX_RATE = 1 * 1024 * 1024
- # return math.log(val + 1) / math.log(MAX_RATE + 1)
- return val / MAX_RATE
- while True:
- with open('/sys/class/net/{}/statistics/rx_bytes'.format(sys.argv[1]), 'r') as rx_file:
- current_rx_bytes = int(rx_file.read())
- with open('/sys/class/net/{}/statistics/tx_bytes'.format(sys.argv[1]), 'r') as tx_file:
- current_tx_bytes = int(tx_file.read())
- #
- previous_rx_bytes = previous_bytes[0]
- previous_tx_bytes = previous_bytes[1]
- rx_rate = (current_rx_bytes - previous_rx_bytes) / interval
- tx_rate = (current_tx_bytes - previous_tx_bytes) / interval
- previous_bytes = (current_rx_bytes, current_tx_bytes)
- rx_ratio = scale(rx_rate)
- tx_ratio = scale(tx_rate)
- print('{} {}'.format(icon, glyphs_for_util(rx_ratio, tx_ratio)), flush=True)
- time.sleep(interval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement