Skip to content

Commit

Permalink
refactor(utils/process): have find_deepest_shell_process return None …
Browse files Browse the repository at this point in the history
…if no such process was found
  • Loading branch information
aravinda0 committed Nov 20, 2023
1 parent fa50955 commit 418f69f
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/qtile_bonsai/utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ def modify_terminal_cmd_with_cwd(cmd: str, parent_pid: int) -> str:
provided `parent_pid`.
This isn't a perfect solution, but works well enough in practice.
Looking for the deepest shell process to determine cwd seems to work alright for
Looking for the deepest **shell** process to determine cwd seems to work alright for
now. Just looking for any deepest process fails when something like nvim spawns node
for LSP and that has the home directory as cwd.
We'll refine this over time.
"""
# TODO: Add more terminal emulators. Are there nuances for some terminals that don't
# have a simple switch?
Expand All @@ -26,21 +25,20 @@ def modify_terminal_cmd_with_cwd(cmd: str, parent_pid: int) -> str:
# cmd.

cmd_frags = cmd.split(" ")
program = cmd_frags[0]
if program in terminal_dir_switches:
switch = terminal_dir_switches[program]
terminal_program = cmd_frags[0]
if terminal_program in terminal_dir_switches:
switch = terminal_dir_switches[terminal_program]
if switch not in cmd_frags:
parent_proc = Process(parent_pid)
deepest_proc = find_deepest_shell_process(parent_proc)
return f"{cmd} {switch} {deepest_proc.cwd()}"
cwd_ref_proc = find_deepest_shell_process(parent_proc) or parent_proc
return f"{cmd} {switch} {cwd_ref_proc.cwd()}"

return cmd


def find_deepest_shell_process(process: Process) -> Process:
def find_deepest_shell_process(process: Process) -> Process | None:
"""Returns the first shell process found at the deepest tree levels from the
provided `process` tree. If no such process is found, the provided process is
returned back.
provided `process` tree or None, if no such process exists.
"""
shells = {
"zsh",
Expand All @@ -66,6 +64,6 @@ def _find_deepest_shell_procs(process: Process, level: int = 0) -> list[Process]
_find_deepest_shell_procs(process), key=lambda x: x[1], reverse=True
)
if not shell_procs:
return process
return None

return shell_procs[0][0]

0 comments on commit 418f69f

Please sign in to comment.