1+ #! /usr/bin/python3
2+ import curses
3+ from curses .textpad import rectangle
4+ import os
5+ from time import sleep
6+ from subprocess import check_output
7+ import sys
8+ blocklist = ["▁" ,"▂" ,"▃" ,"▄" ,"▅" ,"▆" ,"▇" ,"█" ]
9+ #blocklist = [str(s) for s in [0,1,2,3,4,5,6,7]]#for testing
10+ IP = sys .argv [1 ]
11+ def ceil (a ,b ):
12+ return - (a // - b )
13+ def average (data : list ):
14+ a = 0
15+ for d in data :
16+ a += d
17+ return a / len (data )
18+
19+ def main (stdscr ) :
20+ stdscr .nodelay (1 )
21+ s = 0
22+ graph = [1 ]
23+ graphxpoint = 0
24+ tick = 0
25+ lostpackets = 0
26+ while True :
27+ tick += 1
28+ sx , sy = os .get_terminal_size ()
29+ try :
30+ rectangle (stdscr ,4 ,0 ,sy - 1 ,sx - 2 )
31+ except :
32+ pass
33+ try :
34+ _s = check_output (["ping" ,"-c" ,"1" ,IP ])
35+ except Exception as e :
36+ _s = f"dsfdfstime=5000 ms" .encode ()
37+ stdscr .addstr (sy - 1 ,0 ,f"ERROR: Ping get failed: { str (e )} " [0 :sx - 1 ])
38+ lostpackets += 1
39+ s = round (float (_s .decode ().split ("time=" )[1 ].split (" " )[0 ]))
40+
41+ graphy = sy - 7
42+ graphx = sx - 3
43+ maxgraphy = max (graph [graphxpoint :])
44+ graphyinc = (maxgraphy / graphy )
45+ stdscr .addstr (0 ,0 ,f"Watching { IP } " )
46+ stdscr .addstr (1 ,0 ,f"Current Ping: { s } ms" )
47+ stdscr .addstr (0 ,30 ,f"Max Ping: { max (graph [graphxpoint :])} ms" )#Using difft to 1s
48+ stdscr .addstr (1 ,30 ,f"Minimum Ping: { min (graph [graphxpoint :])} ms" )#Using difft to 1s
49+ stdscr .addstr (3 ,0 ,f"Graph Y Increment: { round (graphyinc )} ms" )
50+ stdscr .addstr (2 ,0 ,f"Average Ping: { round (average (graph [graphxpoint :]),3 )} ms" )
51+ stdscr .addstr (3 ,30 ,f"Watching for { tick } seconds" )
52+ stdscr .addstr (2 ,30 ,f"Packet Loss: { round (lostpackets / len (graph )* 100 ,3 )} %" )
53+
54+ #stdscr.addstr(0,20,f"CPU Cores: {psutil.cpu_count()}")
55+ if graphyinc == 0 :
56+ graphxpoint = 0
57+ graph = [1 ]
58+ graphyinc = 0.2
59+ ginc = 0
60+ for g in graph [graphxpoint :]:
61+ ginc += 1
62+ gy = round (g / graphyinc )
63+ gyf = g / graphyinc
64+ gyfmax = ceil (g ,graphyinc )
65+ if g == 0 :
66+ stdscr .addstr (sy - 2 ,ginc ,blocklist [0 ])
67+ else :
68+ stdscr .addstr (sy - 2 - gy ,ginc ,blocklist [round ((gyf / gyfmax * 8 )// 1 - 1 )])
69+ if gy > 0 :
70+ for i in range (gy ):
71+ stdscr .addstr (sy - 2 - (gy - i )+ 1 ,ginc ,blocklist [7 ])
72+ stdscr .refresh ()
73+ stdscr .getch ()
74+ sleep (1 )# Limiting to 1FPS to increase accuracy
75+ stdscr .erase ()
76+ graph .append (s )
77+ if len (graph ) > graphx - 1 :
78+ graphxpoint += 1
79+ curses .wrapper (main )
0 commit comments