From 46053ac19e74c3af189acba60956d31f1cf3271c Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Tue, 14 May 2024 21:36:31 +0200 Subject: [PATCH] special: replace oldest sheppy production code (7 years of service) --- herbstluftwm/hl_start_urxvt_cwd.py | 8 +-- herbstluftwm/start_urxvt_child.py | 79 ------------------------------ 2 files changed, 4 insertions(+), 83 deletions(-) delete mode 100644 herbstluftwm/start_urxvt_child.py diff --git a/herbstluftwm/hl_start_urxvt_cwd.py b/herbstluftwm/hl_start_urxvt_cwd.py index 9211692..fdfa855 100755 --- a/herbstluftwm/hl_start_urxvt_cwd.py +++ b/herbstluftwm/hl_start_urxvt_cwd.py @@ -1,3 +1,5 @@ +#!/usr/bin/python3 + import subprocess import datetime import sys @@ -15,10 +17,8 @@ def get_focused_window(): xdpyinfo = subprocess.check_output('xdpyinfo', universal_newlines=True) for line in xdpyinfo.split(NEWLINE): - print(line) - if line.startswith("focus:"): - print("focus") + if line.startswith("focus:"): # this line will look something like 'focus: window 0x2200009, revert to PointerRoot' # window_id = 0 for field in line.split(WHITESPACE): @@ -39,7 +39,7 @@ def get_window_console_pid(window_id): '''Retrive the PID for a given window_id if the windows contains a console''' x_properties = subprocess.check_output(['xprop','-id', hex(window_id)], universal_newlines=True) - + selected = None pid = -1 diff --git a/herbstluftwm/start_urxvt_child.py b/herbstluftwm/start_urxvt_child.py deleted file mode 100644 index 9211692..0000000 --- a/herbstluftwm/start_urxvt_child.py +++ /dev/null @@ -1,79 +0,0 @@ -import subprocess -import datetime -import sys -import shlex -import psutil - -WHITESPACE = " " -NEWLINE = "\n" -CONSOLE_TYPE = "urxvt" -HEX_BASE = 16 - -def get_focused_window(): - '''Ask the X-Server for the window with focus''' - - xdpyinfo = subprocess.check_output('xdpyinfo', universal_newlines=True) - - for line in xdpyinfo.split(NEWLINE): - print(line) - if line.startswith("focus:"): - print("focus") - - # this line will look something like 'focus: window 0x2200009, revert to PointerRoot' # - window_id = 0 - for field in line.split(WHITESPACE): - if field.startswith("0x"): - window_id = int(field.rstrip(","), HEX_BASE) - break - - if window_id <= 0: - print("No Valid Focused Window Found", file=sys.stderr) - sys.exit(1) - - return window_id - - print("No focus line found in xdpyinfo", file=sys.stderr) - sys.exit(1) - -def get_window_console_pid(window_id): - '''Retrive the PID for a given window_id if the windows contains a console''' - - x_properties = subprocess.check_output(['xprop','-id', hex(window_id)], universal_newlines=True) - - selected = None - pid = -1 - - for line in x_properties.split(NEWLINE): - - if line.startswith("WM_CLASS(STRING)") and CONSOLE_TYPE in line: - # looks like this: WM_CLASS(STRING) = "urxvt", "URxvt # - selected = line - - if line.startswith('_NET_WM_PID(CARDINAL)'): - # looks like this: _NET_WM_PID(CARDINAL) = 91129 # - pid = int(line.split(" = ")[-1]) - - - if not selected or pid <= 0: - print(f"Window does not exist or does not container f{CONSOLE_TYPE}", file=sys.stderr) - sys.exit(1) - - return pid - -def get_path_for_console_pid(pid): - '''Retrieve the currently open path for a given console''' - - process = psutil.Process(pid) - for p in process.children(): #recursive=false - if p.name() in ['zsh','bash']: - return p.cwd() - - -if __name__ == "__main__": - - window_id = get_focused_window() - pid = get_window_console_pid(window_id) - path = get_path_for_console_pid(pid) - - # run the new urxvt # - subprocess.Popen(["urxvt", "-cd", path])