1+ import curses
12import time
23from cpu_monitor import CPUMonitor
34from memory_monitor import MemoryMonitor
45from network_monitor import NetworkMonitor
56from system_info import SystemInfo
67from processes import ProcessMonitor
78
8- # ANSI color escape codes
9- COLOR_RESET = "\033 [0m"
10- COLOR_YELLOW = "\033 [93m" # Yellow for CPU
11- COLOR_GREEN = "\033 [92m" # Green for Memory
12- COLOR_BLUE = "\033 [94m" # Blue for Bandwidth
13- COLOR_CYAN = "\033 [96m" # Cyan for Headers
9+ def main (stdscr ):
10+ # Clear screen
11+ stdscr .clear ()
12+ curses .curs_set (0 ) # Hide the cursor
1413
15- def main ():
1614 cpu_monitor = CPUMonitor ()
1715 memory_monitor = MemoryMonitor ()
1816 network_monitor = NetworkMonitor ()
@@ -21,46 +19,58 @@ def main():
2119
2220 try :
2321 while True :
22+ # Get screen dimensions
23+ height , width = stdscr .getmaxyx ()
24+ half_width = width // 2
25+
2426 # Get real-time data
2527 cpu_output = cpu_monitor .get_cpu_usage ()
2628 memory_output = memory_monitor .get_memory_usage ()
2729 bandwidth_output = network_monitor .get_bandwidth_usage ()
2830 sys_info_output = system_info .get_system_info ()
2931 processes_output = process_monitor .get_running_processes (num_processes = 10 )
3032
31- # Apply colors to entire output strings
32- cpu_output = f"{ COLOR_YELLOW } { cpu_output } { COLOR_RESET } "
33- memory_output = f"{ COLOR_GREEN } { memory_output } { COLOR_RESET } "
34- bandwidth_output = f"{ COLOR_BLUE } { bandwidth_output .replace ('Received' , '↓' ).replace ('Sent' , '↑' )} { COLOR_RESET } "
35- sys_info_output = f"{ COLOR_CYAN } { sys_info_output } { COLOR_RESET } "
36- processes_output = "\n " .join ([f"{ COLOR_CYAN } Running Processes{ COLOR_RESET } " , "-----------------" ] + processes_output )
37-
3833 # Clear screen
39- print ( " \033 c" , end = '' )
34+ stdscr . clear ( )
4035
41- # Print headers
42- print (f"{ COLOR_CYAN } CPU & Memory Network{ COLOR_RESET } " )
43- print (f"{ COLOR_CYAN } ------------- -------{ COLOR_RESET } " )
36+ # Define color pairs
37+ curses .start_color ()
38+ curses .init_pair (1 , curses .COLOR_YELLOW , curses .COLOR_BLACK )
39+ curses .init_pair (2 , curses .COLOR_GREEN , curses .COLOR_BLACK )
40+ curses .init_pair (3 , curses .COLOR_BLUE , curses .COLOR_BLACK )
41+ curses .init_pair (4 , curses .COLOR_CYAN , curses .COLOR_BLACK )
42+ curses .init_pair (5 , curses .COLOR_RED , curses .COLOR_BLACK )
4443
45- # Print meters and system info
46- print (f"{ cpu_output } { bandwidth_output } " )
47- print (f"{ memory_output } " )
48- print (f"{ COLOR_CYAN } System Info{ COLOR_RESET } " )
49- print (f"{ COLOR_CYAN } -----------{ COLOR_RESET } " )
50- sys_info_lines = [line [:40 ] for line in sys_info_output .split ("\n " )]
51- for line in sys_info_lines :
52- print (line )
44+ # Print CPU and Memory Usage
45+ stdscr .addstr (0 , 0 , "CPU Usage" , curses .color_pair (4 ))
46+ stdscr .addstr (1 , 0 , cpu_output [:half_width ], curses .color_pair (1 ))
47+ stdscr .addstr (2 , 0 , "Memory Usage" , curses .color_pair (4 ))
48+ stdscr .addstr (3 , 0 , memory_output [:half_width ], curses .color_pair (2 ))
5349
54- # Print Running Processes
55- print (processes_output )
50+ # Print Bandwidth Usage
51+ stdscr .addstr (0 , half_width , "Network Usage" , curses .color_pair (4 ))
52+ stdscr .addstr (1 , half_width , bandwidth_output .replace ('Received' , '↓' ).replace ('Sent' , '↑' )[:half_width ], curses .color_pair (3 ))
5653
57- # Move cursor to the top
58- print ("\033 [6A" , end = '' )
54+ # Print System Info
55+ stdscr .addstr (5 , 0 , "System Info" , curses .color_pair (4 ))
56+ stdscr .addstr (6 , 0 , "-----------" , curses .color_pair (4 ))
57+ sys_info_lines = [line [:half_width ] for line in sys_info_output .split ("\n " )]
58+ for idx , line in enumerate (sys_info_lines ):
59+ if 7 + idx < height :
60+ stdscr .addstr (7 + idx , 0 , line , curses .color_pair (4 ))
61+
62+ # Print Running Processes
63+ stdscr .addstr (5 , half_width , "Running Processes" , curses .color_pair (4 ))
64+ stdscr .addstr (6 , half_width , "-----------------" , curses .color_pair (4 ))
65+ for idx , process in enumerate (processes_output ):
66+ if 7 + idx < height :
67+ stdscr .addstr (7 + idx , half_width , process [:half_width ], curses .color_pair (5 ))
5968
69+ stdscr .refresh ()
6070 time .sleep (0.5 ) # Wait for 0.5 seconds before updating again
6171
6272 except KeyboardInterrupt :
6373 print ("\n Exiting..." )
6474
6575if __name__ == "__main__" :
66- main ( )
76+ curses . wrapper ( main )
0 commit comments