mirror of
https://github.com/restic/restic.git
synced 2025-12-11 18:47:50 +00:00
termstatus: move cursor handling to terminal package
This commit is contained in:
@@ -69,12 +69,12 @@ func New(wr io.Writer, errWriter io.Writer, disableStatus bool) *Terminal {
|
||||
return t
|
||||
}
|
||||
|
||||
if d, ok := wr.(fder); ok && CanUpdateStatus(d.Fd()) {
|
||||
if d, ok := wr.(fder); ok && terminal.CanUpdateStatus(d.Fd()) {
|
||||
// only use the fancy status code when we're running on a real terminal.
|
||||
t.canUpdateStatus = true
|
||||
t.fd = d.Fd()
|
||||
t.clearCurrentLine = clearCurrentLine(t.fd)
|
||||
t.moveCursorUp = moveCursorUp(t.fd)
|
||||
t.clearCurrentLine = terminal.ClearCurrentLine(t.fd)
|
||||
t.moveCursorUp = terminal.MoveCursorUp(t.fd)
|
||||
}
|
||||
|
||||
return t
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/terminal"
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
@@ -17,16 +18,16 @@ func TestSetStatus(t *testing.T) {
|
||||
|
||||
term.canUpdateStatus = true
|
||||
term.fd = ^uintptr(0)
|
||||
term.clearCurrentLine = posixClearCurrentLine
|
||||
term.moveCursorUp = posixMoveCursorUp
|
||||
term.clearCurrentLine = terminal.PosixClearCurrentLine
|
||||
term.moveCursorUp = terminal.PosixMoveCursorUp
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go term.Run(ctx)
|
||||
|
||||
const (
|
||||
cl = posixControlClearLine
|
||||
home = posixControlMoveCursorHome
|
||||
up = posixControlMoveCursorUp
|
||||
cl = terminal.PosixControlClearLine
|
||||
home = terminal.PosixControlMoveCursorHome
|
||||
up = terminal.PosixControlMoveCursorUp
|
||||
)
|
||||
|
||||
term.SetStatus([]string{"first"})
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package termstatus
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
posixControlMoveCursorHome = "\r"
|
||||
posixControlMoveCursorUp = "\x1b[1A"
|
||||
posixControlClearLine = "\x1b[2K"
|
||||
)
|
||||
|
||||
// posixClearCurrentLine removes all characters from the current line and resets the
|
||||
// cursor position to the first column.
|
||||
func posixClearCurrentLine(wr io.Writer, _ uintptr) {
|
||||
// clear current line
|
||||
_, err := wr.Write([]byte(posixControlMoveCursorHome + posixControlClearLine))
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// posixMoveCursorUp moves the cursor to the line n lines above the current one.
|
||||
func posixMoveCursorUp(wr io.Writer, _ uintptr, n int) {
|
||||
data := []byte(posixControlMoveCursorHome)
|
||||
data = append(data, bytes.Repeat([]byte(posixControlMoveCursorUp), n)...)
|
||||
_, err := wr.Write(data)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "write failed: %v\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package termstatus
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
// clearCurrentLine removes all characters from the current line and resets the
|
||||
// cursor position to the first column.
|
||||
func clearCurrentLine(_ uintptr) func(io.Writer, uintptr) {
|
||||
return posixClearCurrentLine
|
||||
}
|
||||
|
||||
// moveCursorUp moves the cursor to the line n lines above the current one.
|
||||
func moveCursorUp(_ uintptr) func(io.Writer, uintptr, int) {
|
||||
return posixMoveCursorUp
|
||||
}
|
||||
|
||||
// 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 !term.IsTerminal(int(fd)) {
|
||||
return false
|
||||
}
|
||||
term := os.Getenv("TERM")
|
||||
if term == "" {
|
||||
return false
|
||||
}
|
||||
// TODO actually read termcap db and detect if terminal supports what we need
|
||||
return term != "dumb"
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package termstatus
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
// clearCurrentLine removes all characters from the current line and resets the
|
||||
// cursor position to the first column.
|
||||
func clearCurrentLine(fd uintptr) func(io.Writer, uintptr) {
|
||||
// easy case, the terminal is cmd or psh, without redirection
|
||||
if isWindowsTerminal(fd) {
|
||||
return windowsClearCurrentLine
|
||||
}
|
||||
|
||||
// assume we're running in mintty/cygwin
|
||||
return posixClearCurrentLine
|
||||
}
|
||||
|
||||
// moveCursorUp moves the cursor to the line n lines above the current one.
|
||||
func moveCursorUp(fd uintptr) func(io.Writer, uintptr, int) {
|
||||
// easy case, the terminal is cmd or psh, without redirection
|
||||
if isWindowsTerminal(fd) {
|
||||
return windowsMoveCursorUp
|
||||
}
|
||||
|
||||
// assume we're running in mintty/cygwin
|
||||
return posixMoveCursorUp
|
||||
}
|
||||
|
||||
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
|
||||
var (
|
||||
procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
|
||||
procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
|
||||
)
|
||||
|
||||
// windowsClearCurrentLine removes all characters from the current line and
|
||||
// resets the cursor position to the first column.
|
||||
func windowsClearCurrentLine(_ io.Writer, fd uintptr) {
|
||||
var info windows.ConsoleScreenBufferInfo
|
||||
windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info)
|
||||
|
||||
// clear the line
|
||||
cursor := windows.Coord{
|
||||
X: info.Window.Left,
|
||||
Y: info.CursorPosition.Y,
|
||||
}
|
||||
var count, w uint32
|
||||
count = uint32(info.Size.X)
|
||||
procFillConsoleOutputAttribute.Call(fd, uintptr(info.Attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&w)))
|
||||
procFillConsoleOutputCharacter.Call(fd, uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&w)))
|
||||
}
|
||||
|
||||
// windowsMoveCursorUp moves the cursor to the line n lines above the current one.
|
||||
func windowsMoveCursorUp(_ io.Writer, fd uintptr, n int) {
|
||||
var info windows.ConsoleScreenBufferInfo
|
||||
windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info)
|
||||
|
||||
// move cursor up by n lines and to the first column
|
||||
windows.SetConsoleCursorPosition(windows.Handle(fd), windows.Coord{
|
||||
X: 0,
|
||||
Y: info.CursorPosition.Y - int16(n),
|
||||
})
|
||||
}
|
||||
|
||||
// isWindowsTerminal return true if the file descriptor is a windows terminal (cmd, psh).
|
||||
func isWindowsTerminal(fd uintptr) bool {
|
||||
return term.IsTerminal(int(fd))
|
||||
}
|
||||
|
||||
func isPipe(fd uintptr) bool {
|
||||
typ, err := windows.GetFileType(windows.Handle(fd))
|
||||
return err == nil && typ == windows.FILE_TYPE_PIPE
|
||||
}
|
||||
|
||||
func getFileNameByHandle(fd uintptr) (string, error) {
|
||||
type FILE_NAME_INFO struct {
|
||||
FileNameLength int32
|
||||
FileName [windows.MAX_LONG_PATH]uint16
|
||||
}
|
||||
|
||||
var fi FILE_NAME_INFO
|
||||
err := windows.GetFileInformationByHandleEx(windows.Handle(fd), windows.FileNameInfo, (*byte)(unsafe.Pointer(&fi)), uint32(unsafe.Sizeof(fi)))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
filename := syscall.UTF16ToString(fi.FileName[:])
|
||||
return filename, nil
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// easy case, the terminal is cmd or psh, without redirection
|
||||
if isWindowsTerminal(fd) {
|
||||
return true
|
||||
}
|
||||
|
||||
// pipes require special handling
|
||||
if !isPipe(fd) {
|
||||
return false
|
||||
}
|
||||
|
||||
fn, err := getFileNameByHandle(fd)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// inspired by https://github.com/RyanGlScott/mintty/blob/master/src/System/Console/MinTTY/Win32.hsc
|
||||
// terminal: \msys-dd50a72ab4668b33-pty0-to-master
|
||||
// pipe to cat: \msys-dd50a72ab4668b33-13244-pipe-0x16
|
||||
if (strings.HasPrefix(fn, "\\cygwin-") || strings.HasPrefix(fn, "\\msys-")) &&
|
||||
strings.Contains(fn, "-pty") && strings.HasSuffix(fn, "-master") {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package termstatus
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
func TestIsMinTTY(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
path string
|
||||
result bool
|
||||
}{
|
||||
{`\\.\pipe\msys-dd50a72ab4668b33-pty0-to-master`, true},
|
||||
{`\\.\pipe\msys-dd50a72ab4668b33-13244-pipe-0x16`, false},
|
||||
} {
|
||||
filename, err := syscall.UTF16FromString(test.path)
|
||||
rtest.OK(t, err)
|
||||
handle, err := windows.CreateNamedPipe(&filename[0], windows.PIPE_ACCESS_DUPLEX,
|
||||
windows.PIPE_TYPE_BYTE, 1, 1024, 1024, 0, nil)
|
||||
rtest.OK(t, err)
|
||||
defer windows.CloseHandle(handle)
|
||||
|
||||
rtest.Assert(t, CanUpdateStatus(uintptr(handle)) == test.result,
|
||||
"expected CanUpdateStatus(%v) == %v", test.path, test.result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user