Skip to content
Myr

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.

authored by Ayman Bagabas · committed by GitHub · parent b655d7e · Browse files · 10 files+52 added−92 removed
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

10 files+52 added−92 removed

modified:examples/go.mod-1 removed
21 unmodified lines
22 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 // indirect
18 unmodified lines
modified:examples/go.sum-3 removed
19 unmodified lines
20github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 h1:kyT+aGp1z5jwlus3OY0cP6FuT05jYeeExx/4TYxnyrs=21github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=22github.com/charmbracelet/x/exp/teatest v0.0.0-20240229115032-4b79243a3516 h1:7IZFEUZpEgjlTSd7P1MRRhGXs7t4F6mENeMw17TxnQs=23github.com/charmbracelet/x/exp/teatest v0.0.0-20240229115032-4b79243a3516/go.mod h1:SG24wGkG/mix5V2dZLXfQ6Bod43HGvk9CkTDxATwKN4=24github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=25github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=24github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=25github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=26github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=27github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
56 unmodified lines
84golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=85golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=86golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=87golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=90golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=88golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=89golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=90golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=91golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
9 unmodified lines
modified:tutorials/go.mod-1 removed
4 unmodified lines
5require github.com/charmbracelet/bubbletea v0.25.067require (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 // indirect
13 unmodified lines
modified:tutorials/go.sum-3 removed
1github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=2github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=3github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=4github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=3github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=4github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=5github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=6github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
18 unmodified lines
25github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=26golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=27golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=28golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=31golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=30golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=31golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=32golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
3 unmodified lines
modified:go.mod+1 added-2 removed
1 unmodified line
23go 1.1845require (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.0
5 unmodified lines
1617require (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
1github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=2github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=3github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=4github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=3github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=4github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=5github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=6github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=9github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=10github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=7github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=8github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=9github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=10github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=11github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=12github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
12 unmodified lines
25github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=26golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=27golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=28golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=31golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=30golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=31golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=32golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
3 unmodified lines
modified:tea.go+9 added-17 removed
19 unmodified lines
20 "sync"21 "sync/atomic"22 "syscall"2324 "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)2930// ErrProgramKilled is returned by [Program.Run] when the program got killed.31var ErrProgramKilled = errors.New("program was killed")
114 unmodified lines
146 restoreOutput func() error147 renderer renderer148149 // 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{}156157 // was the altscreen active before releasing the terminal?158 altScreenWasActive bool159 ignoreSignals uint32160161 bpWasActive bool // was the bracketed paste mode active before releasing the terminal?162162 // 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,unused170163 filter func(Model, Msg) Msg164165 // fps is the frames per second we should set on the renderer, if166 // applicable,
81 unmodified lines
248// handleResize handles terminal resize events.249func (p *Program) handleResize() chan struct{} {250 ch := make(chan struct{})251260 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()255256 // Listen for window resizes.
183 unmodified lines
440 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 }447448 f, err := openInputTTY()
275 unmodified lines
modified:tty_unix.go+7 added-20 removed
5 unmodified lines
6import (7 "fmt"8 "os"910 "github.com/containerd/console"10 "golang.org/x/term"11)1213func (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)13func (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 }2223 return nil24}2526// 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.30func (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}3826func 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 lines
modified:tty_windows.go+22 added-23 removed
2 unmodified lines
34package tea56import (7 "fmt"8 "os"99 "github.com/containerd/console"10 "golang.org/x/sys/windows"11 "golang.org/x/term"12)1312func (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 = f14func (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 }2422 // 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 }3027 return nil28}2930// restoreInput restores stdout in the event that we placed it aside to handle31// input with CONIN$, above.32func (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 }3537 return nil36 return37}3839// Open the Windows equivalent of a TTY.40func openInputTTY() (*os.File, error) {
6 unmodified lines
modified:tty.go+11 added-17 removed
5 unmodified lines
6 "io"7 "os"8 "time"910 isatty "github.com/mattn/go-isatty"10 "github.com/muesli/cancelreader"11 "golang.org/x/term"12)1314func (p *Program) initTerminal() error {16 err := p.initInput()17 if err != nil {15 if err := p.initInput(); err != nil {16 return err17 }1821 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 }2719 p.renderer.hideCursor()20 return nil21}22
12 unmodified lines
35 time.Sleep(time.Millisecond * 10) //nolint:gomnd36 }37 }3848 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}4142// restoreInput restores the tty input to its original state.43func (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 }5455 return p.restoreInput()49 return nil50}5152// initCancelReader (re)commences reading inputs.53func (p *Program) initCancelReader() error {
35 unmodified lines
89// checkResize detects the current size of the output and informs the program90// via a WindowSizeMsg.91func (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