From 4d14af713f08db2dd5f2bd4044469f5c55922d04 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 13 Oct 2020 12:52:30 -0400 Subject: [PATCH] Remove pkg/term dependency --- go.mod | 1 - go.sum | 4 ---- tty.go | 27 +++++++++++++++++++++++++++ tty_unix.go | 32 +++----------------------------- tty_windows.go | 23 ----------------------- 5 files changed, 30 insertions(+), 57 deletions(-) create mode 100644 tty.go diff --git a/go.mod b/go.mod index 0b4bca2..c599b94 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.13 require ( github.com/muesli/termenv v0.7.2 - github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 ) diff --git a/go.sum b/go.sum index 22dd023..9d24899 100644 --- a/go.sum +++ b/go.sum @@ -8,11 +8,7 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/muesli/termenv v0.7.2 h1:r1raklL3uKE7rOvWgSenmEm2px+dnc33OTisZ8YR1fw= github.com/muesli/termenv v0.7.2/go.mod h1:ct2L5N2lmix82RaY3bMWwVu/jUFc9Ule0KGDCiKYPh8= -github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03 h1:pd4YKIqCB0U7O2I4gWHgEUA2mCEOENmco0l/bM957bU= -github.com/pkg/term v0.0.0-20200520122047-c3ffed290a03/go.mod h1:Z9+Ul5bCbBKnbCvdOWbLqTHhJiYV414CURZJba6L8qA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de h1:ikNHVSjEfnvz6sxdSPCaPt572qowuyMDMJLLm3Db3ig= -golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI= golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= diff --git a/tty.go b/tty.go new file mode 100644 index 0000000..faaf5b4 --- /dev/null +++ b/tty.go @@ -0,0 +1,27 @@ +package tea + +import ( + "os" + + "github.com/muesli/termenv" + "golang.org/x/crypto/ssh/terminal" +) + +var origTTYState *terminal.State + +func initTerminal() error { + var err error + origTTYState, err = terminal.MakeRaw(int(os.Stdin.Fd())) // enter raw mode + if err != nil { + return err + } + + enableAnsiColors() + termenv.HideCursor() + return nil +} + +func restoreTerminal() error { + termenv.ShowCursor() + return terminal.Restore(int(os.Stdin.Fd()), origTTYState) // exit raw mode +} diff --git a/tty_unix.go b/tty_unix.go index 8fe0339..f71efeb 100644 --- a/tty_unix.go +++ b/tty_unix.go @@ -2,32 +2,6 @@ package tea -import ( - "github.com/muesli/termenv" - "github.com/pkg/term" -) - -var ( - tty *term.Term -) - -func initTerminal() error { - var err error - tty, err = term.Open("/dev/tty") - if err != nil { - return err - } - - err = tty.SetRaw() - if err != nil { - return err - } - - termenv.HideCursor() - return nil -} - -func restoreTerminal() { - termenv.ShowCursor() - _ = tty.Restore() -} +// enableAnsiColors is only needed for Windows, so for other systems this is +// a no-op. +func enableAnsiColors() {} diff --git a/tty_windows.go b/tty_windows.go index 2963138..b9ddd4b 100644 --- a/tty_windows.go +++ b/tty_windows.go @@ -5,15 +5,9 @@ package tea import ( "os" - "github.com/muesli/termenv" - "golang.org/x/crypto/ssh/terminal" "golang.org/x/sys/windows" ) -var ( - origTTYState *terminal.State -) - // enableAnsiColors enables support for ANSI color sequences in Windows // default console. Note that this only works with Windows 10. func enableAnsiColors() { @@ -23,20 +17,3 @@ func enableAnsiColors() { windows.GetConsoleMode(stdout, &originalMode) windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) } - -func initTerminal() error { - var err error - origTTYState, err = terminal.MakeRaw(int(os.Stdin.Fd())) // enter raw mode - if err != nil { - return err - } - - enableAnsiColors() - termenv.HideCursor() - return nil -} - -func restoreTerminal() { - _ = terminal.Restore(int(os.Stdin.Fd()), origTTYState) // exit raw mode - termenv.ShowCursor() -}