internal.odin 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #+private
  2. package terminal
  3. import "core:os"
  4. import "core:strings"
  5. // Reference documentation:
  6. //
  7. // - [[ https://no-color.org/ ]]
  8. // - [[ https://github.com/termstandard/colors ]]
  9. // - [[ https://invisible-island.net/ncurses/terminfo.src.html ]]
  10. get_no_color :: proc() -> bool {
  11. buf: [128]u8
  12. if no_color, err := os.lookup_env(buf[:], "NO_COLOR"); err == nil {
  13. return no_color != ""
  14. }
  15. return false
  16. }
  17. get_environment_color :: proc() -> Color_Depth {
  18. buf: [128]u8
  19. // `COLORTERM` is non-standard but widespread and unambiguous.
  20. if colorterm, err := os.lookup_env(buf[:], "COLORTERM"); err == nil {
  21. // These are the only values that are typically advertised that have
  22. // anything to do with color depth.
  23. if colorterm == "truecolor" || colorterm == "24bit" {
  24. return .True_Color
  25. }
  26. }
  27. if term, err := os.lookup_env(buf[:], "TERM"); err == nil {
  28. if strings.contains(term, "-truecolor") {
  29. return .True_Color
  30. }
  31. if strings.contains(term, "-256color") {
  32. return .Eight_Bit
  33. }
  34. if strings.contains(term, "-16color") {
  35. return .Four_Bit
  36. }
  37. // The `terminfo` database, which is stored in binary on *nix
  38. // platforms, has an undocumented format that is not guaranteed to be
  39. // portable, so beyond this point, we can only make safe assumptions.
  40. //
  41. // This section should only be necessary for terminals that do not
  42. // define any of the previous environment values.
  43. //
  44. // Only a small sampling of some common values are checked here.
  45. switch term {
  46. case "ansi": fallthrough
  47. case "konsole": fallthrough
  48. case "putty": fallthrough
  49. case "rxvt": fallthrough
  50. case "rxvt-color": fallthrough
  51. case "screen": fallthrough
  52. case "st": fallthrough
  53. case "tmux": fallthrough
  54. case "vte": fallthrough
  55. case "xterm": fallthrough
  56. case "xterm-color":
  57. return .Three_Bit
  58. }
  59. }
  60. return .None
  61. }
  62. @(init)
  63. init_terminal :: proc() {
  64. _init_terminal()
  65. // We respect `NO_COLOR` specifically as a color-disabler but not as a
  66. // blanket ban on any terminal manipulation codes, hence why this comes
  67. // after `_init_terminal` which will allow Windows to enable Virtual
  68. // Terminal Processing for non-color control sequences.
  69. if !get_no_color() {
  70. color_enabled = color_depth > .None
  71. }
  72. }
  73. @(fini)
  74. fini_terminal :: proc() {
  75. _fini_terminal()
  76. }