Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
env.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
16
17#include "sham/term/env.hpp"
18#include "sham/term/color.hpp"
19#include "sham/term/tty.hpp"
20#include <string_view>
21#include <vector>
22
23namespace {
24
28 static const std::vector<std::string_view> color_support_term{
29 "xterm",
30 "xterm-256",
31 "xterm-256color",
32 "xterm-truecolor",
33 "vt100",
34 "color",
35 "ansi",
36 "cygwin",
37 "linux",
38 "xterm-kitty",
39 "alacritty"};
40
47 bool term_support_color(sham::term::TermEnvVars vars) {
48
49 if (vars.TERM) {
50 for (auto term : color_support_term) {
51 if (*vars.TERM == term) {
52 return true;
53 }
54 }
55 }
56
57 if (vars.COLORTERM) {
58 if (*vars.COLORTERM == "truecolor") {
59 return true;
60 }
61 if (*vars.COLORTERM == "24bit") {
62 return true;
63 }
64 }
65
66 return false;
67 }
68
69} // namespace
70
71namespace sham::term {
72
73 void parse_terminal_support(TermEnvVars vars, const term_parse_callback_t &error_callback) {
74 if (term_support_color(vars)) {
76 } else {
78 }
79
80 bool has_envvar_nocolor = bool(vars.NO_COLOR);
81 bool has_envvar_color = bool(vars.CLICOLOR_FORCE);
82
83 if (has_envvar_color && has_envvar_nocolor) {
84 throw error_callback(
85 "one can not set both NO_COLOR and CLICOLOR_FORCE",
86 std::source_location::current());
87 }
88
89 if (has_envvar_nocolor) {
91 }
92
93 if (has_envvar_color) {
95 }
96
97 auto &res = vars.COLUMN;
98
99 int min_sz = 10;
100 if (res) {
101 try {
102 int val = std::stoi(std::string(*res));
103 if (val < min_sz) {
104 val = min_sz;
105 }
107 } catch (const std::invalid_argument &a) {
108 throw error_callback(
109 "Error : COLUMN is not an integer", std::source_location::current());
110 } catch (const std::out_of_range &a) {
111 throw error_callback(
112 "Error : COLUMN is out of range", std::source_location::current());
113 }
114 }
115 }
116
117} // namespace sham::term
Terminal color escape sequences and enable/disable control.
void enable_colors()
Enable terminal color output.
Definition color.cpp:62
void disable_colors()
Disable all terminal color output.
Definition color.cpp:65
std::function< std::invalid_argument(const char *what, std::source_location where)> term_parse_callback_t
Callback signature for parsing error reporting (returns what, receives source location).
Environment variable parsing for terminal color and size configuration (TERM, COLORTERM,...
void parse_terminal_support(TermEnvVars vars, const term_parse_callback_t &error_callback)
Parses terminal environment variables to determine color support and set terminal size.
Definition env.cpp:73
Holds optional terminal environment variables (TERM, COLORTERM, NO_COLOR, CLICOLOR_FORCE,...
Definition env.hpp:28
This file contains tty info getters.
void set_tty_columns(int columns)
Set the forced width of the terminal.
Definition tty.cpp:43