Restructure core:terminal for better Windows support

This commit is contained in:
Feoramund
2025-05-21 07:49:08 -04:00
parent b6f1821bba
commit e659df1a3f
6 changed files with 150 additions and 117 deletions
+5 -73
View File
@@ -1,7 +1,6 @@
package terminal
import "core:os"
import "core:strings"
/*
This describes the range of colors that a terminal is capable of supporting.
@@ -25,80 +24,13 @@ is_terminal :: proc(handle: os.Handle) -> bool {
return _is_terminal(handle)
}
/*
Get the color depth support for the terminal.
*/
@(require_results)
get_color_depth :: proc() -> Color_Depth {
// Reference documentation:
//
// - [[ https://no-color.org/ ]]
// - [[ https://github.com/termstandard/colors ]]
// - [[ https://invisible-island.net/ncurses/terminfo.src.html ]]
// Respect `NO_COLOR` above all.
if no_color, ok := os.lookup_env("NO_COLOR"); ok {
defer delete(no_color)
if no_color != "" {
return .None
}
}
// `COLORTERM` is non-standard but widespread and unambiguous.
if colorterm, ok := os.lookup_env("COLORTERM"); ok {
defer delete(colorterm)
// These are the only values that are typically advertised that have
// anything to do with color depth.
if colorterm == "truecolor" || colorterm == "24bit" {
return .True_Color
}
}
if term, ok := os.lookup_env("TERM"); ok {
defer delete(term)
if strings.contains(term, "-truecolor") {
return .True_Color
}
if strings.contains(term, "-256color") {
return .Eight_Bit
}
if strings.contains(term, "-16color") {
return .Four_Bit
}
// The `terminfo` database, which is stored in binary on *nix
// platforms, has an undocumented format that is not guaranteed to be
// portable, so beyond this point, we can only make safe assumptions.
//
// This section should only be necessary for terminals that do not
// define any of the previous environment values.
//
// Only a small sampling of some common values are checked here.
switch term {
case "ansi": fallthrough
case "konsole": fallthrough
case "putty": fallthrough
case "rxvt": fallthrough
case "rxvt-color": fallthrough
case "screen": fallthrough
case "st": fallthrough
case "tmux": fallthrough
case "vte": fallthrough
case "xterm": fallthrough
case "xterm-color":
return .Three_Bit
}
}
return .None
}
/*
This is true if the terminal is accepting any form of colored text output.
*/
color_enabled: bool
@(init, private)
init_terminal_status :: proc() {
color_enabled = get_color_depth() > .None
}
/*
This value reports the color depth support as reported by the terminal at the
start of the program.
*/
color_depth: Color_Depth