switch to golang.org/x/term

This commit is contained in:
Michael Eischer 2022-03-28 22:24:15 +02:00
parent c60540b196
commit 61e179ee78
4 changed files with 13 additions and 12 deletions

View File

@ -37,7 +37,7 @@ import (
"os/exec"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)
var version = "0.13.0-dev (compiled manually)"
@ -145,13 +145,13 @@ func checkErrno(err error) error {
}
func stdinIsTerminal() bool {
return terminal.IsTerminal(int(os.Stdin.Fd()))
return term.IsTerminal(int(os.Stdin.Fd()))
}
func stdoutIsTerminal() bool {
// mintty on windows can use pipes which behave like a posix terminal,
// but which are not a terminal handle
return terminal.IsTerminal(int(os.Stdout.Fd())) || stdoutCanUpdateStatus()
return term.IsTerminal(int(os.Stdout.Fd())) || stdoutCanUpdateStatus()
}
func stdoutCanUpdateStatus() bool {
@ -159,7 +159,7 @@ func stdoutCanUpdateStatus() bool {
}
func stdoutTerminalWidth() int {
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
w, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
return 0
}
@ -172,12 +172,12 @@ func stdoutTerminalWidth() int {
// program execution must revert changes to the terminal configuration itself.
// The terminal configuration is only restored while reading a password.
func restoreTerminal() {
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
if !term.IsTerminal(int(os.Stdout.Fd())) {
return
}
fd := int(os.Stdout.Fd())
state, err := terminal.GetState(fd)
state, err := term.GetState(fd)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to get terminal state: %v\n", err)
return
@ -192,7 +192,7 @@ func restoreTerminal() {
if !isReadingPassword {
return nil
}
err := checkErrno(terminal.Restore(fd, state))
err := checkErrno(term.Restore(fd, state))
if err != nil {
fmt.Fprintf(os.Stderr, "unable to restore terminal state: %v\n", err)
}
@ -322,7 +322,7 @@ func readPassword(in io.Reader) (password string, err error) {
func readPasswordTerminal(in *os.File, out io.Writer, prompt string) (password string, err error) {
fmt.Fprint(out, prompt)
isReadingPassword = true
buf, err := terminal.ReadPassword(int(in.Fd()))
buf, err := term.ReadPassword(int(in.Fd()))
isReadingPassword = false
fmt.Fprintln(out)
if err != nil {

1
go.mod
View File

@ -31,6 +31,7 @@ require (
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1
golang.org/x/text v0.3.6
google.golang.org/api v0.50.0
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637

View File

@ -10,7 +10,7 @@ import (
"strings"
"unicode"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
"golang.org/x/text/width"
)
@ -321,7 +321,7 @@ func (t *Terminal) SetStatus(lines []string) {
var width int
if t.canUpdateStatus {
var err error
width, _, err = terminal.GetSize(int(t.fd))
width, _, err = term.GetSize(int(t.fd))
if err != nil || width <= 0 {
// use 80 columns by default
width = 80

View File

@ -7,7 +7,7 @@ import (
"io"
"os"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)
// clearCurrentLine removes all characters from the current line and resets the
@ -24,7 +24,7 @@ func moveCursorUp(wr io.Writer, fd uintptr) func(io.Writer, uintptr, int) {
// CanUpdateStatus returns true if status lines can be printed, the process
// output is not redirected to a file or pipe.
func CanUpdateStatus(fd uintptr) bool {
if !terminal.IsTerminal(int(fd)) {
if !term.IsTerminal(int(fd)) {
return false
}
term := os.Getenv("TERM")