Files
gitea-tea/internal/terminal/color.go
T
bircni ea13c9e4f5 refactor: remove features beyond the UX plan
Drop additions that the UX plan does not ask for:

- the interactive label edit form; label edit now requires a flag
- label clone and the skip mode of label creation it needed
- repo rename rewriting the Git remote or tea.repository; it now
  warns that the remote still points to the old name
- the extra "source" remote on clones of pull mirrors
- run artifact download, which duplicates run download --zip
- 256-color and 24-bit label swatches; colors stay ANSI 16
- config list --show-source (config get keeps it)
- the membership and access GETs before team member/repo add and
  remove; Gitea's PUT and DELETE are idempotent

label edit/delete keep --by-name: it is the only way to address a
label whose name is a number that is also another label's ID.

Signed-off-by: bircni <bircni@icloud.com>
2026-10-12 10:36:50 +02:00

69 lines
2.3 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package terminal
import (
"strings"
"unicode"
)
// ColorEnabled resolves the color preference against terminal and environment policy.
// auto colors TTY stdout unless NO_COLOR is set or TERM is dumb; always forces color; never disables it.
func ColorEnabled(preference string, outTTY bool, getenv func(string) string) bool {
switch preference {
case "always":
return true
case "never":
return false
default: // auto and unknown fall back to terminal detection
if getenv != nil && (getenv("NO_COLOR") != "" || getenv("TERM") == "dumb") {
return false
}
return outTTY
}
}
// HyperlinksEnabled resolves the hyperlinks preference like ColorEnabled, but independently of color:
// NO_COLOR keeps links, and auto links only on TTY stdout whose TERM is not dumb.
func HyperlinksEnabled(preference string, outTTY bool, getenv func(string) string) bool {
switch preference {
case "always":
return true
case "never":
return false
default:
return outTTY && (getenv == nil || getenv("TERM") != "dumb")
}
}
// namedColors are the ANSI 16 colors for states and status, so they follow the terminal's own theme.
var namedColors = map[string]string{"red": "31", "green": "32", "yellow": "33", "blue": "34", "magenta": "35", "cyan": "36", "muted": "90"}
// Colorize colors text by name (red, green, yellow, blue, magenta, cyan or muted) from the terminal's
// ANSI 16 palette. Other colors, such as label hex colors, leave the text plain.
func Colorize(color, text string, enabled bool) string {
code, ok := namedColors[color]
if !enabled || !ok || text == "" {
return text
}
return "\x1b[" + code + "m" + text + "\x1b[0m"
}
// Emphasis applies simple bold styling for TTY human tables.
func Emphasis(text string, enabled bool) string {
if !enabled || text == "" {
return text
}
return "\x1b[1m" + text + "\x1b[0m"
}
// Hyperlink wraps text in an OSC 8 terminal hyperlink to url. URLs with control
// characters are left unlinked so server data cannot inject escape sequences.
func Hyperlink(url, text string, enabled bool) string {
if !enabled || url == "" || text == "" || strings.ContainsFunc(url, unicode.IsControl) {
return text
}
return "\x1b]8;;" + url + "\x1b\\" + text + "\x1b]8;;\x1b\\"
}