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

Remote control ideas: wait for window to close, get window's stdout/stderr #2440

Closed
maximbaz opened this issue Mar 17, 2020 · 5 comments
Closed

Comments

@maximbaz
Copy link
Contributor

I was playing with remote control to implement a script that would take stdin, run fuzzy filter on it in a new kitty window, and spit out the result. A very generic script, can be used e.g. in text editor if I want to search and open a file.

I was struggling quite a bit with the following:

After I create a new window, I would like to wait until this window is closed, and get back its stdout and stderr to the caller.

Please let me know if I'm being silly and am missing an obvious solution to this, but I didn't find a way to do it with the current remote control API, so I basically ended up with the following hacky solution:

#!/bin/sh

tmp=$(mktemp -d)
trap 'rm -rf $tmp' EXIT

mkdir $tmp/done
cat >$tmp/stdin
kitty @ new-window --no-response --cwd "$PWD" sh -c "cat $tmp/stdin | sk -m --reverse >$tmp/stdout 2>$tmp/stderr; touch $tmp/done/1" >/dev/null 2>&1
inotifywait -q -e create "$tmp/done" >/dev/null

cat $tmp/stdout
cat $tmp/stderr >&2

sk is a fuzzy filter, can be replaced with fzy, fzf and the likes.

If the script is saved as filter, it can be used e.g. as rg --files | filter.

Is it something that you'd be interested to add in the remote control API?

@kovidgoyal
Copy link
Owner

kovidgoyal commented Mar 18, 2020 via email

@maximbaz
Copy link
Contributor Author

Taking screen contents might be a bit problematic if you want to distinguish stdout from stderr, but I can see the challenge you describe.

Speaking of waiting for window to close, does kitty create any sort of socket or temporary file for a new window? Or maybe this could be added?

Suppose I create a window via kitty @ new-window cat and I get 2 returned, where 2 is the ID of the window. If I could know that for example a file /tmp/kitty/2.socket was created and that this file would be removed once the window is closed, I wouldn't need any --on-exit, because I could simply use inotifywait -m -e delete_self /tmp/kitty/2.socket to wait, and then run my commands on exit.

What do you think?

@kovidgoyal
Copy link
Owner

kovidgoyal commented Mar 18, 2020 via email

@maximbaz
Copy link
Contributor Author

What's the difference? With --on-exit you can have inotify wait for the created file instead. And you get access to the output of the command.

Not much difference, only that creation and removal of the watch file becomes a user script's responsibility. But on the other hand, if kitty itself has no use for these files, then there is not much sense in creating such files.

Running inotifywait from --on-exit or outside as I have now (after calling kitty @ create-window) seems more or less identical at least for my use-case 🤔

@kovidgoyal
Copy link
Owner

kovidgoyal commented Mar 18, 2020 via email

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

No branches or pull requests

2 participants