Remove code copy-pasted from x/crypto/ssh/terminal

This commit is contained in:
greatroar 2020-02-29 15:56:26 +01:00
parent 863ba76494
commit c8a672fa29
3 changed files with 7 additions and 28 deletions

View file

@ -8,6 +8,8 @@ import (
"io" "io"
"os" "os"
"strings" "strings"
"golang.org/x/crypto/ssh/terminal"
) )
// Terminal is used to write messages and display status lines which can be // Terminal is used to write messages and display status lines which can be
@ -310,7 +312,7 @@ func (t *Terminal) SetStatus(lines []string) {
return return
} }
width, _, err := getTermSize(t.fd) width, _, err := terminal.GetSize(int(t.fd))
if err != nil || width <= 0 { if err != nil || width <= 0 {
// use 80 columns by default // use 80 columns by default
width = 80 width = 80

View file

@ -7,7 +7,6 @@ import (
"os" "os"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/unix"
) )
// clearCurrentLine removes all characters from the current line and resets the // 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 // canUpdateStatus returns true if status lines can be printed, the process
// output is not redirected to a file or pipe. // output is not redirected to a file or pipe.
func canUpdateStatus(fd uintptr) bool { func canUpdateStatus(fd uintptr) bool {
if !terminal.IsTerminal(fd) { if !terminal.IsTerminal(int(fd)) {
return false return false
} }
term := os.Getenv("TERM") 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 // TODO actually read termcap db and detect if terminal supports what we need
return term != "dumb" 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
}

View file

@ -6,6 +6,8 @@ import (
"io" "io"
"syscall" "syscall"
"unsafe" "unsafe"
"golang.org/x/crypto/ssh/terminal"
) )
// clearCurrentLine removes all characters from the current line and resets the // clearCurrentLine removes all characters from the current line and resets the
@ -50,7 +52,6 @@ var (
procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition") procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW") procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute") procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
procGetFileType = kernel32.NewProc("GetFileType") 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)))) 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). // isWindowsTerminal return true if the file descriptor is a windows terminal (cmd, psh).
func isWindowsTerminal(fd uintptr) bool { func isWindowsTerminal(fd uintptr) bool {
var st uint32 return terminal.IsTerminal(int(fd))
r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fd, uintptr(unsafe.Pointer(&st)), 0)
return r != 0 && e == 0
} }
const fileTypePipe = 0x0003 const fileTypePipe = 0x0003