Fail gracefully if /dev/tty is not availble (i.e patch for windows users).

This commit is contained in:
Aetnaeus 2020-12-28 18:33:55 -05:00
parent 402160834a
commit 8e3ac3c0a4
3 changed files with 14 additions and 5 deletions

View File

@ -6,6 +6,6 @@ assets:
python3 tools/themegen.py | gofmt > generatedThemes.go
rel:
GOOS=darwin GOARCH=amd64 go build -o bin/tt-osx *.go
GOOS=windows GOARCH=amd64 go build -o bin/tt-windows *.go
GOOS=windows GOARCH=amd64 go build -o bin/tt.exe *.go
GOOS=linux GOARCH=amd64 go build -o bin/tt-linux *.go
GOOS=linux GOARCH=386 go build -o bin/tt-linux_386 *.go

View File

@ -18,6 +18,10 @@ sudo curl -L https://github.com/lemnos/tt/releases/download/0.0.1/tt-linux -o /u
sudo curl -L https://github.com/lemnos/tt/releases/download/0.0.1/tt-osx -o /usr/local/bin/tt && sudo chmod +x /usr/local/bin/tt
```
## Windows
There is a [windows binary](https://github.com/lemnos/tt/releases/download/0.0.1/tt.exe) but it is largely untested. Using WSL is strongly encouraged (or alternatively switching to a proper OS ;)).
## From source
```

View File

@ -2,6 +2,8 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"time"
@ -21,7 +23,7 @@ type typer struct {
OnStart func()
SkipWord bool
ShowWpm bool
tty *os.File
tty io.Writer
currentWordStyle tcell.Style
nextWordStyle tcell.Style
@ -32,14 +34,17 @@ type typer struct {
}
func NewTyper(scr tcell.Screen, fgcol, bgcol, hicol, hicol2, hicol3, errcol tcell.Color) *typer {
var tty io.Writer
def := tcell.StyleDefault.
Foreground(fgcol).
Background(bgcol)
tty, err := os.OpenFile("/dev/tty", os.O_WRONLY, 0)
//Will fail on windows, but tt is still mostly usable via tcell
if err != nil {
panic(err)
tty = ioutil.Discard
}
return &typer{
Scr: scr,
SkipWord: true,
@ -113,11 +118,11 @@ func (t *typer) start(s string, timeLimit time.Duration, startImmediately bool)
text[i].style = t.backgroundStyle
}
t.tty.WriteString("\033[5 q")
defer t.tty.Write([]byte("\033[5 q"))
//Assumes original cursor shape was a block (the one true cursor shape), there doesn't appear to be a
//good way to save/restore the shape if the user has changed it from the otcs.
defer t.tty.WriteString("\033[2 q")
defer t.tty.Write([]byte("\033[2 q"))
t.Scr.SetStyle(t.backgroundStyle)
idx := 0