nsf / termbox-go

Pure Go termbox implementation
http://godoc.org/github.com/nsf/termbox-go
MIT License
4.66k stars 372 forks source link

Understanding character codes / sequences #225

Closed kaihendry closed 3 years ago

kaihendry commented 4 years ago

Using https://raw.githubusercontent.com/nsf/termbox-go/master/_demos/raw_input.go

Ctrl+e I believe is "\005" and it's shown as "\x05" with termbox's raw_input. https://s.natalian.org/2020-05-04/1588559069_2560x1440.png

Similarly down arrow is show as sequence "\033[B" is shown as "\x1bOB". What encoding is that?

What am I missing? Thank you in advance

nsf commented 4 years ago

I believe it just uses Go strings there. The so called %q format in Go. Which is a shorthand for "quoted" (string). Hence anything inside it is a valid Go string (https://golang.org/ref/spec#String_literals).

In your case "\x1bOB" is a byte 0x1B and ASCII literals 'O' and 'B'. Not sure what you're trying to say, but down arrow could be anything (depending on terminal). It might be \033[B" on some, but if termbox demo shows "\x1bOB" it's "\033OB" then. Obviously \033 octal is 0x1B hex. As you mentioned \005 is indeed \x05, because anything below 8 will have the same representation in octal/hex.

kaihendry commented 4 years ago

On Windows https://s.natalian.org/2020-05-12/windows-wsl.mp4 it's a fairly complex sequence that your program interprets as MouseWheel{Up,Down}. image

On MacOS, it's a similar story. https://s.natalian.org/2020-05-12/macos-term.mp4 image

image

So every terminal makes just an arbitary sequence and it interprets it as MouseWheel{Up,Down}?

Just befuddled why these guys chose something common: https://git.suckless.org/scroll/file/config.def.h.html

scrouthtv commented 3 years ago

As you wrote, terminals emit different sequences for actions. There are some standards for the sequences on Mouse wheel. In this part of the code the parsing of those escape sequences happens: https://github.com/nsf/termbox-go/blob/master/termbox.go#L330-L415

From what I can tell, \033 or \x1b or \[ are all the same (1b in hex = 33 in octal = ^[ in ascii). The part afterwards consists of action;x;yDELIMITER where action is just an arbitrary number that someone defined somewhere.

On the topic of st: There was recently a commit that removed those escape sequences. They were added in the first place because vi and less use those exact sequences (^E and ^Y) for scroll, so the scroll wheel is at least supported in those applications. You can read more about it in the commit.