diff --git a/library/cpp/colorizer/colors.cpp b/library/cpp/colorizer/colors.cpp index be501704c1..75212190d7 100644 --- a/library/cpp/colorizer/colors.cpp +++ b/library/cpp/colorizer/colors.cpp @@ -3,6 +3,8 @@ #include #include +#include + #if defined(_unix_) #include #endif @@ -143,11 +145,11 @@ std::string ToString(NColorizer::EAnsiCode x) { return std::string(ToStringBufC(x)); } -template<> -void Out(IOutputStream& os, TTypeTraits::TFuncParam x) { +std::ostream& operator<<(std::ostream& os, NColorizer::EAnsiCode x) { if (AutoColors(os).IsTTY()) { os << ToStringBufC(x); } + return os; } bool TColors::CalcIsTTY(FILE* file) { @@ -449,11 +451,11 @@ TColors& NColorizer::StdOut() { return *Singleton(); } -TColors& NColorizer::AutoColors(IOutputStream& os) { - if (&os == &Cerr) { +TColors& NColorizer::AutoColors(std::ostream& os) { + if (&os == &std::cerr) { return StdErr(); } - if (&os == &Cout) { + if (&os == &std::cout) { return StdOut(); } return *Singleton(); diff --git a/library/cpp/colorizer/colors.h b/library/cpp/colorizer/colors.h index 875a6f47f3..fab42b5f8c 100644 --- a/library/cpp/colorizer/colors.h +++ b/library/cpp/colorizer/colors.h @@ -220,7 +220,7 @@ namespace NColorizer { /// Choose `TColors` depending on output stream. If passed stream is stderr/stdout, return a corresponding /// singletone. Otherwise, return a disabled singletone (which you can, but should *not* enable). - TColors& AutoColors(IOutputStream& os); + TColors& AutoColors(std::ostream& os); /// Calculate total length of all ANSI escape codes in the text. size_t TotalAnsiEscapeCodeLen(std::string_view text); diff --git a/library/cpp/getopt/small/completer_command.cpp b/library/cpp/getopt/small/completer_command.cpp index 8226c0f9de..eaabbf85a7 100644 --- a/library/cpp/getopt/small/completer_command.cpp +++ b/library/cpp/getopt/small/completer_command.cpp @@ -99,9 +99,9 @@ namespace NLastGetopt { if (shell.empty()) { Cerr << Wrap(80, MakeInfo(command, "--" + name)) << Endl; } else if (shell == "bash") { - TBashCompletionGenerator(opts).Generate(command, Cout); + TBashCompletionGenerator(opts).Generate(command, std::cout); } else if (shell == "zsh") { - TZshCompletionGenerator(opts).Generate(command, Cout); + TZshCompletionGenerator(opts).Generate(command, std::cout); } else { Cerr << "Unknown shell name " << NUtils::Quote(shell) << Endl; exit(1); @@ -140,9 +140,9 @@ namespace NLastGetopt { NUtils::ToLower(arg); if (arg == "bash") { - TBashCompletionGenerator(Modes_).Generate(Command_, Cout); + TBashCompletionGenerator(Modes_).Generate(Command_, std::cout); } else if (arg == "zsh") { - TZshCompletionGenerator(Modes_).Generate(Command_, Cout); + TZshCompletionGenerator(Modes_).Generate(Command_, std::cout); } else { Cerr << "Unknown shell name " << NUtils::Quote(arg) << Endl; parsedOptions.PrintUsage(); diff --git a/library/cpp/getopt/small/completion_generator.cpp b/library/cpp/getopt/small/completion_generator.cpp index 3e41850047..e8f4925ce0 100644 --- a/library/cpp/getopt/small/completion_generator.cpp +++ b/library/cpp/getopt/small/completion_generator.cpp @@ -30,7 +30,7 @@ namespace NLastGetopt { Y_ABORT_UNLESS(opts != nullptr); } - void TZshCompletionGenerator::Generate(std::string_view command, IOutputStream& stream) { + void TZshCompletionGenerator::Generate(std::string_view command, std::ostream& stream) { TFormattedOutput out; NComp::TCompleterManager manager{command}; @@ -370,7 +370,7 @@ namespace NLastGetopt { line << "' \\"; } - void TBashCompletionGenerator::Generate(std::string_view command, IOutputStream& stream) { + void TBashCompletionGenerator::Generate(std::string_view command, std::ostream& stream) { TFormattedOutput out; NComp::TCompleterManager manager{command}; diff --git a/library/cpp/getopt/small/completion_generator.h b/library/cpp/getopt/small/completion_generator.h index 846cc9904f..e5bafba286 100644 --- a/library/cpp/getopt/small/completion_generator.h +++ b/library/cpp/getopt/small/completion_generator.h @@ -15,7 +15,7 @@ namespace NLastGetopt { virtual ~TCompletionGenerator() = default; public: - virtual void Generate(std::string_view command, IOutputStream& stream) = 0; + virtual void Generate(std::string_view command, std::ostream& stream) = 0; protected: std::variant Options_; @@ -26,7 +26,7 @@ namespace NLastGetopt { using TCompletionGenerator::TCompletionGenerator; public: - void Generate(std::string_view command, IOutputStream& stream) override; + void Generate(std::string_view command, std::ostream& stream) override; private: static void GenerateModesCompletion(TFormattedOutput& out, const TModChooser& chooser, NComp::TCompleterManager& manager); @@ -40,7 +40,7 @@ namespace NLastGetopt { using TCompletionGenerator::TCompletionGenerator; public: - void Generate(std::string_view command, IOutputStream& stream) override; + void Generate(std::string_view command, std::ostream& stream) override; private: static void GenerateModesCompletion(TFormattedOutput& out, const TModChooser& chooser, NComp::TCompleterManager& manager, size_t level); diff --git a/library/cpp/getopt/small/formatted_output.cpp b/library/cpp/getopt/small/formatted_output.cpp index 16b8c7681c..9292d71076 100644 --- a/library/cpp/getopt/small/formatted_output.cpp +++ b/library/cpp/getopt/small/formatted_output.cpp @@ -3,6 +3,8 @@ #include #include +#include + namespace NLastGetopt { TFormattedOutput::IndentGuard::IndentGuard(TFormattedOutput* output) @@ -23,16 +25,16 @@ namespace NLastGetopt { return Lines_.emplace_back(IndentLevel_, TStringBuilder()).second; } - void TFormattedOutput::Print(IOutputStream& out) { + void TFormattedOutput::Print(std::ostream& out) { for (auto&[indent, line] : Lines_) { if (indent && !line.empty()) { TTempBuf buf(indent); Fill(buf.Data(), buf.Data() + indent, ' '); - out.Write(buf.Data(), indent); + out.write(buf.Data(), indent); } out << line; if (!std::string_view(line).ends_with('\n')) { - out << Endl; + out << std::endl; } } } diff --git a/library/cpp/getopt/small/formatted_output.h b/library/cpp/getopt/small/formatted_output.h index afabbbaac7..06a2dad5af 100644 --- a/library/cpp/getopt/small/formatted_output.h +++ b/library/cpp/getopt/small/formatted_output.h @@ -22,7 +22,7 @@ namespace NLastGetopt { TStringBuilder& Line(); /// Collect all lines into a stream. - void Print(IOutputStream& out); + void Print(std::ostream& out); private: int IndentLevel_ = 0; diff --git a/library/cpp/getopt/small/last_getopt_opts.cpp b/library/cpp/getopt/small/last_getopt_opts.cpp index 466e928921..1a3534f919 100644 --- a/library/cpp/getopt/small/last_getopt_opts.cpp +++ b/library/cpp/getopt/small/last_getopt_opts.cpp @@ -323,7 +323,7 @@ namespace NLastGetopt { return result.Str(); } - void TOpts::PrintCmdLine(const std::string_view& program, IOutputStream& os, const NColorizer::TColors& colors) const { + void TOpts::PrintCmdLine(const std::string_view& program, std::ostream& os, const NColorizer::TColors& colors) const { os << colors.BoldColor() << "Usage" << colors.OldColor() << ": "; if (!CustomUsage.empty()) { os << CustomUsage; @@ -331,7 +331,7 @@ namespace NLastGetopt { os << program << " "; } if (!CustomCmdLineDescr.empty()) { - os << CustomCmdLineDescr << Endl; + os << CustomCmdLineDescr << std::endl; return; } os << "[OPTIONS]"; @@ -361,11 +361,11 @@ namespace NLastGetopt { os << " [" << TrailingArgSpec_.GetTitle(DefaultFreeArgTitle_) << "]..."; } - os << Endl; + os << std::endl; } - void TOpts::PrintUsage(const std::string_view& program, IOutputStream& osIn, const NColorizer::TColors& colors) const { - TStringStream os; + void TOpts::PrintUsage(const std::string_view& program, std::ostream& osIn, const NColorizer::TColors& colors) const { + std::stringstream os; if (!Title.empty()) os << Title << "\n\n"; @@ -404,14 +404,14 @@ namespace NLastGetopt { if (requiredOptionsSection) { if (requiredOptionsCount == 0) continue; - os << Endl << colors.BoldColor() << "Required parameters" << colors.OldColor() << ":" << Endl; + os << std::endl << colors.BoldColor() << "Required parameters" << colors.OldColor() << ":" << std::endl; } else { if (requiredOptionsCount == Opts_.size()) continue; if (requiredOptionsCount == 0) - os << Endl << colors.BoldColor() << "Options" << colors.OldColor() << ":" << Endl; + os << std::endl << colors.BoldColor() << "Options" << colors.OldColor() << ":" << std::endl; else - os << Endl << colors.BoldColor() << "Optional parameters" << colors.OldColor() << ":" << Endl; // optional options would be a tautology + os << std::endl << colors.BoldColor() << "Optional parameters" << colors.OldColor() << ":" << std::endl; // optional options would be a tautology } for (size_t i = 0; i < Opts_.size(); i++) { @@ -423,7 +423,7 @@ namespace NLastGetopt { continue; if (leftColumnSizes[i] > leftWidth && !opt->GetHelp().empty()) { - os << SPad << leftColumn[i] << Endl << SPad << leftPadding << ' '; + os << SPad << leftColumn[i] << std::endl << SPad << leftPadding << ' '; } else { os << SPad << leftColumn[i] << ' '; if (leftColumnSizes[i] < leftWidth) @@ -443,7 +443,7 @@ namespace NLastGetopt { auto choicesHelp = opt->GetChoicesHelp(); if (!choicesHelp.empty()) { if (!help.empty()) { - os << Endl << SPad << leftPadding << " "; + os << std::endl << SPad << leftPadding << " "; } os << "(values: " << choicesHelp << ")"; } @@ -451,14 +451,14 @@ namespace NLastGetopt { if (opt->HasDefaultValue()) { auto quotedDef = QuoteForHelp(opt->GetDefaultValue()); if (helpHasParagraphs) { - os << Endl << Endl << SPad << leftPadding << " "; + os << std::endl << std::endl << SPad << leftPadding << " "; os << "Default: " << colors.CyanColor() << quotedDef << colors.OldColor() << "."; } else if (help.ends_with('.')) { - os << Endl << SPad << leftPadding << " "; + os << std::endl << SPad << leftPadding << " "; os << "Default: " << colors.CyanColor() << quotedDef << colors.OldColor() << "."; } else if (!help.empty()) { if (SPad.size() + leftWidth + 1 + lastLineLength + 12 + quotedDef.size() > Wrap_) { - os << Endl << SPad << leftPadding << " "; + os << std::endl << SPad << leftPadding << " "; } else { os << " "; } @@ -468,10 +468,10 @@ namespace NLastGetopt { } } - os << Endl; + os << std::endl; if (helpHasParagraphs) { - os << Endl; + os << std::endl; } } } @@ -479,19 +479,19 @@ namespace NLastGetopt { PrintFreeArgsDesc(os, colors); for (auto& [heading, text] : Sections) { - os << Endl << colors.BoldColor() << heading << colors.OldColor() << ":" << Endl; + os << std::endl << colors.BoldColor() << heading << colors.OldColor() << ":" << std::endl; - os << SPad << Wrap(Wrap_, text, SPad) << Endl; + os << SPad << Wrap(Wrap_, text, SPad) << std::endl; } - osIn << os.Str(); + osIn << os.str(); } - void TOpts::PrintUsage(const std::string_view& program, IOutputStream& os) const { + void TOpts::PrintUsage(const std::string_view& program, std::ostream& os) const { PrintUsage(program, os, NColorizer::AutoColors(os)); } - void TOpts::PrintFreeArgsDesc(IOutputStream& os, const NColorizer::TColors& colors) const { + void TOpts::PrintFreeArgsDesc(std::ostream& os, const NColorizer::TColors& colors) const { if (0 == FreeArgsMax_) return; @@ -505,7 +505,7 @@ namespace NLastGetopt { } leftFreeWidth = Min(leftFreeWidth, size_t(30)); - os << Endl << colors.BoldColor() << "Free args" << colors.OldColor() << ":"; + os << std::endl << colors.BoldColor() << "Free args" << colors.OldColor() << ":"; os << " min: " << colors.GreenColor() << FreeArgsMin_ << colors.OldColor() << ","; os << " max: " << colors.GreenColor(); @@ -514,7 +514,7 @@ namespace NLastGetopt { } else { os << "unlimited"; } - os << colors.OldColor() << Endl; + os << colors.OldColor() << std::endl; const size_t limit = FreeArgSpecs_.empty() ? 0 : FreeArgSpecs_.rbegin()->first; for (size_t i = 0; i <= limit; ++i) { @@ -524,8 +524,8 @@ namespace NLastGetopt { auto help = FreeArgSpecs_.at(i).GetHelp(); if (!help.empty()) { auto title = GetFreeArgTitle(i); - os << SPad << colors.GreenColor() << RightPad(title, leftFreeWidth, ' ') << colors.OldColor() - << SPad << help << Endl; + os << SPad << colors.GreenColor() << ToString(RightPad(title, leftFreeWidth, ' ')) << colors.OldColor() + << SPad << help << std::endl; } } @@ -533,8 +533,8 @@ namespace NLastGetopt { auto title = TrailingArgSpec_.GetTitle(DefaultFreeArgTitle_); auto help = TrailingArgSpec_.GetHelp(); if (!help.empty()) { - os << SPad << colors.GreenColor() << RightPad(title, leftFreeWidth, ' ') << colors.OldColor() - << SPad << help << Endl; + os << SPad << colors.GreenColor() << ToString(RightPad(title, leftFreeWidth, ' ')) << colors.OldColor() + << SPad << help << std::endl; } } } diff --git a/library/cpp/getopt/small/last_getopt_opts.h b/library/cpp/getopt/small/last_getopt_opts.h index 4297b2556f..3e0ead7b6a 100644 --- a/library/cpp/getopt/small/last_getopt_opts.h +++ b/library/cpp/getopt/small/last_getopt_opts.h @@ -2,9 +2,10 @@ #include "last_getopt_opt.h" -#include #include +#include +#include namespace NLastGetopt { enum EArgPermutation { @@ -602,7 +603,7 @@ namespace NLastGetopt { * @param os destination stream * @param colors colorizer */ - void PrintUsage(const std::string_view& program, IOutputStream& os, const NColorizer::TColors& colors) const; + void PrintUsage(const std::string_view& program, std::ostream& os, const NColorizer::TColors& colors) const; /** * Print usage string @@ -610,7 +611,7 @@ namespace NLastGetopt { * @param program prefix of result (path to the program) * @param os destination stream */ - void PrintUsage(const std::string_view& program, IOutputStream& os = Cout) const; + void PrintUsage(const std::string_view& program, std::ostream& os = std::cout) const; /** * Get list of options in order of definition. @@ -639,7 +640,7 @@ namespace NLastGetopt { * @param os destination stream * @param colors colorizer */ - void PrintCmdLine(const std::string_view& program, IOutputStream& os, const NColorizer::TColors& colors) const; + void PrintCmdLine(const std::string_view& program, std::ostream& os, const NColorizer::TColors& colors) const; /** * Print usage helper @@ -647,7 +648,7 @@ namespace NLastGetopt { * @param os destination stream * @param colors colorizer */ - void PrintFreeArgsDesc(IOutputStream& os, const NColorizer::TColors& colors) const; + void PrintFreeArgsDesc(std::ostream& os, const NColorizer::TColors& colors) const; }; } diff --git a/library/cpp/getopt/small/last_getopt_parse_result.cpp b/library/cpp/getopt/small/last_getopt_parse_result.cpp index 42522264c3..55ec498876 100644 --- a/library/cpp/getopt/small/last_getopt_parse_result.cpp +++ b/library/cpp/getopt/small/last_getopt_parse_result.cpp @@ -99,7 +99,7 @@ namespace NLastGetopt { return Parser_->ProgramName_; } - void TOptsParseResult::PrintUsage(IOutputStream& os) const { + void TOptsParseResult::PrintUsage(std::ostream& os) const { Parser_->Opts_->PrintUsage(Parser_->ProgramName_, os); } diff --git a/library/cpp/getopt/small/last_getopt_parse_result.h b/library/cpp/getopt/small/last_getopt_parse_result.h index d786b7c0ce..09c0d09157 100644 --- a/library/cpp/getopt/small/last_getopt_parse_result.h +++ b/library/cpp/getopt/small/last_getopt_parse_result.h @@ -165,7 +165,7 @@ namespace NLastGetopt { /** * Print usage string. */ - void PrintUsage(IOutputStream& os = Cout) const; + void PrintUsage(std::ostream& os = std::cout) const; /** * @return position in [premuted argv] of the first free argument diff --git a/library/cpp/getopt/small/last_getopt_parser.cpp b/library/cpp/getopt/small/last_getopt_parser.cpp index 335dbed522..40025d70b5 100644 --- a/library/cpp/getopt/small/last_getopt_parser.cpp +++ b/library/cpp/getopt/small/last_getopt_parser.cpp @@ -379,11 +379,11 @@ namespace NLastGetopt { throw usage; // don't need lineinfo, just the message } - void TOptsParser::PrintUsage(IOutputStream& os, const NColorizer::TColors& colors) const { + void TOptsParser::PrintUsage(std::ostream& os, const NColorizer::TColors& colors) const { Opts_->PrintUsage(ProgramName(), os, colors); } - void TOptsParser::PrintUsage(IOutputStream& os) const { + void TOptsParser::PrintUsage(std::ostream& os) const { PrintUsage(os, NColorizer::AutoColors(os)); } diff --git a/library/cpp/getopt/small/last_getopt_parser.h b/library/cpp/getopt/small/last_getopt_parser.h index 3a1305e8db..5e50949936 100644 --- a/library/cpp/getopt/small/last_getopt_parser.h +++ b/library/cpp/getopt/small/last_getopt_parser.h @@ -3,7 +3,9 @@ #include "last_getopt_opts.h" #include + #include +#include namespace NLastGetopt { /** @@ -145,8 +147,8 @@ namespace NLastGetopt { return ProgramName_; } - void PrintUsage(IOutputStream& os = Cout) const; + void PrintUsage(std::ostream& os = std::cout) const; - void PrintUsage(IOutputStream& os, const NColorizer::TColors& colors) const; + void PrintUsage(std::ostream& os, const NColorizer::TColors& colors) const; }; } //namespace NLastGetopt diff --git a/library/cpp/getopt/small/opt.cpp b/library/cpp/getopt/small/opt.cpp index 73ff2a582c..890342b2e9 100644 --- a/library/cpp/getopt/small/opt.cpp +++ b/library/cpp/getopt/small/opt.cpp @@ -88,7 +88,7 @@ int Opt::Get(int* longOptionIndex) { } } -void Opt::DummyHelp(IOutputStream& os) { +void Opt::DummyHelp(std::ostream& os) { Opts_->PrintUsage(GetProgramName(), os); } diff --git a/library/cpp/getopt/small/opt.h b/library/cpp/getopt/small/opt.h index 86aca66666..74a96ad038 100644 --- a/library/cpp/getopt/small/opt.h +++ b/library/cpp/getopt/small/opt.h @@ -106,7 +106,7 @@ class Opt : TNonCopyable { int GetArgC() const; const char** GetArgV() const; - void DummyHelp(IOutputStream& os = Cerr); + void DummyHelp(std::ostream& os = std::cerr); }; // call before getopt. returns non-negative int, removing it from arguments (not found: -1)