kdheepak / TerminalUserInterfaces.jl

Terminal User Interfaces in Julia.
MIT License
97 stars 5 forks source link

How do you interpret key values? #16

Closed wmanning-cra closed 2 years ago

wmanning-cra commented 2 years ago

I'd like to read the arrow keys, but it seems that they come in as a strange combination of 3 bytes, meaning I have to call take!(t.stdin_channel) 3 times and put the values together. What kind of data can I generally expect to come out of stdin in "raw mode"?

kdheepak commented 2 years ago

Unfortunately yes. You can probably do something like this:

julia> using TerminalUserInterfaces; const TUI = TerminalUserInterfaces
TerminalUserInterfaces

julia> function print_press_on_left_arrow()
       TUI.enable_raw_mode()
       t = TUI.Terminal()
       byte = take!(t.stdin_channel)
       if byte == '\x1B'
           if take!(t.stdin_channel) == '['
          c = take!(t.stdin_channel)
               if c == 'D'
                   println("Pressed left arrow")
               elseif c == 'F'
                   ...
               end
           end
       end
       TUI.disable_raw_mode()
       nothing
       end
print_press_on_left_arrow (generic function with 1 method)

julia> print_press_on_left_arrow()
Pressed left arrow

See https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 for a more comprehensive list and see similar implementation in Rust for "parsing" the event code: https://github.com/crossterm-rs/crossterm/blob/21155716e2eedd5ba8ab97168e5eed7cd50d2ad8/src/event/sys/unix/parse.rs#L26

I would be really interested in implementing the input parsing in Julia in this package and providing the user with Enum values or even just symbols for the input, instead of having users rely on understanding the ascii code themselves.

wmanning-cra commented 2 years ago

Thanks for the quick response!