Skip to content

Commit

Permalink
feat: use x/term instead of golang/term
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 9, 2024
1 parent 4a737f7 commit 2e67dbf
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ go 1.18
require (
github.com/charmbracelet/x/ansi v0.1.0
github.com/charmbracelet/x/input v0.1.0
github.com/charmbracelet/x/term v0.1.0
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6
github.com/muesli/cancelreader v0.2.2
golang.org/x/sync v0.7.0
golang.org/x/sys v0.20.0
golang.org/x/term v0.20.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/charmbracelet/x/ansi v0.1.0 h1:o4NbQQCoVgbLpD5RC1cI687baoLwrLZyCOTGlF
github.com/charmbracelet/x/ansi v0.1.0/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=
github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=
github.com/charmbracelet/x/term v0.1.0 h1:yOhOuAKvqAIIrUg2TDHnURdxVXOBQIjMhfgg/avR6j0=
github.com/charmbracelet/x/term v0.1.0/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=
github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=
github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
Expand All @@ -23,5 +25,3 @@ golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
4 changes: 2 additions & 2 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"syscall"

"github.com/charmbracelet/x/input"
"github.com/charmbracelet/x/term"
"golang.org/x/sync/errgroup"
"golang.org/x/term"
)

// ErrProgramKilled is returned by [Program.Run] when the program got killed.
Expand Down Expand Up @@ -444,7 +444,7 @@ func (p *Program) Run() (Model, error) {
if !isFile {
break
}
if term.IsTerminal(int(f.Fd())) {
if term.IsTerminal(f.Fd()) {
break
}

Expand Down
8 changes: 4 additions & 4 deletions tty.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/charmbracelet/x/input"
"github.com/charmbracelet/x/term"
"github.com/muesli/cancelreader"
"golang.org/x/term"
)

func (p *Program) initTerminal() error {
Expand Down Expand Up @@ -44,12 +44,12 @@ func (p *Program) restoreTerminalState() error {
// restoreInput restores the tty input to its original state.
func (p *Program) restoreInput() error {
if p.ttyInput != nil && p.previousTtyInputState != nil {
if err := term.Restore(int(p.ttyInput.Fd()), p.previousTtyInputState); err != nil {
if err := term.Restore(p.ttyInput.Fd(), p.previousTtyInputState); err != nil {
return fmt.Errorf("error restoring console: %w", err)
}
}
if p.ttyOutput != nil && p.previousOutputState != nil {
if err := term.Restore(int(p.ttyOutput.Fd()), p.previousOutputState); err != nil {
if err := term.Restore(p.ttyOutput.Fd(), p.previousOutputState); err != nil {
return fmt.Errorf("error restoring console: %w", err)
}
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func (p *Program) checkResize() {
return
}

w, h, err := term.GetSize(int(p.ttyOutput.Fd()))
w, h, err := term.GetSize(p.ttyOutput.Fd())
if err != nil {
select {
case <-p.ctx.Done():
Expand Down
8 changes: 4 additions & 4 deletions tty_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
"fmt"
"os"

"golang.org/x/term"
"github.com/charmbracelet/x/term"
)

func (p *Program) initInput() (err error) {
// Check if input is a terminal
if f, ok := p.input.(*os.File); ok && term.IsTerminal(int(f.Fd())) {
if f, ok := p.input.(*os.File); ok && term.IsTerminal(f.Fd()) {
p.ttyInput = f
p.previousTtyInputState, err = term.MakeRaw(int(p.ttyInput.Fd()))
p.previousTtyInputState, err = term.MakeRaw(p.ttyInput.Fd())
if err != nil {
return fmt.Errorf("error entering raw mode: %w", err)
}
}

if f, ok := p.output.(*os.File); ok && term.IsTerminal(int(f.Fd())) {
if f, ok := p.output.(*os.File); ok && term.IsTerminal(f.Fd()) {
p.ttyOutput = f
}

Expand Down
10 changes: 5 additions & 5 deletions tty_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
"fmt"
"os"

"github.com/charmbracelet/x/term"
"golang.org/x/sys/windows"
"golang.org/x/term"
)

func (p *Program) initInput() (err error) {
// Save stdin state and enable VT input
// We also need to enable VT
// input here.
if f, ok := p.input.(*os.File); ok && term.IsTerminal(int(f.Fd())) {
if f, ok := p.input.(*os.File); ok && term.IsTerminal(f.Fd()) {
p.ttyInput = f
p.previousTtyInputState, err = term.MakeRaw(int(p.ttyInput.Fd()))
p.previousTtyInputState, err = term.MakeRaw(p.ttyInput.Fd())
if err != nil {
return err
}
Expand All @@ -34,9 +34,9 @@ func (p *Program) initInput() (err error) {
}

// Save output screen buffer state and enable VT processing.
if f, ok := p.output.(*os.File); ok && term.IsTerminal(int(f.Fd())) {
if f, ok := p.output.(*os.File); ok && term.IsTerminal(f.Fd()) {
p.ttyOutput = f
p.previousOutputState, err = term.GetState(int(f.Fd()))
p.previousOutputState, err = term.GetState(f.Fd())
if err != nil {
return err
}
Expand Down

0 comments on commit 2e67dbf

Please sign in to comment.