11import time
2- import psutil
2+ from cpu_monitor import CPUMonitor
3+ from memory_monitor import MemoryMonitor
4+ from network_monitor import NetworkMonitor
35
4- # Function to display CPU and memory usage in a visual format
5- def cpu_and_memory_usage (cpu , memory , bars = 50 ):
6- cpu_percent = (cpu / 100.0 )
7- cpu_visual = '#' * int (cpu_percent * bars ) + '-' * (bars - int (cpu_percent * bars ))
8-
9- memory_percent = (memory / 100.0 )
10- memory_visual = '#' * int (memory_percent * bars ) + '-' * (bars - int (memory_percent * bars ))
11-
12- return f"CPU: [{ cpu_visual } ] { cpu :.2f} % | Memory: [{ memory_visual } ] { memory :.2f} %"
13-
14- # Function to monitor network bandwidth usage
15- def bandwidth_monitor (last_received , last_sent ):
16- # Get the current number of bytes received and sent
17- bytes_received = psutil .net_io_counters ().bytes_recv
18- bytes_sent = psutil .net_io_counters ().bytes_sent
19-
20- # Calculate the difference from the previous values
21- new_received = bytes_received - last_received
22- new_sent = bytes_sent - last_sent
23-
24- # Convert to kilobytes
25- received = new_received / 1024
26- sent = new_sent / 1024
27- total = (new_received + new_sent ) / 1024
28-
29- return f"Received: { received :.2f} KB | Sent: { sent :.2f} KB | Total: { total :.2f} KB" , bytes_received , bytes_sent
6+ # ANSI color escape codes
7+ COLOR_RESET = "\033 [0m"
8+ COLOR_YELLOW = "\033 [93m" # Yellow for CPU
9+ COLOR_GREEN = "\033 [92m" # Green for Memory
10+ COLOR_BLUE = "\033 [94m" # Blue for Bandwidth
3011
3112def main ():
32- # Get the initial values for bytes received and sent
33- last_received = psutil . net_io_counters (). bytes_recv
34- last_sent = psutil . net_io_counters (). bytes_sent
13+ cpu_monitor = CPUMonitor ()
14+ memory_monitor = MemoryMonitor ()
15+ network_monitor = NetworkMonitor ()
3516
36- while True :
37- # Get CPU, memory, and bandwidth usage
38- cpu_memory_output = cpu_and_memory_usage (psutil .cpu_percent (), psutil .virtual_memory ().percent )
39- bandwidth_output , last_received , last_sent = bandwidth_monitor (last_received , last_sent )
40-
41- # Clear the screen and print the updated output
42- print (f"\033 [H\033 [J{ cpu_memory_output } \n { bandwidth_output } " , end = '' )
43- time .sleep (0.5 ) # Wait for 0.5 seconds before updating again
44-
45- if __name__ == "__main__" :
4617 try :
47- main ()
18+ while True :
19+ cpu_output = cpu_monitor .get_cpu_usage ()
20+ memory_output = memory_monitor .get_memory_usage ()
21+ bandwidth_output = network_monitor .get_bandwidth_usage ()
22+
23+ # Apply colors to entire output strings
24+ cpu_output = f"{ COLOR_YELLOW } { cpu_output } { COLOR_RESET } "
25+ memory_output = f"{ COLOR_GREEN } { memory_output } { COLOR_RESET } "
26+ bandwidth_output = f"{ COLOR_BLUE } { bandwidth_output } { COLOR_RESET } "
27+
28+ print (f"\033 [H\033 [J{ cpu_output } | { memory_output } \n { bandwidth_output } " , end = '\r ' )
29+ time .sleep (0.5 ) # Wait for 0.5 seconds before updating again
4830 except KeyboardInterrupt :
49- print ("\n Exiting..." )
31+ print ("\n Exiting..." )
32+
33+ if __name__ == "__main__" :
34+ main ()
0 commit comments