feat: reduce console/term dependencies (#897)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Replace mattn/isatty and containerd/console with golang.org/x/term. This mostly affects Windows. On Windows, unlike Unix, the console (TTY) has different handles for input/output. Using the Console API, we need to enable VT input on the input handle (CONIN) and VT processing on the output handle (CONOUT). Doing so enables processing VT sequences on Windows i.e. ANSI colors, mouse sequences, cursor movements, etc. We already handle enabling VT processing for the program output using Termenv `EnableVirtualTerminalProcessing`. For the input side, we enable VT input right before setting the console to raw. By doing this, we can drop both containerd/console and mattn/isatty.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+52 added−92 removed
- other:+52 added−92 removed
Provenance
- Commit
4624106- Tree
54d5293- Author
Ayman Bagabas <ayman.bagabas@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
b655d7e
10 files changed+52 added−92 removed
modified:examples/go.mod-1 removed
21 unmodified lines22 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect23 github.com/aymanbagabas/go-udiff v0.2.0 // indirect24 github.com/aymerick/douceur v0.2.0 // indirect25 github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 // indirect26- github.com/containerd/console v1.0.4 // indirect26 github.com/dlclark/regexp2 v1.4.0 // indirect27 github.com/dustin/go-humanize v1.0.1 // indirect28 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect29 github.com/gorilla/css v1.0.0 // indirect18 unmodified linesmodified:examples/go.sum-3 removed
19 unmodified lines20 github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 h1:kyT+aGp1z5jwlus3OY0cP6FuT05jYeeExx/4TYxnyrs=21 github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=22 github.com/charmbracelet/x/exp/teatest v0.0.0-20240229115032-4b79243a3516 h1:7IZFEUZpEgjlTSd7P1MRRhGXs7t4F6mENeMw17TxnQs=23 github.com/charmbracelet/x/exp/teatest v0.0.0-20240229115032-4b79243a3516/go.mod h1:SG24wGkG/mix5V2dZLXfQ6Bod43HGvk9CkTDxATwKN4=24-github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=25-github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=24 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=25 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=26 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=27 github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=56 unmodified lines84 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=85 golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=86 golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=87 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=90-golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=88 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=89 golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=90 golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=91 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=9 unmodified linesmodified:tutorials/go.mod-1 removed
4 unmodified lines5 require github.com/charmbracelet/bubbletea v0.25.06 7 require (8 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect9- github.com/containerd/console v1.0.4 // indirect9 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect10 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect11 github.com/mattn/go-isatty v0.0.20 // indirect12 github.com/mattn/go-localereader v0.0.1 // indirect13 unmodified linesmodified:tutorials/go.sum-3 removed
1 github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=2 github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=3-github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=4-github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=3 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=4 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=5 github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=6 github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=18 unmodified lines25 github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=26 golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=27 golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=28 golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=31-golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=30 golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=31 golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=32 golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=3 unmodified linesmodified:go.mod+1 added-2 removed
1 unmodified line2 3 go 1.184 5 require (6- github.com/containerd/console v1.0.46 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f8- github.com/mattn/go-isatty v0.0.207 github.com/mattn/go-localereader v0.0.18 github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f69 github.com/muesli/cancelreader v0.2.210 github.com/muesli/reflow v0.3.05 unmodified lines16 17 require (18 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect19 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect20+ github.com/mattn/go-isatty v0.0.18 // indirect21 github.com/mattn/go-runewidth v0.0.15 // indirect22 github.com/rivo/uniseg v0.4.6 // indirect23 golang.org/x/text v0.3.8 // indirect24 )modified:go.sum+2 added-5 removed
1 github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=2 github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=3-github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=4-github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=3 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=4 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=5 github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=6 github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=9-github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=10-github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=7+github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=8+github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=9 github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=10 github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=11 github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=12 github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=12 unmodified lines25 github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=26 golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=27 golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=28 golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=31-golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=30 golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=31 golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=32 golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=3 unmodified linesmodified:tea.go+9 added-17 removed
19 unmodified lines20 "sync"21 "sync/atomic"22 "syscall"23 24- "github.com/containerd/console"25- isatty "github.com/mattn/go-isatty"24 "github.com/muesli/cancelreader"25 "github.com/muesli/termenv"26 "golang.org/x/sync/errgroup"27+ "golang.org/x/term"28 )29 30 // ErrProgramKilled is returned by [Program.Run] when the program got killed.31 var ErrProgramKilled = errors.New("program was killed")114 unmodified lines146 restoreOutput func() error147 renderer renderer148 149 // where to read inputs from, this will usually be os.Stdin.151- input io.Reader152- cancelReader cancelreader.CancelReader153- readLoopDone chan struct{}154- console console.Console150+ input io.Reader151+ // tty is null if input is not a TTY.152+ tty *os.File153+ previousTtyState *term.State154+ cancelReader cancelreader.CancelReader155+ readLoopDone chan struct{}156 157 // was the altscreen active before releasing the terminal?158 altScreenWasActive bool159 ignoreSignals uint32160 161 bpWasActive bool // was the bracketed paste mode active before releasing the terminal?162 162- // Stores the original reference to stdin for cases where input is not a163- // TTY on windows and we've automatically opened CONIN$ to receive input.164- // When the program exits this will be restored.165- //166- // Lint ignore note: the linter will find false positive on unix systems167- // as this value only comes into play on Windows, hence the ignore comment168- // below.169- windowsStdin *os.File //nolint:golint,structcheck,unused170-163 filter func(Model, Msg) Msg164 165 // fps is the frames per second we should set on the renderer, if166 // applicable,81 unmodified lines248 // handleResize handles terminal resize events.249 func (p *Program) handleResize() chan struct{} {250 ch := make(chan struct{})251 260- if f, ok := p.output.TTY().(*os.File); ok && isatty.IsTerminal(f.Fd()) {252+ if f, ok := p.output.TTY().(*os.File); ok && term.IsTerminal(int(f.Fd())) {253 // Get the initial terminal size and send it to the program.254 go p.checkResize()255 256 // Listen for window resizes.183 unmodified lines440 f, isFile := p.input.(*os.File)441 if !isFile {442 break443 }452- if isatty.IsTerminal(f.Fd()) {444+ if term.IsTerminal(int(f.Fd())) {445 break446 }447 448 f, err := openInputTTY()275 unmodified linesmodified:tty_unix.go+7 added-20 removed
5 unmodified lines6 import (7 "fmt"8 "os"9 10- "github.com/containerd/console"10+ "golang.org/x/term"11 )12 13-func (p *Program) initInput() error {14- // If input's a file, use console to manage it15- if f, ok := p.input.(*os.File); ok {16- c, err := console.ConsoleFromFile(f)13+func (p *Program) initInput() (err error) {14+ // Check if input is a terminal15+ if f, ok := p.input.(*os.File); ok && term.IsTerminal(int(f.Fd())) {16+ p.tty = f17+ p.previousTtyState, err = term.MakeRaw(int(p.tty.Fd()))18 if err != nil {18- return nil //nolint:nilerr // ignore error, this was just a test19+ return fmt.Errorf("error entering raw mode: %w", err)20 }20- p.console = c21 }22 23 return nil24 }25 26-// On unix systems, RestoreInput closes any TTYs we opened for input. Note that27-// we don't do this on Windows as it causes the prompt to not be drawn until28-// the terminal receives a keypress rather than appearing promptly after the29-// program exits.30-func (p *Program) restoreInput() error {31- if p.console != nil {32- if err := p.console.Reset(); err != nil {33- return fmt.Errorf("error restoring console: %w", err)34- }35- }36- return nil37-}38-26 func openInputTTY() (*os.File, error) {27 f, err := os.Open("/dev/tty")28 if err != nil {29 return nil, fmt.Errorf("could not open a new TTY: %w", err)3 unmodified linesmodified:tty_windows.go+22 added-23 removed
2 unmodified lines3 4 package tea5 6 import (7+ "fmt"8 "os"9 9- "github.com/containerd/console"10+ "golang.org/x/sys/windows"11+ "golang.org/x/term"12 )13 12-func (p *Program) initInput() error {13- // If input's a file, use console to manage it14- if f, ok := p.input.(*os.File); ok {15- // Save a reference to the current stdin then replace stdin with our16- // input. We do this so we can hand input off to containerd/console to17- // set raw mode, and do it in this fashion because the method18- // console.ConsoleFromFile isn't supported on Windows.19- p.windowsStdin = os.Stdin20- os.Stdin = f14+func (p *Program) initInput() (err error) {15+ // Save stdin state and enable VT input16+ // We enable VT processing using Termenv, but we also need to enable VT17+ // input here.18+ if f, ok := p.input.(*os.File); ok && term.IsTerminal(int(f.Fd())) {19+ p.tty = f20+ p.previousTtyState, err = term.MakeRaw(int(p.tty.Fd()))21+ if err != nil {22+ return err23+ }24 22- // Note: this will panic if it fails.23- c := console.Current()24- p.console = c25- }25+ // Enable VT input26+ var mode uint3227+ if err := windows.GetConsoleMode(windows.Handle(p.tty.Fd()), &mode); err != nil {28+ return fmt.Errorf("error getting console mode: %w", err)29+ }30 27- return nil28-}29-30-// restoreInput restores stdout in the event that we placed it aside to handle31-// input with CONIN$, above.32-func (p *Program) restoreInput() error {33- if p.windowsStdin != nil {34- os.Stdin = p.windowsStdin31+ if err := windows.SetConsoleMode(windows.Handle(p.tty.Fd()), mode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err != nil {32+ return fmt.Errorf("error setting console mode: %w", err)33+ }34 }35 37- return nil36+ return37 }38 39 // Open the Windows equivalent of a TTY.40 func openInputTTY() (*os.File, error) {6 unmodified linesmodified:tty.go+11 added-17 removed
5 unmodified lines6 "io"7 "os"8 "time"9 10- isatty "github.com/mattn/go-isatty"10 "github.com/muesli/cancelreader"11 "golang.org/x/term"12 )13 14 func (p *Program) initTerminal() error {16- err := p.initInput()17- if err != nil {15+ if err := p.initInput(); err != nil {16 return err17 }18 21- if p.console != nil {22- err = p.console.SetRaw()23- if err != nil {24- return fmt.Errorf("error entering raw mode: %w", err)25- }26- }27-19 p.renderer.hideCursor()20 return nil21 }22 12 unmodified lines35 time.Sleep(time.Millisecond * 10) //nolint:gomnd36 }37 }38 48- if p.console != nil {49- err := p.console.Reset()50- if err != nil {51- return fmt.Errorf("error restoring terminal state: %w", err)39+ return p.restoreInput()40+}41+42+// restoreInput restores the tty input to its original state.43+func (p *Program) restoreInput() error {44+ if p.tty != nil && p.previousTtyState != nil {45+ if err := term.Restore(int(p.tty.Fd()), p.previousTtyState); err != nil {46+ return fmt.Errorf("error restoring console: %w", err)47 }48 }54-55- return p.restoreInput()49+ return nil50 }51 52 // initCancelReader (re)commences reading inputs.53 func (p *Program) initCancelReader() error {35 unmodified lines89 // checkResize detects the current size of the output and informs the program90 // via a WindowSizeMsg.91 func (p *Program) checkResize() {92 f, ok := p.output.TTY().(*os.File)99- if !ok || !isatty.IsTerminal(f.Fd()) {93+ if !ok || !term.IsTerminal(int(f.Fd())) {94 // can't query window size95 return96 }97 15 unmodified lines