Filter as you type feature

This commit is contained in:
MikeTheWatchGuy 2018-10-06 22:42:47 -04:00
parent cbc7b410a7
commit addbf19ff9
1 changed files with 23 additions and 14 deletions

View File

@ -12,11 +12,10 @@ import operator
""" """
Utility to show running processes, CPU usage and provides way to kill processes.
Based on psutil package that is easily installed using pip
""" """
def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True, def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True,
timeout=None, on_terminate=None): timeout=None, on_terminate=None):
"""Kill a process tree (including grandchildren) with signal """Kill a process tree (including grandchildren) with signal
@ -40,21 +39,24 @@ def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True,
def main(): def main():
# ---------------- Create Form ---------------- # ---------------- Create Form ----------------
# sg.ChangeLookAndFeel('Dark') sg.ChangeLookAndFeel('Topanga')
layout = [[sg.Text('Process Killer - Choose one or more processes', layout = [[sg.Text('Process Killer - Choose one or more processes',
size=(45,1), font=('Helvetica', 15))], size=(45,1), font=('Helvetica', 15))],
[sg.Listbox(values=[' '], size=(50, 30), select_mode=sg.SELECT_MODE_EXTENDED, font=('Courier', 12), key='_processes_')], [sg.Listbox(values=[' '], size=(50, 30), select_mode=sg.SELECT_MODE_EXTENDED, font=('Courier', 12), key='_processes_')],
[sg.Text('Click refresh once or twice.. once for list, second to get CPU usage')], [sg.Text('Click refresh once or twice.. once for list, second to get CPU usage')],
[sg.RButton('Refresh'), sg.RButton('Kill', button_color=('white','red')), [sg.T('Filter by typing name', font='ANY 14'), sg.In(size=(15,1), font='any 14', key='_filter_')],
sg.Exit(button_color=('white', 'firebrick4'))]] [sg.RButton('Refresh'),
sg.RButton('Kill', button_color=('white','red')),
sg.Exit(button_color=('white', 'sea green'))]]
window = sg.Window('Process Killer', window = sg.Window('Process Killer',
keep_on_top=True, keep_on_top=True,
auto_size_buttons=False, auto_size_buttons=False,
default_button_element_size=(9,1), default_button_element_size=(9,1),
return_keyboard_events=True,
grab_anywhere=False).Layout(layout) grab_anywhere=False).Layout(layout)
proc_list = []
# ---------------- main loop ---------------- # ---------------- main loop ----------------
while (True): while (True):
# --------- Read and update window -------- # --------- Read and update window --------
@ -71,25 +73,32 @@ def main():
# cpu_percent = psutil.cpu_percent(interval=interval) # if don't wan to use a task # cpu_percent = psutil.cpu_percent(interval=interval) # if don't wan to use a task
display_string = ''
# --------- Create list of top % CPU porocesses -------- # --------- Create list of top % CPU porocesses --------
top_sorted = sorted(top.items(), key=operator.itemgetter(0), reverse=False) top_sorted = sorted(top.items(), key=operator.itemgetter(0), reverse=False)
if top_sorted: if top_sorted:
top_sorted.pop(0) top_sorted.pop(0)
display_string = [] proc_list = []
for proc, pair in top_sorted: for proc, pair in top_sorted:
cpu, pid = pair cpu, pid = pair
display_string.append('{:5d} {:5.2f} {}\n'.format(pid, cpu/10, proc)) proc_list.append('{:5d} {:5.2f} {}\n'.format(pid, cpu/10, proc))
window.FindElement('_processes_').Update(display_string) window.FindElement('_processes_').Update(proc_list)
elif button == 'Kill':
if button == 'Kill':
processes_to_kill = values['_processes_'] processes_to_kill = values['_processes_']
for proc in processes_to_kill: for proc in processes_to_kill:
pid = int(proc[0:5]) pid = int(proc[0:5])
if sg.PopupYesNo('About to kill {} {}'.format(pid, proc[13:]), keep_on_top=True) == 'Yes': if sg.PopupYesNo('About to kill {} {}'.format(pid, proc[13:]), keep_on_top=True) == 'Yes':
kill_proc_tree(pid=pid) kill_proc_tree(pid=pid)
else:
new_output = []
for line in proc_list:
if values['_filter_'] in line.lower():
new_output.append(line)
window.FindElement('_processes_').Update(new_output)
if __name__ == "__main__": if __name__ == "__main__":