Skip to content

Commit 35b229b

Browse files
committed
implemented OOP and added colours
1 parent bb64053 commit 35b229b

File tree

4 files changed

+80
-42
lines changed

4 files changed

+80
-42
lines changed

cpu_monitor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import psutil
2+
3+
class CPUMonitor:
4+
def __init__(self, bars=50):
5+
self.bars = bars
6+
7+
def get_cpu_usage(self):
8+
cpu = psutil.cpu_percent(interval=1)
9+
return self._format_usage(cpu)
10+
11+
def _format_usage(self, cpu):
12+
cpu_percent = cpu / 100.0
13+
cpu_visual = '#' * int(cpu_percent * self.bars) + '-' * (self.bars - int(cpu_percent * self.bars))
14+
return f"CPU: [{cpu_visual}] {cpu:.2f}%"

main.py

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,34 @@
11
import 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

3112
def 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("\nExiting...")
31+
print("\nExiting...")
32+
33+
if __name__ == "__main__":
34+
main()

memory_monitor.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import psutil
2+
3+
class MemoryMonitor:
4+
def __init__(self, bars=50):
5+
self.bars = bars
6+
7+
def get_memory_usage(self):
8+
memory = psutil.virtual_memory().percent
9+
return self._format_usage(memory)
10+
11+
def _format_usage(self, memory):
12+
memory_percent = memory / 100.0
13+
memory_visual = '#' * int(memory_percent * self.bars) + '-' * (self.bars - int(memory_percent * self.bars))
14+
return f"Memory: [{memory_visual}] {memory:.2f}%"
15+
16+
def get_memory_percentage(self):
17+
return psutil.virtual_memory().percent

network_monitor.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import psutil
2+
3+
class NetworkMonitor:
4+
def __init__(self):
5+
self.last_received = psutil.net_io_counters().bytes_recv
6+
self.last_sent = psutil.net_io_counters().bytes_sent
7+
8+
def get_bandwidth_usage(self):
9+
bytes_received = psutil.net_io_counters().bytes_recv
10+
bytes_sent = psutil.net_io_counters().bytes_sent
11+
12+
new_received = bytes_received - self.last_received
13+
new_sent = bytes_sent - self.last_sent
14+
15+
self.last_received = bytes_received
16+
self.last_sent = bytes_sent
17+
18+
received = new_received / 1024
19+
sent = new_sent / 1024
20+
total = (new_received + new_sent) / 1024
21+
22+
return f"Received: {received:.2f} KB | Sent: {sent:.2f} KB | Total: {total:.2f} KB"

0 commit comments

Comments
 (0)