Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
generic_opts.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
18#include "shambase/print.hpp"
19#include "shambase/string.hpp"
21#include "shamcmdopt/cmdopt.hpp"
23#include "shamcmdopt/env.hpp"
24#include "shamcmdopt/tty.hpp"
25#include <string_view>
26#include <vector>
27
31static const std::vector<std::string_view> color_suport_term{
32 "xterm",
33 "xterm-256",
34 "xterm-256color",
35 "xterm-truecolor",
36 "vt100",
37 "color",
38 "ansi",
39 "cygwin",
40 "linux",
41 "xterm-kitty",
42 "alacritty"};
43
51
52 auto term_var = shamcmdopt::getenv_str("TERM");
53 if (term_var) {
54 for (auto term : color_suport_term) {
55 if (*term_var == term) {
56 return true;
57 }
58 }
59 }
60
61 auto colorterm_var = shamcmdopt::getenv_str("COLORTERM");
62 if (colorterm_var) {
63 if (*colorterm_var == "truecolor") {
64 return true;
65 }
66 if (*colorterm_var == "24bit") {
67 return true;
68 }
69 }
70
71 return false;
72}
73
74namespace shamcmdopt {
75
77
78 register_opt("--nocolor", {}, "disable colored output");
79 register_opt("--color", {}, "force colored output");
80 register_opt("--help", {}, "show this message");
81
82 register_env_var_doc("NO_COLOR", "Disable colors (if no color cli args are passed)");
83 register_env_var_doc("CLICOLOR_FORCE", "Enable colors (if no color cli args are passed)");
84 register_env_var_doc("TERM", "Terminal emulator identifier");
85 register_env_var_doc("COLORTERM", "Terminal color support identifier");
86 register_env_var_doc("SHAMTTYCOL", "Set tty assumed column count");
87 }
88
104
105 bool has_opt_nocolor = has_option("--nocolor");
106 bool has_opt_color = has_option("--color");
107
108 bool has_envvar_nocolor = bool(getenv_str("NO_COLOR"));
109 bool has_envvar_color = bool(getenv_str("CLICOLOR_FORCE"));
110
111 if (has_opt_color && has_opt_nocolor) {
113 "You can not pass --nocolor and --color simultaneously");
114 }
115
116 if (has_opt_nocolor) {
117 shambase::term_colors::disable_colors();
118 } else if (has_opt_color) {
119 shambase::term_colors::enable_colors();
120 } else if (has_envvar_nocolor) {
121 shambase::term_colors::disable_colors();
122 } else if (has_envvar_color) {
123 shambase::term_colors::enable_colors();
124 } else {
125 if (term_support_color() && is_a_tty()) {
126 shambase::term_colors::enable_colors();
127 } else {
128 shambase::term_colors::disable_colors();
129 }
130 }
131 }
132
141 void process_tty() {
142 auto res = getenv_str("SHAMTTYCOL");
143
144 int min_sz = 10;
145 if (res) {
146 try {
147 try {
148 int val = std::stoi(*res);
149 if (val < min_sz) {
150 val = min_sz;
151 }
152 set_tty_columns(val);
153 } catch (const std::invalid_argument &a) {
154 shambase::println("Error : SHAMTTYCOL is not an integer");
155 }
156 } catch (const std::out_of_range &a) {
157 shambase::println("Error : SHAMTTYCOL is out of range");
158 }
159 }
160 }
161
163
165 process_tty();
166
167 if (has_option("--help")) {
168 print_help();
169
170 shambase::println("\nEnv deduced vars :");
171
172 if (is_a_tty()) {
173 shambase::println(" isatty = Yes");
174 } else {
175 shambase::println(" isatty = No");
176 }
177
178 if (shambase::term_colors::colors_enabled()) {
179 shambase::println(" color = enabled");
180 } else {
181 shambase::println(" color = disabled");
182 }
183
185 shambase::format(" tty size = {}x{}", get_tty_lines(), get_tty_columns()));
186 }
187 }
188
189} // namespace shamcmdopt
This header file contains utility functions related to exception handling in the code.
bool term_support_color()
detect if terminal emulator support colored outputs
This file handler generic cli & env options.
void println(std::string_view sv)
Prints a string to the console followed by a newline.
Definition print.cpp:37
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for cli utilities
Definition ci_env.hpp:19
std::optional< std::string > getenv_str(const char *env_var)
Get the content of the environment variable if it exist.
Definition env.cpp:24
void process_cmdopt_generic_opts()
Process generic cli and env variables options.
void set_tty_columns(u32 columns)
Set the forced width of the terminal.
Definition tty.cpp:43
bool has_option(const std::string_view &option_name)
Check if an option is present.
Definition cmdopt.cpp:128
bool is_a_tty()
Test if current terminal is a tty.
Definition tty.cpp:31
void process_colors()
Detect if the current process should use colored output or not.
void process_tty()
Process the SHAMTTYCOL environment variable to set the number of columns for the terminal.
void register_cmdopt_generic_opts()
Register generic cli and env variables options.
void register_env_var_doc(std::string env_var, std::string desc)
Register the documentation of an environment variable.
Definition env.cpp:35
u32 get_tty_lines()
Get the number of lines of the current terminal.
Definition tty.cpp:82
void register_opt(std::string name, std::optional< std::string > args, std::string description)
Register a command line option.
Definition cmdopt.cpp:165
void print_help()
Print the help message.
Definition cmdopt.cpp:210
u32 get_tty_columns()
Get the number of columns of the current terminal.
Definition tty.cpp:81
This file contains tty info getters.