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

Command history with arrow keys on Windows #630

Closed
kmatt opened this issue Jun 23, 2023 Discussed in #629 · 13 comments · Fixed by #634
Closed

Command history with arrow keys on Windows #630

kmatt opened this issue Jun 23, 2023 Discussed in #629 · 13 comments · Fixed by #634
Labels
bug Unexpected behavior

Comments

@kmatt
Copy link

kmatt commented Jun 23, 2023

Discussed in #629

murex v4.2.5110

Originally posted by kmatt June 22, 2023
Compared to Murex on macOS, up arrow on Win10 does not retrieve command history, in either Windows Terminal or Alacritty.

Is this configurable, or an issue with the readline implementation?

@kmatt
Copy link
Author

kmatt commented Jun 23, 2023

Left and right arrows also don't select auto-complete options, only the left most selection is available:

murex » vim ~/.mur

 ~/.murex_history   ~/.murex_modules/  ~/.murex_preload   ~/.murex_profile

@lmorg
Copy link
Owner

lmorg commented Jun 24, 2023

You can use tab to move selections too, but you're right that the arrow keys should work here as well.

This behaviour isn't (yet) configurable but it should just work out of the box. I'll take a look this weekend.

@lmorg
Copy link
Owner

lmorg commented Jun 27, 2023

This is proving more difficult than I hoped. For reasons I haven't yet figured out, Windows Terminal isn't sending any escape codes when the arrow keys are pressed.

I'm assuming it is because I'm not setting the terminal mode correctly. But I've not gotten much further in my investigation thus far

@lmorg lmorg added bug Unexpected behavior in progress Issue is currently being worked on (possibly in a feature branch) labels Jun 27, 2023
@kmatt
Copy link
Author

kmatt commented Jun 27, 2023

Note I'm seeing the same issue with both Windows Terminal and Alacritty 0.12.1 on Windows, so this may be a Windows thing.

@lmorg
Copy link
Owner

lmorg commented Jun 28, 2023

It's definitely a Windows thing. The problem is Windows doesn't have TTYs like Linux/UNIX. In fairness, TTYs themselves introduce so many issues so I'm not trying to suggest TTYs are better. But the problem here is I need to write some radically different code to compensate for the different console backend.

I've been having a read through some other code out there, like termbox, and it's given me some ideas.

@kmatt
Copy link
Author

kmatt commented Jun 28, 2023

Borrowing from https://github.com/containerd/console/blob/65eb8c0396d0cac15c888bcf4d47049c21317b18/console_windows.go#L191

https://github.com/kmatt/murex/commit/98aef426d94c0fc68df48bce2f486fe6d0afabef

Seems to enable arrow keys history in a build of 4.3. I did not make a PR because I don't know if this is sufficient.

diff --git a/utils/readline/raw_windows.go b/utils/readline/raw_windows.go
index e2ee386b..188f55e9 100644
--- a/utils/readline/raw_windows.go
+++ b/utils/readline/raw_windows.go
@@ -41,6 +41,9 @@ func MakeRaw(fd int) (*State, error) {
                return nil, err
        }
        raw := st &^ (windows.ENABLE_ECHO_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT)
+
+       raw |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
+
        if err := windows.SetConsoleMode(windows.Handle(fd), raw); err != nil {
                return nil, err
        }

@lmorg
Copy link
Owner

lmorg commented Jun 29, 2023

Awesome, thank you. I’ll give that a try.

Can I ask a favour, do you mind writing up some instructions on how to install the additional tooling to compile from source on Windows? At the moment I’m just cross compiling from Linux myself so it would help with my own testing let alone others coming to Murex from Windows.

@kmatt
Copy link
Author

kmatt commented Jun 29, 2023

@lmorg thankfully it was only ensuring that a gcc was in the path, as per the docs of go-sqlite3:

https://github.com/kmatt/murex/blob/v4.3/INSTALL-Windows.md

@lmorg
Copy link
Owner

lmorg commented Jun 29, 2023

How about the other coreutils stuff? Was that just MinGW?

@kmatt
Copy link
Author

kmatt commented Jun 30, 2023

Probably all from Git for Windows, as I don't have a MinGW installed directly:

  • C:\Program Files\Git\mingw64\bin
  • C:\Program Files\Git\usr\bin

Since AFAIK there is not an equivalent to Python's virtualenv for Go, I'm spinning up a clean Windows 11 VM to see what would be needed when not having existing development tools in place.

After the 21.5 GB image downloads ...

@kmatt
Copy link
Author

kmatt commented Jun 30, 2023

Its now building on a Win11 VM without Git or MinGW or a GCC being installed. I was certain a gcc was required for go-sqlite3 previously.

Would building on Windows without any dependencies installed other than Go be expected?

@lmorg
Copy link
Owner

lmorg commented Jun 30, 2023

honestly, I was sure gcc was required for sqlite3 too. Also you'd need git to import dependencies as part of the go build (go modules use git under the hood).

Is it possible that Win11 image has git and gcc already installed?

@lmorg
Copy link
Owner

lmorg commented Jul 2, 2023

Borrowing from https://github.com/containerd/console/blob/65eb8c0396d0cac15c888bcf4d47049c21317b18/console_windows.go#L191

kmatt@98aef42

Seems to enable arrow keys history in a build of 4.3. I did not make a PR because I don't know if this is sufficient.

diff --git a/utils/readline/raw_windows.go b/utils/readline/raw_windows.go
index e2ee386b..188f55e9 100644
--- a/utils/readline/raw_windows.go
+++ b/utils/readline/raw_windows.go
@@ -41,6 +41,9 @@ func MakeRaw(fd int) (*State, error) {
                return nil, err
        }
        raw := st &^ (windows.ENABLE_ECHO_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT)
+
+       raw |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
+
        if err := windows.SetConsoleMode(windows.Handle(fd), raw); err != nil {
                return nil, err
        }

That did exactly the job! Thank you

@lmorg lmorg mentioned this issue Jul 2, 2023
@lmorg lmorg linked a pull request Jul 2, 2023 that will close this issue
@lmorg lmorg closed this as completed in #634 Jul 2, 2023
@lmorg lmorg mentioned this issue Jul 12, 2023
@lmorg lmorg removed the in progress Issue is currently being worked on (possibly in a feature branch) label Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants