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
16
19#include "shambase/print.hpp"
20#include "shambase/string.hpp"
22#include "sham/term/color.hpp"
23#include "sham/term/env.hpp"
24#include "sham/term/tty.hpp"
25#include "shamcmdopt/cmdopt.hpp"
27#include "shamcmdopt/env.hpp"
28#include <string_view>
29#include <optional>
30#include <stdexcept>
31#include <string>
32#include <vector>
33
34namespace {
35
36 auto term_parse_error_callback(const char *what, std::source_location where)
37 -> std::invalid_argument {
39 };
40
41} // namespace
42
43namespace shamcmdopt {
44
46
47 register_opt("--nocolor", {}, "disable colored output");
48 register_opt("--color", {}, "force colored output");
49 register_opt("--help", {}, "show this message");
50
51 register_env_var_doc("NO_COLOR", "Disable colors (if no color cli args are passed)");
52 register_env_var_doc("CLICOLOR_FORCE", "Enable colors (if no color cli args are passed)");
53 register_env_var_doc("TERM", "Terminal emulator identifier");
54 register_env_var_doc("COLORTERM", "Terminal color support identifier");
55 register_env_var_doc("COLUMN", "Set tty assumed column count");
56 register_env_var_doc("LC_ALL", "Locale override, used to detect UTF-8 support");
58 "LC_CTYPE", "Character classification locale, used to detect UTF-8 support");
59 register_env_var_doc("LANG", "Default locale, used to detect UTF-8 support");
60 register_env_var_doc("NO_UTF8", "Disable UTF-8 output (overrides locale detection)");
61 register_env_var_doc("FORCE_UTF8", "Force UTF-8 output (overrides locale detection)");
62 }
63
79
80 bool has_opt_nocolor = has_option("--nocolor");
81 bool has_opt_color = has_option("--color");
82
83 if (has_opt_color && has_opt_nocolor) {
85 "You can not pass --nocolor and --color simultaneously");
86 }
87
88 auto TERM = getenv_str_view("TERM");
89 auto COLORTERM = getenv_str_view("COLORTERM");
90 auto NO_COLOR = getenv_str_view("NO_COLOR");
91 auto CLICOLOR_FORCE = getenv_str_view("CLICOLOR_FORCE");
92
93 auto COLUMN = getenv_str_view("COLUMN");
94
95 auto LANG = getenv_str_view("LANG");
96 auto lc_all = getenv_str_view("LC_ALL");
97 auto lc_ctype = getenv_str_view("LC_CTYPE");
98
99 auto NO_UTF8 = getenv_str_view("NO_UTF8");
100 auto FORCE_UTF8 = getenv_str_view("FORCE_UTF8");
101
103 {
104 .TERM = TERM,
105 .COLORTERM = COLORTERM,
106 .NO_COLOR = NO_COLOR,
107 .CLICOLOR_FORCE = CLICOLOR_FORCE,
108 .COLUMN = COLUMN,
109 .LANG = LANG,
110 .lc_all = lc_all,
111 .lc_ctype = lc_ctype,
112 .NO_UTF8 = NO_UTF8,
113 .FORCE_UTF8 = FORCE_UTF8,
114 },
115 term_parse_error_callback);
116
117 if (has_opt_nocolor) {
119 } else if (has_opt_color) {
121 }
122 }
123
125
127
128 if (has_option("--help")) {
129 print_help();
130
131 shambase::println("\nEnv deduced vars :");
132
133 if (sham::term::is_a_tty()) {
134 shambase::println(" isatty = Yes");
135 } else {
136 shambase::println(" isatty = No");
137 }
138
140 shambase::println(" color = enabled");
141 } else {
142 shambase::println(" color = disabled");
143 }
144
146 shambase::println(" utf8 = enabled");
147 } else {
148 shambase::println(" utf8 = disabled");
149 }
150
151 const char *color_level_str = "unknown";
152 switch (sham::term::color_level()) {
153 case sham::term::ColorLevel::NoColor : color_level_str = "0 (none)"; break;
154 case sham::term::ColorLevel::Basic : color_level_str = "1 (ANSI/16 colors)"; break;
155 case sham::term::ColorLevel::ANSI256 : color_level_str = "2 (256 colors)"; break;
156 case sham::term::ColorLevel::TrueColor: color_level_str = "3 (truecolor)"; break;
157 }
158 shambase::println(sham::format(" colorlevel = {}", color_level_str));
159
161 sham::format(
162 " tty size = {}x{}",
165 }
166 }
167
168} // namespace shamcmdopt
Source location utility.
Terminal color escape sequences and enable/disable control.
@ TrueColor
24-bit RGB truecolor, \x1b[38;2;r;g;bm.
Definition color.hpp:35
@ NoColor
No color support, plain ASCII output only.
Definition color.hpp:29
@ Basic
ANSI/16 colors (basic SGR codes), \x1b[3Xm.
Definition color.hpp:31
@ ANSI256
256-color palette (16 ANSI + 216-color cube + 24 grayscale), \x1b[38;5;Nm.
Definition color.hpp:33
ColorLevel color_level()
Query the currently detected/forced terminal color support level.
Definition color.cpp:45
This header file contains utility functions related to exception handling in the code.
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
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
namespace for cli utilities
Definition ci_env.hpp:19
void process_cmdopt_generic_opts()
Process generic cli and env variables options.
bool has_option(const std::string_view &option_name)
Check if an option is present.
Definition cmdopt.cpp:128
void register_cmdopt_generic_opts()
Register generic cli and env variables options.
void process_env_vars()
Detect if the current process should use colored output or not.
void register_env_var_doc(std::string env_var, std::string desc)
Register the documentation of an environment variable.
Definition env.cpp:43
std::optional< std::string_view > getenv_str_view(const char *env_var)
Get the content of the environment variable if it exist.
Definition env.cpp:32
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:211
Environment variable parsing for terminal color, size and UTF-8 support configuration (TERM,...
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:132
provide information about the source location
bool colors_enabled()
Are colors enabled.
void disable_colors()
Disable all colors.
void enable_colors()
Enable colors in logs.
This file contains tty info getters.
int get_tty_columns()
Get the number of columns of the current terminal.
Definition tty.cpp:92
bool support_utf8()
Query whether the current terminal/locale supports UTF-8 output.
Definition tty.cpp:99
int get_tty_lines()
Get the number of lines of the current terminal.
Definition tty.cpp:93
bool is_a_tty()
Test if current terminal is a tty.
Definition tty.cpp:45