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 for OSC 133 escape sequence. #126

Open
UtkarshVerma opened this issue Mar 17, 2024 · 4 comments
Open

Support for OSC 133 escape sequence. #126

UtkarshVerma opened this issue Mar 17, 2024 · 4 comments

Comments

@UtkarshVerma
Copy link
Contributor

I wanted to use the externalpipe patch define a keymap for copying any command output from the terminal. Still, segregating commands from each other was challenging. After doing a bit of research, I found out about OSC 133 which is the escape sequence modern terminal emulators are using to do the same.

The resources below explain it well:

In a gist, these terminals rely on OSC 133 as markers left by the shell to figure out where a command starts and ends, along with their output.

I tried using the externalpipe patch to match against the OSC to grep out the commands, but noticed that the escape sequences were actually omitted from the terminal contents piped to my script.

What can we do?

  1. We can make changes to the externalpipe patch so that it includes these escape sequences. Additionally, having the eternal version of this patch would also be required to copy long pieces of text.
  2. We introduce a new patch that mimics the functionality implemented in foot. <ctrl+shift+z/x> to move between prompts, and some other binding to copy the terminal output.

I prefer the second approach since we get a new keybinding to move between prompts, which would be a huge time saver. I'm already trying to read the source to introduce these changes but it's proving to be a bit hard to follow, especially after the reflow patch.

Please let me know what you think and an outline of how to do this would be really helpful. I would love to make a PR for this.

@bakkeby
Copy link
Owner

bakkeby commented Mar 17, 2024

I am wondering what is actually stripping the escape sequences in the externalpipe function. Could it be the utf8encode call?

@veltza
Copy link
Contributor

veltza commented Mar 17, 2024

I added preliminary support for the OSC "133;A" sequence to st-sx (osc133 branch). It also implements ctrl+shift+z/x shortcuts, which can be used to scroll to previous command prompts, if the prompts have the OSC "133;A" sequence.

@UtkarshVerma Would you like to try it out and see if it's what you're looking for? It should be easy to add other OSC 133 sequences to it as well, but I don't know how to add support for the externalpipe as I'm not sure how the sequences should behave with it. So, if you want to create that PR, you can port this commit to st-flexipatch and continue from there.

@UtkarshVerma
Copy link
Contributor Author

Thanks @veltza. I have ported it to st-flexipatch and will make a PR. The prompt switching works as expected. I actually gave the command output copying some thought and realised the externalpipe modifications will be better for it because:

  • With externalpipe, if we have access to the entire terminal history, we can display dmenu to let us choose any command we would like to copy.
  • The script can also transform the output according to the user. (e.g., replace sudo with #)

Maybe we could implement something like this:

  • Create a new externalpipe command called externalpipefullraw which would pass the full terminal output history without removing the escape sequences.
  • Let the user match against OSC133 sequences to filter out the commands, and their outputs, similar to Luke's st-copyout script, but without the hardcodes.

@bakkeby I tried looking into it and the utf8encode is the only place where the character validation is happening. So it could be possible it's skipping those bytes.

@veltza
Copy link
Contributor

veltza commented Mar 18, 2024

@UtkarshVerma You need to grab the OSC133 sequences and store them in the glyph attributes. And when you output the terminal buffer(s), you need to check if a glyph has OSC133 attributes and then recreate the appropriate OSC133 sequences for output.

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

3 participants