copy langext & termext from ffsclient

This commit is contained in:
2022-10-27 16:48:26 +02:00
parent 0eaeb5ac4f
commit 3717eeb515
17 changed files with 606 additions and 7 deletions

45
termext/colors.go Normal file
View File

@@ -0,0 +1,45 @@
package termext
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorBlue = "\033[34m"
colorPurple = "\033[35m"
colorCyan = "\033[36m"
colorGray = "\033[37m"
colorWhite = "\033[97m"
)
func Red(v string) string {
return colorRed + v + colorReset
}
func Green(v string) string {
return colorGreen + v + colorReset
}
func Yellow(v string) string {
return colorYellow + v + colorReset
}
func Blue(v string) string {
return colorBlue + v + colorReset
}
func Purple(v string) string {
return colorPurple + v + colorReset
}
func Cyan(v string) string {
return colorCyan + v + colorReset
}
func Gray(v string) string {
return colorGray + v + colorReset
}
func White(v string) string {
return colorWhite + v + colorReset
}

5
termext/osutil.go Normal file
View File

@@ -0,0 +1,5 @@
package termext
func enableColor() bool {
return true
}

24
termext/osutil_windows.go Normal file
View File

@@ -0,0 +1,24 @@
package termext
func enableColor() bool {
handle, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
if err != nil {
return false
}
var mode uint32
err = windows.GetConsoleMode(handle, &mode)
if err != nil {
return false
}
if mode&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING != windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING {
mode = mode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
err = windows.SetConsoleMode(handle, mode)
if err != nil {
return false
}
}
return true
}

90
termext/termcolor.go Normal file
View File

@@ -0,0 +1,90 @@
package termext
import (
"golang.org/x/term"
"os"
"regexp"
"strconv"
"strings"
)
// -> partly copied from [ https://github.com/jwalton/go-supportscolor/tree/master ]
func SupportsColors() bool {
if isatty := term.IsTerminal(int(os.Stdout.Fd())); !isatty {
return false
}
termenv := os.Getenv("TERM")
if termenv == "dumb" {
return false
}
if osColorEnabled := enableColor(); !osColorEnabled {
return false
}
if _, ci := os.LookupEnv("CI"); ci {
var ciEnvNames = []string{"TRAVIS", "CIRCLECI", "APPVEYOR", "GITLAB_CI", "GITHUB_ACTIONS", "BUILDKITE", "DRONE"}
for _, ciEnvName := range ciEnvNames {
_, exists := os.LookupEnv(ciEnvName)
if exists {
return true
}
}
if os.Getenv("CI_NAME") == "codeship" {
return true
}
return false
}
if teamCityVersion, isTeamCity := os.LookupEnv("TEAMCITY_VERSION"); isTeamCity {
versionRegex := regexp.MustCompile(`^(9\.(0*[1-9]\d*)\.|\d{2,}\.)`)
if versionRegex.MatchString(teamCityVersion) {
return true
}
return false
}
if os.Getenv("COLORTERM") == "truecolor" {
return true
}
if termProgram, termProgramPreset := os.LookupEnv("TERM_PROGRAM"); termProgramPreset {
switch termProgram {
case "iTerm.app":
termProgramVersion := strings.Split(os.Getenv("TERM_PROGRAM_VERSION"), ".")
version, err := strconv.ParseInt(termProgramVersion[0], 10, 64)
if err == nil && version >= 3 {
return true
}
return true
case "Apple_Terminal":
return true
default:
// No default
}
}
var term256Regex = regexp.MustCompile("(?i)-256(color)?$")
if term256Regex.MatchString(termenv) {
return true
}
var termBasicRegex = regexp.MustCompile("(?i)^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux")
if termBasicRegex.MatchString(termenv) {
return true
}
if _, colorTerm := os.LookupEnv("COLORTERM"); colorTerm {
return true
}
return false
}