Add term package (back) with clean esc

pull/53/head
rwxrob 2 years ago
parent 4da6efc028
commit dbabeb6132
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -0,0 +1,69 @@
/*
Package esc contains commonly used ANSI terminal escape sequences for
different attributes and terminal control. These are meant to be printed
directly to any terminal supporting the curses standard. They should be
used directly whenever possible for best terminal performance. Also see
the equivalent variables in the term package that are set to empty strings when support for them is not detected at init() time in order to maintain
the original stateful approach to terminal escape sequence support (first designed for the terminfo C library.)
*/
package esc
const (
Reset = "\033[0m"
Bold = "\033[1m"
Faint = "\033[2m"
Italic = "\033[3m"
Underline = "\033[4m"
BlinkSlow = "\033[5m"
BlinkFast = "\033[6m"
Reverse = "\033[7m"
Concealed = "\033[8m"
StrikeOut = "\033[9m"
BoldItalic = "\033[1m\033[3m"
Black = "\033[30m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
BBlack = "\033[40m"
BRed = "\033[41m"
BGreen = "\033[42m"
BYellow = "\033[43m"
BBlue = "\033[44m"
BMagenta = "\033[45m"
BCyan = "\033[46m"
BWhite = "\033[47m"
HBlack = "\033[90m"
HRed = "\033[91m"
HGreen = "\033[92m"
HYellow = "\033[93m"
HBlue = "\033[94m"
HMagenta = "\033[95m"
HCyan = "\033[96m"
HWhite = "\033[97m"
BHBlack = "\033[100m"
BHRed = "\033[101m"
BHGreen = "\033[102m"
BHYellow = "\033[103m"
BHBlue = "\033[104m"
BHMagenta = "\033[105m"
BHCyan = "\033[106m"
BHWhite = "\033[107m"
ClearScreen = "\033[2J\033[H"
X = "\033[0m" // Reset
B = "\033[1m" // Bold
I = "\033[3m" // Italic
U = "\033[4m" // Underline
BI = "\033[1m\033[3m" // BoldItalic
CS = "\033[2J\033[H" // ClearScreen
)

@ -0,0 +1,30 @@
package term
import (
"os"
)
// WinSizeStruct is the exact struct used by the ioctl system library.
type WinSizeStruct struct {
Row, Col uint16
Xpixel, Ypixel uint16
}
// WinSize is 80x24 by default but is detected and set to a more
// accurate value at init() time on systems that support ioctl
// (currently) and can be updated with WinSizeUpdate on systems that
// support it. This value can be overriden by those wishing a more
// consistent value or who prefer not to fill the screen completely when
// displaying help and usage information.
var WinSize WinSizeStruct
// IsTerminal returns true if the output is to an interactive terminal
// (not piped in any way). This is useful when detemining if an extra
// line return is needed to avoid making programs chomp the line returns
// unnecessarily.
func IsTerminal() bool {
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
return true
}
return false
}

@ -0,0 +1,37 @@
package term
import (
"fmt"
"os"
"os/exec"
"testing"
)
// coverage will never catch this test
func TestIsTerminal_false(t *testing.T) {
if os.Getenv("TEST_ISNOTTERM") == "1" {
fmt.Println("out")
if !IsTerminal() {
os.Exit(20)
}
os.Exit(1)
}
exe := os.Args[0]
cmd := exec.Command(exe, "-test.run=TestIsTerminal_false")
cmd.Env = append(os.Environ(), "TEST_ISNOTTERM=1")
cmd.StdoutPipe() // just enough to push into background
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok {
t.Log(e.ExitCode())
if e.ExitCode() != 20 {
t.Errorf("exit %v: still a terminal", e.ExitCode())
}
}
}
func TestIsTerminal_true(t *testing.T) {
if !IsTerminal() {
t.Error("terminal not connected")
}
}

@ -0,0 +1,14 @@
//go:build !aix && !js && !nacl && !plan9 && !windows && !android && !solaris
package term
import (
"syscall"
"unsafe"
)
func WinSizeUpdate() {
syscall.Syscall(syscall.SYS_IOCTL,
uintptr(0), uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(&WinSize)))
}

@ -0,0 +1,7 @@
//go:build aix || js || nacl || plan9 || windows || android || solaris
package term
func WinSizeUpdate() {
WinSize = WinSizeStruct{80, 24, 100, 100}
}
Loading…
Cancel
Save