Added --no-color argument for boring people.
This commit is contained in:
+26
-11
@@ -14,11 +14,15 @@ import argparse
|
|||||||
import tty
|
import tty
|
||||||
import termios
|
import termios
|
||||||
|
|
||||||
GREEN = "\033[32m"
|
def supports_color():
|
||||||
YELLOW = "\033[33m"
|
if not sys.stdout.isatty():
|
||||||
ORANGE = "\033[38;5;214m"
|
return False
|
||||||
RED = "\033[31m"
|
term = os.environ.get('TERM', '')
|
||||||
RESET_TO_GREEN = "\033[32m"
|
if 'color' in term.lower():
|
||||||
|
return True
|
||||||
|
if os.name == 'nt':
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def clear_screen():
|
def clear_screen():
|
||||||
os.system('cls' if os.name == 'nt' else 'clear')
|
os.system('cls' if os.name == 'nt' else 'clear')
|
||||||
@@ -43,25 +47,32 @@ def get_top_processes(limit):
|
|||||||
processes.sort(reverse=True, key=lambda p: p[0])
|
processes.sort(reverse=True, key=lambda p: p[0])
|
||||||
return processes[:limit]
|
return processes[:limit]
|
||||||
|
|
||||||
def colorize(cpu, name, user):
|
def colorize(cpu, name, user, use_color):
|
||||||
if cpu <= 25:
|
if cpu <= 25 or not use_color:
|
||||||
return f"{cpu:6.2f}% {name[:25]:<25} {user[:15]:<15}"
|
return f"{cpu:6.2f}% {name[:25]:<25} {user[:15]:<15}"
|
||||||
else:
|
else:
|
||||||
|
YELLOW = "\033[33m"
|
||||||
|
ORANGE = "\033[38;5;214m"
|
||||||
|
RED = "\033[31m"
|
||||||
|
RESET_TO_GREEN = "\033[32m"
|
||||||
color = YELLOW if cpu <= 50 else ORANGE if cpu <= 75 else RED
|
color = YELLOW if cpu <= 50 else ORANGE if cpu <= 75 else RED
|
||||||
cpu_str = f"{cpu:6.2f}%"
|
cpu_str = f"{cpu:6.2f}%"
|
||||||
return f"{color}{cpu_str} {name[:25]:<25} {user[:15]:<15}{RESET_TO_GREEN}"
|
return f"{color}{cpu_str} {name[:25]:<25} {user[:15]:<15}{RESET_TO_GREEN}"
|
||||||
|
|
||||||
def print_processes(limit, show_quit_hint=True):
|
def print_processes(limit, show_quit_hint=True, use_color=True):
|
||||||
clear_screen()
|
clear_screen()
|
||||||
top_processes = get_top_processes(limit)
|
top_processes = get_top_processes(limit)
|
||||||
|
|
||||||
print(GREEN, end='')
|
GREEN = "\033[32m" if use_color else ""
|
||||||
|
|
||||||
|
if use_color:
|
||||||
|
print(GREEN, end='')
|
||||||
|
|
||||||
print(" CPU Process User")
|
print(" CPU Process User")
|
||||||
print("----------------------------------------------")
|
print("----------------------------------------------")
|
||||||
|
|
||||||
for (cpu, name, user) in top_processes:
|
for (cpu, name, user) in top_processes:
|
||||||
print(colorize(cpu, name, user))
|
print(colorize(cpu, name, user, use_color))
|
||||||
|
|
||||||
if show_quit_hint:
|
if show_quit_hint:
|
||||||
print("\nPress 'q' to quit.")
|
print("\nPress 'q' to quit.")
|
||||||
@@ -81,10 +92,13 @@ def main():
|
|||||||
parser = argparse.ArgumentParser(description="Monitor system processes by CPU usage.")
|
parser = argparse.ArgumentParser(description="Monitor system processes by CPU usage.")
|
||||||
parser.add_argument("-n", "--number", type=int, default=10, help="Number of processes to monitor (default is 10).")
|
parser.add_argument("-n", "--number", type=int, default=10, help="Number of processes to monitor (default is 10).")
|
||||||
parser.add_argument("-i", "--interval", type=int, default=1, help="Refresh interval in seconds (default is 1).")
|
parser.add_argument("-i", "--interval", type=int, default=1, help="Refresh interval in seconds (default is 1).")
|
||||||
|
parser.add_argument("--no-color", action="store_true", help="Disable colored output.")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
limit = args.number
|
limit = args.number
|
||||||
interval = max(1, args.interval)
|
interval = max(1, args.interval)
|
||||||
|
|
||||||
|
use_color = not args.no_color and supports_color()
|
||||||
|
|
||||||
refresh_count = 0
|
refresh_count = 0
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
@@ -93,9 +107,10 @@ def main():
|
|||||||
refresh_count += 1
|
refresh_count += 1
|
||||||
elapsed = time.time() - start_time
|
elapsed = time.time() - start_time
|
||||||
show_quit = refresh_count < 3 and elapsed < 3
|
show_quit = refresh_count < 3 and elapsed < 3
|
||||||
print_processes(limit, show_quit_hint=show_quit)
|
print_processes(limit, show_quit_hint=show_quit, use_color=use_color)
|
||||||
key = wait_for_keypress(timeout=interval)
|
key = wait_for_keypress(timeout=interval)
|
||||||
if key and key.lower() == 'q':
|
if key and key.lower() == 'q':
|
||||||
|
print("Exiting...")
|
||||||
break
|
break
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\nProgram interrupted. Exiting...")
|
print("\nProgram interrupted. Exiting...")
|
||||||
|
|||||||
Reference in New Issue
Block a user