From c8a672fa2903d560a74c5bd68a4137f2e8d5741b Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Sat, 29 Feb 2020 15:56:26 +0100 Subject: [PATCH] Remove code copy-pasted from x/crypto/ssh/terminal --- internal/ui/termstatus/status.go | 4 +++- internal/ui/termstatus/terminal_unix.go | 13 +------------ internal/ui/termstatus/terminal_windows.go | 18 +++--------------- 3 files changed, 7 insertions(+), 28 deletions(-) diff --git a/internal/ui/termstatus/status.go b/internal/ui/termstatus/status.go index d997d6bb1..b15a881e5 100644 --- a/internal/ui/termstatus/status.go +++ b/internal/ui/termstatus/status.go @@ -8,6 +8,8 @@ import ( "io" "os" "strings" + + "golang.org/x/crypto/ssh/terminal" ) // Terminal is used to write messages and display status lines which can be @@ -310,7 +312,7 @@ func (t *Terminal) SetStatus(lines []string) { return } - width, _, err := getTermSize(t.fd) + width, _, err := terminal.GetSize(int(t.fd)) if err != nil || width <= 0 { // use 80 columns by default width = 80 diff --git a/internal/ui/termstatus/terminal_unix.go b/internal/ui/termstatus/terminal_unix.go index b438aa954..3f0061c01 100644 --- a/internal/ui/termstatus/terminal_unix.go +++ b/internal/ui/termstatus/terminal_unix.go @@ -7,7 +7,6 @@ import ( "os" "golang.org/x/crypto/ssh/terminal" - "golang.org/x/sys/unix" ) // clearCurrentLine removes all characters from the current line and resets the @@ -24,7 +23,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(fd) { + if !terminal.IsTerminal(int(fd)) { return false } term := os.Getenv("TERM") @@ -34,13 +33,3 @@ func canUpdateStatus(fd uintptr) bool { // TODO actually read termcap db and detect if terminal supports what we need return term != "dumb" } - -// getTermSize returns the dimensions of the given terminal. -// the code is taken from "golang.org/x/crypto/ssh/terminal" -func getTermSize(fd uintptr) (width, height int, err error) { - ws, err := unix.IoctlGetWinsize(int(fd), unix.TIOCGWINSZ) - if err != nil { - return -1, -1, err - } - return int(ws.Col), int(ws.Row), nil -} diff --git a/internal/ui/termstatus/terminal_windows.go b/internal/ui/termstatus/terminal_windows.go index 5a46169c9..6ef87bf51 100644 --- a/internal/ui/termstatus/terminal_windows.go +++ b/internal/ui/termstatus/terminal_windows.go @@ -6,6 +6,8 @@ import ( "io" "syscall" "unsafe" + + "golang.org/x/crypto/ssh/terminal" ) // clearCurrentLine removes all characters from the current line and resets the @@ -50,7 +52,6 @@ var ( procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition") procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW") procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute") - procGetConsoleMode = kernel32.NewProc("GetConsoleMode") procGetFileType = kernel32.NewProc("GetFileType") ) @@ -106,22 +107,9 @@ func windowsMoveCursorUp(wr io.Writer, fd uintptr, n int) { procSetConsoleCursorPosition.Call(fd, uintptr(*(*int32)(unsafe.Pointer(&info.cursorPosition)))) } -// getTermSize returns the dimensions of the given terminal. -// the code is taken from "golang.org/x/crypto/ssh/terminal" -func getTermSize(fd uintptr) (width, height int, err error) { - var info consoleScreenBufferInfo - _, _, e := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, fd, uintptr(unsafe.Pointer(&info)), 0) - if e != 0 { - return 0, 0, error(e) - } - return int(info.size.x), int(info.size.y), nil -} - // isWindowsTerminal return true if the file descriptor is a windows terminal (cmd, psh). func isWindowsTerminal(fd uintptr) bool { - var st uint32 - r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fd, uintptr(unsafe.Pointer(&st)), 0) - return r != 0 && e == 0 + return terminal.IsTerminal(int(fd)) } const fileTypePipe = 0x0003