2021-09-09 20:25:25 +08:00
|
|
|
//go:build !js
|
|
|
|
// +build !js
|
2020-08-01 02:57:48 +08:00
|
|
|
|
|
|
|
package terminal
|
|
|
|
|
|
|
|
import (
|
2020-10-06 23:34:26 +08:00
|
|
|
"fmt"
|
2020-08-01 02:57:48 +08:00
|
|
|
"os"
|
|
|
|
|
2022-03-31 05:13:53 +08:00
|
|
|
"golang.org/x/term"
|
2020-08-01 02:57:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetSize reads the dimensions of the current terminal or returns a
|
|
|
|
// sensible default
|
|
|
|
func GetSize() (w, h int) {
|
2022-03-31 05:13:53 +08:00
|
|
|
w, h, err := term.GetSize(int(os.Stdout.Fd()))
|
2020-08-01 02:57:48 +08:00
|
|
|
if err != nil {
|
|
|
|
w, h = 80, 25
|
|
|
|
}
|
|
|
|
return w, h
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTerminal returns whether the fd passed in is a terminal or not
|
|
|
|
func IsTerminal(fd int) bool {
|
2022-03-31 05:13:53 +08:00
|
|
|
return term.IsTerminal(fd)
|
2020-08-01 02:57:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadPassword reads a line of input from a terminal without local echo. This
|
|
|
|
// is commonly used for inputting passwords and other sensitive data. The slice
|
|
|
|
// returned does not include the \n.
|
|
|
|
func ReadPassword(fd int) ([]byte, error) {
|
2022-03-31 05:13:53 +08:00
|
|
|
return term.ReadPassword(fd)
|
2020-08-01 02:57:48 +08:00
|
|
|
}
|
2020-10-06 23:34:26 +08:00
|
|
|
|
|
|
|
// WriteTerminalTitle writes a string to the terminal title
|
|
|
|
func WriteTerminalTitle(title string) {
|
|
|
|
fmt.Printf(ChangeTitle + title + BEL)
|
|
|
|
}
|