Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Gazizonoki committed Apr 1, 2024
1 parent 5f47805 commit df60a1e
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 58 deletions.
12 changes: 7 additions & 5 deletions library/cpp/colorizer/colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <util/stream/output.h>
#include <util/generic/singleton.h>

#include <iostream>

#if defined(_unix_)
#include <unistd.h>
#endif
Expand Down Expand Up @@ -143,11 +145,11 @@ std::string ToString(NColorizer::EAnsiCode x) {
return std::string(ToStringBufC(x));
}

template<>
void Out<NColorizer::EAnsiCode>(IOutputStream& os, TTypeTraits<NColorizer::EAnsiCode>::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) {
Expand Down Expand Up @@ -449,11 +451,11 @@ TColors& NColorizer::StdOut() {
return *Singleton<TStdOutColors>();
}

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<TDisabledColors>();
Expand Down
2 changes: 1 addition & 1 deletion library/cpp/colorizer/colors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions library/cpp/getopt/small/completer_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions library/cpp/getopt/small/completion_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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};

Expand Down
6 changes: 3 additions & 3 deletions library/cpp/getopt/small/completion_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const TModChooser*, const TOpts*> Options_;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions library/cpp/getopt/small/formatted_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <util/memory/tempbuf.h>
#include <util/generic/algorithm.h>

#include <iostream>

namespace NLastGetopt {

TFormattedOutput::IndentGuard::IndentGuard(TFormattedOutput* output)
Expand All @@ -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;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/cpp/getopt/small/formatted_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 26 additions & 26 deletions library/cpp/getopt/small/last_getopt_opts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,15 @@ 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;
} else {
os << program << " ";
}
if (!CustomCmdLineDescr.empty()) {
os << CustomCmdLineDescr << Endl;
os << CustomCmdLineDescr << std::endl;
return;
}
os << "[OPTIONS]";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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++) {
Expand All @@ -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)
Expand All @@ -443,22 +443,22 @@ namespace NLastGetopt {
auto choicesHelp = opt->GetChoicesHelp();
if (!choicesHelp.empty()) {
if (!help.empty()) {
os << Endl << SPad << leftPadding << " ";
os << std::endl << SPad << leftPadding << " ";
}
os << "(values: " << choicesHelp << ")";
}

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 << " ";
}
Expand All @@ -468,30 +468,30 @@ namespace NLastGetopt {
}
}

os << Endl;
os << std::endl;

if (helpHasParagraphs) {
os << Endl;
os << std::endl;
}
}
}

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;

Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -524,17 +524,17 @@ 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;
}
}

if (FreeArgsMax_ == UNLIMITED_ARGS) {
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;
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions library/cpp/getopt/small/last_getopt_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#include "last_getopt_opt.h"

#include <map>
#include <library/cpp/colorizer/fwd.h>

#include <iostream>
#include <map>

namespace NLastGetopt {
enum EArgPermutation {
Expand Down Expand Up @@ -602,15 +603,15 @@ 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
*
* @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.
Expand Down Expand Up @@ -639,15 +640,15 @@ 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
*
* @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;
};

}
2 changes: 1 addition & 1 deletion library/cpp/getopt/small/last_getopt_parse_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion library/cpp/getopt/small/last_getopt_parse_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions library/cpp/getopt/small/last_getopt_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Loading

0 comments on commit df60a1e

Please sign in to comment.