Repository: aristocratos/bpytop
File: bpytop.py (Python)

Code before:
		if found: continue

		if "proc" in Box.boxes:
			if key in ["left", "right"]:
				ProcCollector.sorting(key)
			elif key == " " and CONFIG.proc_tree and ProcBox.selected > 0:
				if ProcBox.selected_pid in ProcCollector.collapsed:
					ProcCollector.collapsed[ProcBox.selected_pid] = not ProcCollector.collapsed[ProcBox.selected_pid]
				Collector.collect(ProcCollector, interrupt=True, redraw=True)
			elif key == "e":
				CONFIG.proc_tree = not CONFIG.proc_tree
				Collector.collect(ProcCollector, interrupt=True, redraw=True)
			elif key == "r":
				CONFIG.proc_reversed = not CONFIG.proc_reversed
				Collector.collect(ProcCollector, interrupt=True, redraw=True)
			elif key == "c":
				CONFIG.proc_per_core = not CONFIG.proc_per_core
				Collector.collect(ProcCollector, interrupt=True, redraw=True)
			elif key == "f":
				ProcBox.filtering = True
				ProcCollector.case_sensitive = False
				if not ProcCollector.search_filter: ProcBox.start = 0
				Collector.collect(ProcCollector, redraw=True, only_draw=True)
			elif key == "F":
				ProcBox.filtering = True
				ProcCollector.case_sensitive = True
				if not ProcCollector.search_filter: ProcBox.start = 0
				Collector.collect(ProcCollector, redraw=True, only_draw=True)
			elif key.lower() in ["t", "k", "i"] and (ProcBox.selected > 0 or ProcCollector.detailed):
				pid: int = ProcBox.selected_pid if ProcBox.selected > 0 else ProcCollector.detailed_pid # type: ignore
				if psutil.pid_exists(pid):
					if key.lower() == "t": sig = signal.SIGTERM
					elif key.lower() == "k": sig = signal.SIGKILL
					elif key.lower() == "i": sig = signal.SIGINT
					try:
						os.kill(pid, sig)
					except Exception as e:
						errlog.error(f'Exception when sending signal {sig} to pid {pid}')
						errlog.exception(f'{e}')
			elif key == "delete" and ProcCollector.search_filter:
				ProcCollector.search_filter = ""
				Collector.collect(ProcCollector, proc_interrupt=True, redraw=True)
			elif key == "enter":
				if ProcBox.selected > 0 and ProcCollector.detailed_pid != ProcBox.selected_pid and psutil.pid_exists(ProcBox.selected_pid):
					ProcCollector.detailed = True
					ProcBox.last_selection = ProcBox.selected
					ProcBox.selected = 0
					ProcCollector.detailed_pid = ProcBox.selected_pid
					ProcBox.resized = True
					Collector.proc_counter = 1
				elif ProcCollector.detailed:

Review comment (security):
This could be a bit more efficient like:
```python
elif key in ["f", "F"]:
	ProcBox.filtering = True
	ProcCollector.case_sensitive = key == "F"
	if not ProcCollector.search_filter: ProcBox.start = 0
	Collector.collect(ProcCollector, redraw=True, only_draw=True)
```

Code after:
				ProcCollector.sorting(key)
			elif key == " " and CONFIG.proc_tree and ProcBox.selected > 0:
				if ProcBox.selected_pid in ProcCollector.collapsed:
					ProcCollector.collapsed[ProcBox.selected_pid] = not ProcCollector.collapsed[ProcBox.selected_pid]
				Collector.collect(ProcCollector, interrupt=True, redraw=True)
			elif key == "e":
				CONFIG.proc_tree = not CONFIG.proc_tree
				Collector.collect(ProcCollector, interrupt=True, redraw=True)
			elif key == "r":
				CONFIG.proc_reversed = not CONFIG.proc_reversed
				Collector.collect(ProcCollector, interrupt=True, redraw=True)
			elif key == "c":
				CONFIG.proc_per_core = not CONFIG.proc_per_core
				Collector.collect(ProcCollector, interrupt=True, redraw=True)
			elif key in ["f", "F"]:
				ProcBox.filtering = True
				ProcCollector.case_sensitive = key == "F"
				if not ProcCollector.search_filter: ProcBox.start = 0
				Collector.collect(ProcCollector, redraw=True, only_draw=True)
			elif key.lower() in ["t", "k", "i"] and (ProcBox.selected > 0 or ProcCollector.detailed):
				pid: int = ProcBox.selected_pid if ProcBox.selected > 0 else ProcCollector.detailed_pid # type: ignore
				if psutil.pid_exists(pid):
					if key.lower() == "t": sig = signal.SIGTERM
					elif key.lower() == "k": sig = signal.SIGKILL
					elif key.lower() == "i": sig = signal.SIGINT
					try:
						os.kill(pid, sig)
					except Exception as e:
						errlog.error(f'Exception when sending signal {sig} to pid {pid}')
						errlog.exception(f'{e}')
			elif key == "delete" and ProcCollector.search_filter:
				ProcCollector.search_filter = ""
				Collector.collect(ProcCollector, proc_interrupt=True, redraw=True)
			elif key == "enter":
				if ProcBox.selected > 0 and ProcCollector.detailed_pid != ProcBox.selected_pid and psutil.pid_exists(ProcBox.selected_pid):
					ProcCollector.detailed = True
					ProcBox.last_selection = ProcBox.selected
					ProcBox.selected = 0
					ProcCollector.detailed_pid = ProcBox.selected_pid
					ProcBox.resized = True
					Collector.proc_counter = 1
				elif ProcCollector.detailed:
					ProcBox.selected = ProcBox.last_selection
					ProcBox.last_selection = 0
					ProcCollector.detailed = False
					ProcCollector.detailed_pid = None
					ProcBox.resized = True
					Collector.proc_counter = 1
				else:
					continue
				ProcCollector.details = {}