v0.2.0 16 colors, default colors, /dev/tty, bracketed paste, themed form controls

This commit is contained in:
lixen committed 2026-10-09 14:15:46 -04:00
1 parent 070131c7fd
commit 2b5d127e84
32 files changed
+1447 -424

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
// Package env reads what the environment says about a terminal's colors,
// for terminal and inline alike
package env
import (
"os"
"strings"
)
// Colors reports how many colors the environment says the terminal shows:
// 16 on a text console, which wins over COLORTERM since profiles export it
// everywhere; 1<<24 when the terminal announces true color; else 256
func Colors() int {
switch {
case textConsole(os.Getenv("TERM")):
return 16
case trueColor():
return 1 << 24
}
return 256
}
// textConsole reports a TERM limited to 16 colors: the Linux VT, FreeBSD
// syscons, VT100-class terminals (vt and a digit, not vte) and *-16color
func textConsole(term string) bool {
return term == "linux" || term == "ansi" || strings.HasPrefix(term, "cons25") ||
len(term) > 2 && term[:2] == "vt" && term[2] >= '0' && term[2] <= '9' ||
strings.HasSuffix(term, "-16color")
}
// trueColorVars are set by emulators that draw 24-bit color
var trueColorVars = []string{"KITTY_WINDOW_ID", "KONSOLE_VERSION", "ITERM_SESSION_ID",
"ALACRITTY_WINDOW_ID", "ALACRITTY_LOG", "WEZTERM_PANE", "WT_SESSION", "WT_PROFILE_ID"}
func trueColor() bool {
if ct := os.Getenv("COLORTERM"); ct == "truecolor" || ct == "24bit" {
return true
}
for _, v := range trueColorVars {
if os.Getenv(v) != "" {
return true
}
}
term := os.Getenv("TERM")
return strings.Contains(term, "truecolor") || strings.Contains(term, "24bit") || strings.Contains(term, "direct")
}
+26
View File
@@ -0,0 +1,26 @@
package env
import "testing"
// A text console, VT100-class included, gets 16 colors whatever COLORTERM
// says; vte, a modern emulator, is not one
func TestTextConsoleWinsOverColorterm(t *testing.T) {
for _, v := range trueColorVars {
t.Setenv(v, "")
}
t.Setenv("COLORTERM", "truecolor")
for term, want := range map[string]int{
"linux": 16, "cons25": 16, "vt220": 16, "ansi": 16, "xterm-16color": 16,
"vte-256color": 1 << 24, "vt": 1 << 24, "xterm-256color": 1 << 24,
} {
t.Setenv("TERM", term)
if got := Colors(); got != want {
t.Errorf("TERM=%s: %d colors, want %d", term, got, want)
}
}
t.Setenv("COLORTERM", "")
t.Setenv("TERM", "vte-256color")
if got := Colors(); got != 256 {
t.Errorf("vte-256color: %d", got)
}
}