bakkeby / st-flexipatch

An st build with preprocessor directives to decide which patches to include during build time
MIT License
347 stars 107 forks source link

Support for OSC 133 escape sequence. #126

Open UtkarshVerma opened 5 months ago

UtkarshVerma commented 5 months ago

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 commented 5 months ago

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

veltza commented 5 months ago

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 commented 5 months ago

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:

Maybe we could implement something like this:

@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 commented 5 months ago

@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.