Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support in-terminal application detection #12

Open
TroyMitchell911 opened this issue Sep 29, 2024 · 8 comments
Open

Support in-terminal application detection #12

TroyMitchell911 opened this issue Sep 29, 2024 · 8 comments

Comments

@TroyMitchell911
Copy link

Hi.
Can you add detection of foreground applications in the terminal? When I use this tool, I can only detect Gnome-shell. I think it should also list the foreground applications in the shell, which is a reasonable feature. It can get rid of long shortcut keys in vim and more app.

If I knew ruby, I would definitely submit a PR...

@iberianpig
Copy link
Owner

It seems difficult because it is necessary to handle the execution of Shell commands.

  • Detecting foreground apps in a terminal is complex since running terminal processes aren't recognized like GUI windows.
  • Shell commands run within the shell, not as separate apps, making detection harder.

Please reconsider the necessity of this feature; clarifying the specific use case might make its value clearer.

@TroyMitchell911
Copy link
Author

In vim or tmux, we can use the touchpad to switch windows

@TroyMitchell911
Copy link
Author

Sorry, Idk if it's diffcult. but how fg works? I think it's similar to the principle of fg.

@iberianpig
Copy link
Owner

I don't know. However the behavior of fg and bg is dependent on the shell implementation.

Would it be possible to create a script that retrieves the foreground process using any commands?

@TroyMitchell911
Copy link
Author

I don't know. However the behavior of fg and bg is dependent on the shell implementation.

Would it be possible to create a script that retrieves the foreground process using any commands?

of course.
I run a vim in a zsh terminal.
it's search child process here:

❯ ps --ppid 76432
    PID TTY          TIME CMD
 184521 pts/0    00:00:01 vim

and it's search a vim process and it's parent process:

#!/bin/sh

VIM_PID=$(pgrep vim)

# parent pid
PPID=$(ps -o ppid= -p $VIM_PID)

ps -p $VIM_PID
ps -p $PPID

@TroyMitchell911
Copy link
Author

If you can get what application is selected, I think you can get it's pid?(sorry I don't know ruby so I didn't check source code)

@TroyMitchell911
Copy link
Author

You can through system call to get these information. e.g. procfs getppid sysctl

@iberianpig
Copy link
Owner

There are a few issues to consider:

  1. Terminal App Issue
    When multiple terminal apps (like gnome-terminal) are open, they may return the same PID. This makes it difficult to determine which gnome-terminal is in the foreground.

  2. Tmux Issue
    If we also support tmux, we need to check each session. In tmux, the foreground process is usually tmux itself, and to find the running applications (like Vim) within it, we must investigate the specific tmux session. This adds complexity to the process hierarchy.

Considering these points, detecting the foreground application can be challenging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants