Added more emojis.
This commit is contained in:
@ -772,10 +772,11 @@ class driver(screenDriver):
|
||||
import re
|
||||
|
||||
# Find all application processes (non-bash, non-screen)
|
||||
app_pattern = r'([a-zA-Z0-9_-]+)\((\d+)\)'
|
||||
# Pattern excludes --- prefix from pstree connection lines
|
||||
app_pattern = r'([a-zA-Z0-9_]+)\((\d+)\)'
|
||||
matches = re.findall(app_pattern, pstree_output)
|
||||
|
||||
skip_processes = {'screen', 'bash', 'sh', 'grep', 'ps'}
|
||||
skip_processes = {'screen', 'bash', 'sh', 'grep', 'ps', 'sudo', 'sleep', 'clipboard_sync'}
|
||||
applications = []
|
||||
|
||||
for app_name, pid in matches:
|
||||
@ -788,16 +789,30 @@ class driver(screenDriver):
|
||||
|
||||
if ps_result.returncode == 0:
|
||||
stat = ps_result.stdout.strip()
|
||||
# Look for processes that are active (S+ state or similar)
|
||||
if '+' in stat or 'l' in stat.lower():
|
||||
applications.append((app_name, pid, stat))
|
||||
# Prioritize active processes
|
||||
priority = 0
|
||||
if 'S' in stat or 'R' in stat: # Active processes
|
||||
priority += 10
|
||||
if '+' in stat: # Foreground processes
|
||||
priority += 20
|
||||
if 'l' in stat.lower(): # Locked processes
|
||||
priority += 5
|
||||
|
||||
applications.append((app_name, pid, priority, stat))
|
||||
except:
|
||||
# If we can't check status, still consider it
|
||||
applications.append((app_name, pid, 'unknown'))
|
||||
# If we can't check status, still consider it with low priority
|
||||
applications.append((app_name, pid, 1, 'unknown'))
|
||||
|
||||
# Return the first active application found
|
||||
# Sort by priority and return the highest priority application
|
||||
if applications:
|
||||
return applications[0][0].upper()
|
||||
applications.sort(key=lambda x: (x[2], int(x[1])), reverse=True)
|
||||
best_app = applications[0][0].upper()
|
||||
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
f"parse_active_app_from_pstree found {len(applications)} apps, selected: {best_app}",
|
||||
debug.DebugLevel.INFO
|
||||
)
|
||||
return best_app
|
||||
|
||||
except Exception as e:
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
@ -806,39 +821,95 @@ class driver(screenDriver):
|
||||
return None
|
||||
|
||||
def get_app_from_screen_session(self, tty_num):
|
||||
"""Get current application from screen session on specific TTY"""
|
||||
"""Get current application from active screen window on specific TTY"""
|
||||
try:
|
||||
# Step 1: Find screen process for this TTY
|
||||
screen_tty_pid = self.get_screen_process_for_tty(tty_num)
|
||||
if not screen_tty_pid:
|
||||
return None
|
||||
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
f"Found screen TTY process: {screen_tty_pid} for TTY{tty_num}",
|
||||
debug.DebugLevel.INFO
|
||||
)
|
||||
|
||||
# Step 2: Find session manager process
|
||||
session_pid = self.get_screen_session_process(screen_tty_pid)
|
||||
if not session_pid:
|
||||
return None
|
||||
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
f"Found screen session process: {session_pid}",
|
||||
debug.DebugLevel.INFO
|
||||
)
|
||||
|
||||
# Step 3: Get process tree and find active app
|
||||
result = subprocess.run([
|
||||
'pstree', '-p', session_pid
|
||||
], capture_output=True, text=True, timeout=3)
|
||||
# Find the screen session PID for this TTY
|
||||
ps_result = subprocess.run([
|
||||
'ps', '-eo', 'pid,ppid,comm,tty,args', '--no-headers'
|
||||
], capture_output=True, text=True, timeout=2)
|
||||
|
||||
if result.returncode == 0:
|
||||
if ps_result.returncode == 0:
|
||||
# Find the main screen process for our TTY
|
||||
screen_pid = None
|
||||
for line in ps_result.stdout.split('\n'):
|
||||
if not line.strip():
|
||||
continue
|
||||
parts = line.split(None, 4)
|
||||
if len(parts) >= 4 and parts[2] == 'screen' and f'tty{tty_num}' in parts[3]:
|
||||
screen_pid = parts[0]
|
||||
break
|
||||
|
||||
if not screen_pid:
|
||||
return None
|
||||
|
||||
# Get the session manager screen process (child of TTY screen)
|
||||
session_pid = None
|
||||
for line in ps_result.stdout.split('\n'):
|
||||
if not line.strip():
|
||||
continue
|
||||
parts = line.split(None, 4)
|
||||
if len(parts) >= 3 and parts[1] == screen_pid and parts[2] == 'screen':
|
||||
session_pid = parts[0]
|
||||
break
|
||||
|
||||
if not session_pid:
|
||||
return None
|
||||
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
f"Pstree output: {result.stdout[:200]}...",
|
||||
f"Found screen session PID: {session_pid}",
|
||||
debug.DebugLevel.INFO
|
||||
)
|
||||
return self.parse_active_app_from_pstree(result.stdout)
|
||||
|
||||
# Get all bash processes under this screen session
|
||||
bash_processes = []
|
||||
for line in ps_result.stdout.split('\n'):
|
||||
if not line.strip():
|
||||
continue
|
||||
parts = line.split(None, 4)
|
||||
if len(parts) >= 3 and parts[1] == session_pid and parts[2] == 'bash':
|
||||
bash_processes.append(parts[0])
|
||||
|
||||
# Check each bash for child applications, prioritizing active ones
|
||||
best_app = None
|
||||
best_priority = 0
|
||||
|
||||
for bash_pid in bash_processes:
|
||||
child_result = subprocess.run([
|
||||
'ps', '--ppid', bash_pid, '-o', 'comm,stat', '--no-headers'
|
||||
], capture_output=True, text=True, timeout=1)
|
||||
|
||||
if child_result.returncode == 0:
|
||||
for child_line in child_result.stdout.split('\n'):
|
||||
if not child_line.strip():
|
||||
continue
|
||||
child_parts = child_line.split()
|
||||
if len(child_parts) >= 2:
|
||||
child_comm, child_stat = child_parts[0], child_parts[1]
|
||||
if child_comm.lower() not in ['grep', 'ps', 'bash', 'sh', 'sudo', 'sleep']:
|
||||
# Calculate priority based on process state
|
||||
priority = 0
|
||||
if 'R' in child_stat: # Running
|
||||
priority += 30
|
||||
elif 'S' in child_stat: # Sleeping
|
||||
priority += 20
|
||||
if '+' in child_stat: # Foreground
|
||||
priority += 20
|
||||
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
f"Found child process: {child_comm} ({child_stat}) priority: {priority}",
|
||||
debug.DebugLevel.INFO
|
||||
)
|
||||
|
||||
if priority > best_priority:
|
||||
best_app = child_comm.upper()
|
||||
best_priority = priority
|
||||
|
||||
if best_app:
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
f"Selected best app: {best_app} (priority: {best_priority})",
|
||||
debug.DebugLevel.INFO
|
||||
)
|
||||
return best_app
|
||||
|
||||
except (subprocess.TimeoutExpired, subprocess.CalledProcessError, FileNotFoundError) as e:
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
@ -846,6 +917,62 @@ class driver(screenDriver):
|
||||
)
|
||||
return None
|
||||
|
||||
def parse_latest_active_app_from_pstree(self, pstree_output):
|
||||
"""Parse pstree output to find the most recently active application"""
|
||||
try:
|
||||
import re
|
||||
|
||||
# Find all application processes with PIDs
|
||||
app_pattern = r'([a-zA-Z0-9_]+)\((\d+)\)'
|
||||
matches = re.findall(app_pattern, pstree_output)
|
||||
|
||||
skip_processes = {'screen', 'bash', 'sh', 'grep', 'ps', 'sudo', 'sleep', 'clipboard_sync'}
|
||||
applications = []
|
||||
|
||||
for app_name, pid in matches:
|
||||
if app_name.lower() not in skip_processes:
|
||||
# Check if this process is still active and get its start time
|
||||
try:
|
||||
ps_result = subprocess.run([
|
||||
'ps', '-p', pid, '-o', 'stat,lstart', '--no-headers'
|
||||
], capture_output=True, text=True, timeout=1)
|
||||
|
||||
if ps_result.returncode == 0:
|
||||
stat_info = ps_result.stdout.strip().split(None, 1)
|
||||
if len(stat_info) >= 2:
|
||||
stat = stat_info[0]
|
||||
start_time = stat_info[1]
|
||||
|
||||
# Prioritize processes that are active or have recent activity
|
||||
priority = 0
|
||||
if 'S' in stat or 'R' in stat: # Running or sleeping (active)
|
||||
priority += 10
|
||||
if '+' in stat: # Foreground process
|
||||
priority += 20
|
||||
|
||||
applications.append((app_name, pid, priority, start_time))
|
||||
except:
|
||||
# If we can't get status, still consider it but with low priority
|
||||
applications.append((app_name, pid, 1, 'unknown'))
|
||||
|
||||
# Sort by priority (highest first), then by PID (most recent)
|
||||
if applications:
|
||||
applications.sort(key=lambda x: (x[2], int(x[1])), reverse=True)
|
||||
best_app = applications[0][0].upper()
|
||||
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
f"Found {len(applications)} applications, selected: {best_app}",
|
||||
debug.DebugLevel.INFO
|
||||
)
|
||||
return best_app
|
||||
|
||||
except Exception as e:
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
f"Error parsing latest active app from pstree: {str(e)}",
|
||||
debug.DebugLevel.ERROR
|
||||
)
|
||||
return None
|
||||
|
||||
def get_app_from_tmux_session(self, tty_num):
|
||||
"""Get current application from tmux session on specific TTY"""
|
||||
try:
|
||||
@ -948,28 +1075,26 @@ class driver(screenDriver):
|
||||
)
|
||||
|
||||
def get_app_via_standard_ps(self, curr_screen):
|
||||
"""Original ps-based application detection as fallback"""
|
||||
"""Fallback ps-based application detection for non-screen/tmux environments"""
|
||||
try:
|
||||
apps = (
|
||||
subprocess.Popen(
|
||||
"ps -t tty" + curr_screen + " -o comm,tty,stat",
|
||||
shell=True,
|
||||
stdout=subprocess.PIPE,
|
||||
)
|
||||
.stdout.read()
|
||||
.decode()[:-1]
|
||||
.split("\n")
|
||||
)
|
||||
# Simple TTY-specific detection as fallback
|
||||
result = subprocess.run([
|
||||
'ps', '-t', f'tty{curr_screen}', '-o', 'comm,stat', '--no-headers'
|
||||
], capture_output=True, text=True, timeout=2)
|
||||
|
||||
for i in apps:
|
||||
i = i.upper()
|
||||
i = i.split()
|
||||
if len(i) >= 3:
|
||||
comm, tty, stat = i[0], i[1], i[2]
|
||||
if "+" in stat and comm != "":
|
||||
if comm not in ["GREP", "SH", "PS", "BASH"]:
|
||||
if "TTY" + curr_screen in tty:
|
||||
return comm
|
||||
if result.returncode == 0:
|
||||
for line in result.stdout.split('\n'):
|
||||
if not line.strip():
|
||||
continue
|
||||
parts = line.split()
|
||||
if len(parts) >= 2:
|
||||
comm, stat = parts[0], parts[1]
|
||||
if '+' in stat and comm.upper() not in ['BASH', 'SH', 'SCREEN', 'GREP', 'PS']:
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
f"Fallback detection found: {comm}",
|
||||
debug.DebugLevel.INFO
|
||||
)
|
||||
return comm.upper()
|
||||
except Exception as e:
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
f"Standard ps detection error: {str(e)}", debug.DebugLevel.ERROR
|
||||
|
Reference in New Issue
Block a user