mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-14 18:02:22 -07:00
1a2f83f123
And make `core:terminal` use it so that `core:log` can be imported with `-default-to-nil-allocator`, in which the actual allocator is set up in `main()`. Windows was tricky because of the utf-8 <> utf-16 conversion, so we use some temporary stack buffers for that purpose, limiting the non-allocating version there to 512 utf-16 characters each for the key and environment value. In general the value is (obviously) limited to the size of the supplied buffer, and a `.Buffer_Full` error is returned if that buffer is insufficient. If the key is not found, the procedure returns `.Env_Var_Not_Found`. TODO: - Factor out buffer-backed utf8 + utf16 conversion to `core:sys/util` to more easily apply this pattern. - Add similar `lookup_env` and `get_env` procedures to `core:os/os2`. Fixes #5336
87 lines
2.3 KiB
Odin
87 lines
2.3 KiB
Odin
#+private
|
|
package terminal
|
|
|
|
import "core:os"
|
|
import "core:strings"
|
|
|
|
// Reference documentation:
|
|
//
|
|
// - [[ https://no-color.org/ ]]
|
|
// - [[ https://github.com/termstandard/colors ]]
|
|
// - [[ https://invisible-island.net/ncurses/terminfo.src.html ]]
|
|
|
|
get_no_color :: proc() -> bool {
|
|
buf: [128]u8
|
|
if no_color, err := os.lookup_env(buf[:], "NO_COLOR"); err == nil {
|
|
return no_color != ""
|
|
}
|
|
return false
|
|
}
|
|
|
|
get_environment_color :: proc() -> Color_Depth {
|
|
buf: [128]u8
|
|
// `COLORTERM` is non-standard but widespread and unambiguous.
|
|
if colorterm, err := os.lookup_env(buf[:], "COLORTERM"); err == nil {
|
|
// 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, err := os.lookup_env(buf[:], "TERM"); err == nil {
|
|
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
|
|
}
|
|
|
|
@(init)
|
|
init_terminal :: proc() {
|
|
_init_terminal()
|
|
|
|
// We respect `NO_COLOR` specifically as a color-disabler but not as a
|
|
// blanket ban on any terminal manipulation codes, hence why this comes
|
|
// after `_init_terminal` which will allow Windows to enable Virtual
|
|
// Terminal Processing for non-color control sequences.
|
|
if !get_no_color() {
|
|
color_enabled = color_depth > .None
|
|
}
|
|
}
|
|
|
|
@(fini)
|
|
fini_terminal :: proc() {
|
|
_fini_terminal()
|
|
}
|