16#include <magic_enum.hpp>
27bool CmdIOUtil::do_output =
true;
29bool CmdIOUtil::is_initialized =
false;
40 std::cin.tie(&std::cout);
41 std::ios_base::sync_with_stdio(
true);
43 is_initialized =
true;
52 do_output = do_output_;
58 assert(is_initialized);
67 std::cout << std::endl;
74#if defined(DESIGNLAB_USE_COLOR_OUTPUT)
86 const std::string space(tag.size(),
' ');
91 for (
size_t i = 0; i < line.size(); i++)
93 if (i != 0) { std::cout << space; }
100 std::cout << line[i] << std::endl;
103#if defined(DESIGNLAB_USE_COLOR_OUTPUT)
104 std::cout <<
"\x1b[0m";
120 std::stringstream ss(str);
123 while (std::getline(ss, line))
125 if (kHorizontalLineLength > line.length())
129 const int space_num = (kHorizontalLineLength -
static_cast<int>(line.length())) / 2;
131 for (
int i = 0; i < space_num; ++i)
136 Output(space + line, detail);
148 std::stringstream ss(str);
151 while (std::getline(ss, line))
153 if (kHorizontalLineLength > line.length())
157 const int space_num = kHorizontalLineLength -
static_cast<int>(line.length());
159 for (
int i = 0; i < space_num; ++i)
164 Output(space + line, detail);
176 if (num <= 0) {
return; }
178 for (
int i = 0; i < num; i++)
186 if (line_visual.size() != 1) {
return; }
190 for (
int i = 0; i < kHorizontalLineLength; i++)
208 if (output_copy_right)
210 OutputRight(
"(C) 2023 Design Engineering Laboratory ", detail);
228 const int default_num,
const std::string& str)
234 std::string input_str;
235 std::cout <<
">>" << std::flush;
236 std::cin >> input_str;
238 int res = default_num;
242 res = std::stoi(input_str);
244 if (res < min || res > max)
268 std::string input_str;
269 std::cout <<
">>" << std::flush;
270 std::cin >> input_str;
273 if (input_str ==
"y" || input_str ==
"yes" || input_str ==
"Y" || input_str ==
"Yes" || input_str ==
"YES")
277 else if (input_str ==
"n" || input_str ==
"no" || input_str ==
"N" || input_str ==
"No" || input_str ==
"NO")
290 const std::vector<std::string> invalid_chars = {
"\\",
"/",
":",
"*",
"?",
"\"",
"<",
">",
"|" };
291 constexpr int kMaxDirNameLength = 255;
295 std::string input_str;
296 std::cout <<
">>" << std::flush;
297 std::cin >> input_str;
299 bool is_invalid =
true;
301 for (
const auto& invalid_char : invalid_chars)
303 if (input_str.find(invalid_char) != std::string::npos)
305 SystemOutput(
"Directory names cannot contain the following characters : \\ / : * ? \" < > |");
311 if (input_str.find(
" ") != std::string::npos)
317 if (input_str.length() > kMaxDirNameLength)
323 if (input_str.length() == 0)
static void SystemOutput(const std::string &str)
コマンドラインに文字を出力する関数.System用の出力.
static std::string InputDirName(const std::string &str="Enter a directory name. (Japanese is not recommended).")
ディレクトリ名を入力させる関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される. ディレクトリ名には次の文字は使えない. \ / : * ?...
static void SpacedOutput(const std::string &str, OutputDetail detail)
コマンドラインに文字を出力する関数. 前と後ろに改行を挿入する.
static void Output(const std::string &str, OutputDetail detail)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
static void WaitAnyKey(const std::string &str="Waiting for input.")
入力待ちをする関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static int InputInt(int min, int max, int default_num, const std::string &str="Please enter an integer.")
整数を入力させる関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static void OutputNewLine(int num, OutputDetail detail)
コマンドラインで改行をする関数.
static void OutputTitle(const std::string &title_name, bool output_copy_right=false)
コマンドラインにこのソフトのタイトルを出力する関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static void DoOutput(bool do_output)
そもそも出力をするかを設定する関数. false に設定しても システムメッセージは出力される.
static void OutputHorizontalLine(const std::string &line_visual, OutputDetail detail)
コマンドラインに水平線を出力する関数.
static bool InputYesNo(const std::string &str="Are you sure?")
yesかnoを入力させる関数.返り値で yes なら true,noなら falseを返す. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static void OutputCenter(const std::string &str, OutputDetail detail)
中央に文字を出力する関数. 文字列が長すぎる場合は普通に左詰めで出力される.
static void FormatOutput(OutputDetail detail, const std::format_string< Args... > str, Args &&... args)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
static void OutputRight(const std::string &str, OutputDetail detail)
右端に文字を出力する関数. 文字列が長すぎる場合は普通に左詰めで出力される.
static void SetOutputLimit(OutputDetail limit)
出力するメッセージをどこまで許可するかを設定する関数. この関数を呼び出してから出ないと,他の関数を使えない. 例えば kError に設定すると,kError 未満の出力( kInfo とか ...
std::vector< std::string > Split(const std::string &str, const std::string &separator)
文字列を分割する関数.指定した文字で文字列を分割する. 分割した結果,空白が含まれる場合や文字列がない場合は,そのまま返す. 最後が区切り文字で終わる場合は,それを無視する.
std::string EnumToStringRemoveTopK(const T &enum_value)
enumを文字列に変換する関数. Google C++ coding style だと enumの要素は 先頭にkをつけてキャメルケースで書くことが推奨されている. 例えば,
OutputDetail
コマンドラインに文字を出力する際に,その詳細を指定するための列挙体.
@ kSystem
システムメッセージ,常に出力する.
@ kDebug
デバッグ時のみ出力,一番優先度が低い.
@ kWarning
警告メッセージ,エラーではないが注意が必要なメッセージ.