1
1
mirror of https://github.com/walles/moar.git synced 2024-08-16 15:30:34 +03:00

Windows: Color output, character input

Arrow keys don't work.
This commit is contained in:
Johan Walles 2021-04-17 07:03:52 +02:00
parent 9f9dc37594
commit 9b86fc5736
6 changed files with 38 additions and 16 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/moar
/moar.exe
/releases/moar-*

4
go.mod
View File

@ -5,7 +5,9 @@ go 1.16
require (
github.com/alecthomas/chroma v0.8.2
github.com/google/go-cmp v0.5.5 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/objx v0.1.1 // indirect
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
golang.org/x/term v0.0.0-20210317153231-de623e64d2a6
gotest.tools v2.2.0+incompatible

2
go.sum
View File

@ -40,6 +40,8 @@ github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

View File

@ -2,7 +2,12 @@
package twin
import "os"
import (
"os"
"golang.org/x/sys/windows"
"golang.org/x/term"
)
func (screen *UnixScreen) setupSigwinchNotification() {
screen.sigwinch = make(chan int, 1)
@ -12,10 +17,22 @@ func (screen *UnixScreen) setupSigwinchNotification() {
// sigwinch.go for inspiration.
}
func (screen *UnixScreen) setupTtyIn() {
func (screen *UnixScreen) setupTtyInTtyOut() {
// This won't work if we're getting data piped to us, contributions welcome.
screen.ttyIn = os.Stdin
// Set input stream to raw mode
var err error
screen.ttyIn, err = os.Open("CONIN$")
screen.oldTerminalState, err = term.MakeRaw(int(screen.ttyIn.Fd()))
if err != nil {
panic(err)
}
screen.ttyOut = os.Stdout
// Enable console colors, from: https://stackoverflow.com/a/52579002
stdout := windows.Handle(screen.ttyOut.Fd())
var originalMode uint32
windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
}

View File

@ -8,6 +8,7 @@ import (
"syscall"
log "github.com/sirupsen/logrus"
"golang.org/x/term"
)
func (screen *UnixScreen) setupSigwinchNotification() {
@ -42,7 +43,7 @@ func (screen *UnixScreen) setupSigwinchNotification() {
}()
}
func (screen *UnixScreen) setupTtyIn() {
func (screen *UnixScreen) setupTtyInTtyOut() {
// os.Stdout is a stream that goes to our terminal window.
//
// So if we read from there, we'll get input from the terminal window.
@ -52,4 +53,13 @@ func (screen *UnixScreen) setupTtyIn() {
//
// Tested on macOS and Linux, works like a charm!
screen.ttyIn = os.Stdout // <- YES, WE SHOULD ASSIGN STDOUT TO TTYIN
// Set input stream to raw mode
var err error
screen.oldTerminalState, err = term.MakeRaw(int(screen.ttyIn.Fd()))
if err != nil {
panic(err)
}
screen.ttyOut = os.Stdout
}

View File

@ -101,17 +101,7 @@ func NewScreen() (Screen, error) {
screen.events = make(chan Event, 80)
screen.setupSigwinchNotification()
screen.setupTtyIn()
screen.ttyOut = os.Stdout
// Set input stream to raw mode
var err error
screen.oldTerminalState, err = term.MakeRaw(int(screen.ttyIn.Fd()))
if err != nil {
panic(err)
}
screen.setupTtyInTtyOut()
screen.setAlternateScreenMode(true)
screen.enableMouseTracking(true)
screen.hideCursor(true)