Skip to content

Commit

Permalink
Merge pull request #8 from smallstep/ui-windows
Browse files Browse the repository at this point in the history
Do not set console mode on init
  • Loading branch information
maraino committed Oct 7, 2021
2 parents 5d65eeb + 062bf3f commit e7b2ff9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
12 changes: 12 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ func init() {
readline.Stdout = &stderr{}
}

// Init initializes the terminal to be used by this package. This is generally a
// noop except for windows.
func Init() {
setConsoleMode()
}

// Reset sets the terminal as it was before the initialization. This is
// generally a noop except for windows.
func Reset() {
resetConsoleMode()
}

// Print uses templates to print the arguments formated to os.Stderr.
func Print(args ...interface{}) error {
var o options
Expand Down
8 changes: 8 additions & 0 deletions ui/ui_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !windows
// +build !windows

package ui

func setConsoleMode() {}

func resetConsoleMode() {}
41 changes: 34 additions & 7 deletions ui/ui_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build windows
// +build windows

package ui

import (
Expand All @@ -7,18 +10,42 @@ import (
"golang.org/x/sys/windows"
)

var inMode, outMode uint32

func init() {
var inMode, outMode uint32
if err := windows.GetConsoleMode(windows.Stdin, &inMode); err == nil {
inMode |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
var _ = windows.GetConsoleMode(windows.Stdin, &inMode)
var _ = windows.GetConsoleMode(windows.Stdout, &outMode)
}

func setConsoleMode() {
in := inMode | windows.ENABLE_VIRTUAL_TERMINAL_INPUT
out := outMode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING

if inMode != 0 && inMode != in {
if err := windows.SetConsoleMode(windows.Stdin, in); err != nil {
fmt.Fprintf(os.Stderr, "Failed to set console mode: %v\n", err)
}
}

if outMode != 0 && outMode != out {
if err := windows.SetConsoleMode(windows.Stdout, out); err != nil {
fmt.Fprintf(os.Stderr, "Failed to set console mode: %v\n", err)
}
}
}

func resetConsoleMode() {
in := inMode | windows.ENABLE_VIRTUAL_TERMINAL_INPUT
out := outMode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING

if inMode != 0 && inMode != in {
if err := windows.SetConsoleMode(windows.Stdin, inMode); err != nil {
fmt.Fprintf(os.Stderr, "Failed to set console mode: %v", err)
fmt.Fprintf(os.Stderr, "Failed to reset console mode: %v\n", err)
}
}
if err := windows.GetConsoleMode(windows.Stdout, &outMode); err == nil {
outMode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
if outMode != 0 && outMode != out {
if err := windows.SetConsoleMode(windows.Stdout, outMode); err != nil {
fmt.Fprintf(os.Stderr, "Failed to set console mode: %v", err)
fmt.Fprintf(os.Stderr, "Failed to reset console mode: %v\n", err)
}
}
}

0 comments on commit e7b2ff9

Please sign in to comment.