From df5f10d188b80a17eeed48961c4e64c90166f7e6 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Wed, 17 Jul 2024 18:54:29 -0400 Subject: [PATCH] Fix ANSI escapes (on Windows) not being interpreted after using GPG pinentry --- src/backend/2globalsUNIX.go | 6 ++++++ src/backend/2globalsWIN.go | 17 +++++++++++++++++ src/backend/gpg.go | 5 +++++ wiki/MUTN/bugs.md | 1 - wiki/MUTN/quirks.md | 6 ------ wiki/libmutton/bugs.md | 3 +++ wiki/main.md | 4 +--- 7 files changed, 32 insertions(+), 10 deletions(-) delete mode 100644 wiki/MUTN/quirks.md create mode 100644 wiki/libmutton/bugs.md diff --git a/src/backend/2globalsUNIX.go b/src/backend/2globalsUNIX.go index caafb97..6e3bacc 100644 --- a/src/backend/2globalsUNIX.go +++ b/src/backend/2globalsUNIX.go @@ -12,3 +12,9 @@ const ( PathSeparator = "/" IsWindows = false ) + +// enableVirtualTerminalProcessing is a dummy function on UNIX-like systems (only needed on Windows) +// TODO remove after migration off of GPG, as pinentry is responsible for disabling ANSI escape sequence interpretation +func enableVirtualTerminalProcessing() { + return +} diff --git a/src/backend/2globalsWIN.go b/src/backend/2globalsWIN.go index 5b92a53..1da260f 100644 --- a/src/backend/2globalsWIN.go +++ b/src/backend/2globalsWIN.go @@ -2,6 +2,11 @@ package backend +import ( + "os" + "syscall" +) + // EntryRoot path to libmutton entry directory var EntryRoot = Home + "\\AppData\\Local\\libmutton\\entries" var ConfigDir = Home + "\\AppData\\Local\\libmutton\\config" @@ -12,3 +17,15 @@ const ( PathSeparator = "\\" IsWindows = true ) + +// enableVirtualTerminalProcessing ensures ANSI escape sequences are interpreted properly on Windows +// TODO remove after migration off of GPG, as pinentry is responsible for disabling ANSI escape sequence interpretation +func enableVirtualTerminalProcessing() { + stdout := syscall.Handle(os.Stdout.Fd()) + + var originalMode uint32 + syscall.GetConsoleMode(stdout, &originalMode) + originalMode |= 0x0004 + + syscall.MustLoadDLL("kernel32").MustFindProc("SetConsoleMode").Call(uintptr(stdout), uintptr(originalMode)) +} diff --git a/src/backend/gpg.go b/src/backend/gpg.go index 2d22aeb..11351ef 100644 --- a/src/backend/gpg.go +++ b/src/backend/gpg.go @@ -13,11 +13,16 @@ import ( func DecryptGPG(targetLocation string) []string { cmd := exec.Command("gpg", "--pinentry-mode", "loopback", "-q", "-d", targetLocation) output, err := cmd.Output() + + // ensure ANSI escape sequences are interpreted properly on Windows + enableVirtualTerminalProcessing() + if err != nil { fmt.Println(AnsiError + "Failed to decrypt \"" + targetLocation + "\" - ensure it is a valid GPG-encrypted file and that you entered your passphrase correctly" + AnsiReset) os.Exit(1) } outputSlice := strings.Split(string(output), "\n") + return outputSlice } diff --git a/wiki/MUTN/bugs.md b/wiki/MUTN/bugs.md index b09c4b7..d672fdb 100644 --- a/wiki/MUTN/bugs.md +++ b/wiki/MUTN/bugs.md @@ -5,4 +5,3 @@ - [Extra blank lines after nested list items](https://github.com/charmbracelet/glamour/issues/102) - [Incorrect wrapping of text in lists](https://github.com/charmbracelet/glamour/issues/56) - [Excessive binary size and increased program startup time](https://github.com/charmbracelet/glamour/issues/288) -- Some [weird Windows behavior](https://github.com/rwinkhart/MUTN/blob/main/wiki/MUTN/quirks.md) diff --git a/wiki/MUTN/quirks.md b/wiki/MUTN/quirks.md deleted file mode 100644 index ffc3915..0000000 --- a/wiki/MUTN/quirks.md +++ /dev/null @@ -1,6 +0,0 @@ -## Windows Quirks -MUTN for Windows is fully functional, though it does exhibit a couple quirks/bugs not found on other platforms. Watch out for these, and if you have the know-how, pull requests addressing them are welcomed! - -- Reading entries (especially those containing Markdown notes) often results in failure to interpret ANSI escape codes, leading to the raw escape codes being dumped to stdout. This happens seemingly randomly and re-running the exact same command ALWAYS yields the correct output. It's like the terminal has to "warm up" to ASNI escape codes or something. **Help wanted**. -- GPG is sometimes (seems unpredictable) incredibly slow to start on Windows (often after a reboot), leading to many operations seemingly hanging - - **This will be addressed** in the migration off of GPG that will take place before v1.0.0 diff --git a/wiki/libmutton/bugs.md b/wiki/libmutton/bugs.md new file mode 100644 index 0000000..709cac9 --- /dev/null +++ b/wiki/libmutton/bugs.md @@ -0,0 +1,3 @@ +# Known Bugs - libmutton +- On Windows, GPG is sometimes (seems unpredictable) incredibly slow to start (often after a reboot), leading to many operations seemingly hanging + - **This will be addressed** in the migration off of GPG that will take place before v1.0.0 diff --git a/wiki/main.md b/wiki/main.md index c855c08..e4fa713 100644 --- a/wiki/main.md +++ b/wiki/main.md @@ -9,8 +9,6 @@ Crossed out items have not yet been added to the wiki. [~~Usage~~](https://github.com/rwinkhart/MUTN/blob/main/wiki/MUTN/usage.md) -[Windows Quirks](https://github.com/rwinkhart/MUTN/blob/main/wiki/MUTN/quirks.md) - [Shell Completions Troubleshooting](https://github.com/rwinkhart/MUTN/blob/main/wiki/MUTN/completions.md) [Known Bugs](https://github.com/rwinkhart/MUTN/blob/main/wiki/MUTN/bugs.md) @@ -23,4 +21,4 @@ Crossed out items have not yet been added to the wiki. [Developer Guide](https://github.com/rwinkhart/MUTN/blob/main/wiki/libmutton/developers.md) -[~~Known Bugs~~](https://github.com/rwinkhart/MUTN/blob/main/wiki/libmutton/bugs.md) \ No newline at end of file +[Known Bugs](https://github.com/rwinkhart/MUTN/blob/main/wiki/libmutton/bugs.md)