Skip to content

Commit

Permalink
[core] TermCtl: Drop the fg: bg: prefixes in color tags
Browse files Browse the repository at this point in the history
Use '@' for bg, make fg the default: `<red><@blue>`
  • Loading branch information
rbrich committed Jul 13, 2024
1 parent 5560887 commit 1c59429
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 110 deletions.
2 changes: 1 addition & 1 deletion examples/core/demo_termctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main()

cout << t.move_up().move_right(6).bold().green() << "GREEN" <<t.normal() << endl;

t.print("<b><fg:yellow>formatted<n>\n");
t.print("<b><yellow>formatted <*white><@yellow> bg <n>\n");
t.print("<bold>bold<normal_intensity> "
"<dim>dim<normal_intensity> "
"<italic>italic<no_italic> "
Expand Down
14 changes: 7 additions & 7 deletions src/xci/core/ArgParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ std::string Option::usage() const
if (!p)
break;
if (is_remainder() && p.dashes == 2 && p.len == 0) {
res += t.format("[<fg:green>{}<normal>] ", std::string(dp + p.pos, p.dashes));
res += t.format("[<green>{}<normal>] ", std::string(dp + p.pos, p.dashes));
} else if (first) {
first = false;
res += t.format("<bold><fg:green>{}<normal>", std::string(dp + p.pos, p.dashes + p.len));
res += t.format("<bold><green>{}<normal>", std::string(dp + p.pos, p.dashes + p.len));
} else if (!p.dashes) {
res += t.format(" <fg:green>{}<normal>", std::string(dp + p.pos, p.len));
res += t.format(" <green>{}<normal>", std::string(dp + p.pos, p.len));
}
dp += p.end();
}
Expand Down Expand Up @@ -312,7 +312,7 @@ ArgParser& ArgParser::operator()(const char* argv[], bool detect_width, unsigned
if (!parse_program_name(argv[0])) {
// this should not occur
auto& t = TermCtl::stderr_instance();
t.print("<bold><fg:red>Missing program name (argv[0])<normal>\n");
t.print("<bold><red>Missing program name (argv[0])<normal>\n");
exit(1);
}
try {
Expand All @@ -326,7 +326,7 @@ ArgParser& ArgParser::operator()(const char* argv[], bool detect_width, unsigned
}
} catch (const BadArgument& e) {
auto& t = TermCtl::stderr_instance();
t.print("<bold><fg:yellow>Error: <fg:red>{}<normal>\n\n", e.what());
t.print("<bold><yellow>Error: <red>{}<normal>\n\n", e.what());
print_usage();
print_help_notice();
exit(1);
Expand Down Expand Up @@ -525,7 +525,7 @@ void ArgParser::print_usage() const

unsigned indent = 0;
{
auto head = t.format("<bold><fg:yellow>Usage:<normal> <bold>{}<normal> ", m_progname);
auto head = t.format("<bold><yellow>Usage:<normal> <bold>{}<normal> ", m_progname);
indent = TermCtl::stripped_width(head);
cout << head;
}
Expand All @@ -545,7 +545,7 @@ void ArgParser::print_help() const
desc_cols = std::max(desc_cols, (unsigned) opt.desc().size());
print_usage();
auto& t = TermCtl::stdout_instance();
t.print("\n<bold><fg:yellow>Options:<normal>\n");
t.print("\n<bold><yellow>Options:<normal>\n");
for (const auto& opt : m_opts) {
cout << " " << opt.formatted_desc(desc_cols) << " ";
wrapping_print(opt.help(), desc_cols + 4, 0, m_max_width);
Expand Down
54 changes: 19 additions & 35 deletions src/xci/core/TermCtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,14 @@ std::string TermCtl::_format(std::string_view fmt)
if (*it == '<') {
++it;

bool is_bg = false;
if (*it == '@') {
is_bg = true;
++it;
}

auto beg = it;
while (std::islower(*it) || *it == '_')
while (std::islower(*it) || *it == '_' || *it == '*')
++it;
std::string_view key (std::to_address(beg), it - beg);
if (*it == '>') {
Expand All @@ -628,43 +634,21 @@ std::string TermCtl::_format(std::string_view fmt)
++it;
continue;
}
}
if (*it != ':') {
r.push_back('<');
r += key;
continue;
}
++it;

beg = it;
while (std::islower(*it) || *it == '_' || *it == '*')
++it;
std::string_view value (std::to_address(beg), it-beg);
if (*it != '>') {
r.push_back('<');
r += key;
r.push_back(':');
r += value;
continue;
}
++it;

if (key == "fg" || key == "bg") {
const auto c = _parse_color(value);
if (c > Color::_Last) {
r.push_back('<');
r += key;
r.push_back(':');
r += value;
r.push_back('>');
const auto c = _parse_color(key);
if (c <= Color::_Last) {
if (is_bg)
r += bg(c).seq();
else
r += fg(c).seq();
++it;
continue;
}
if (key == "fg")
r += fg(c).seq();
else
r += bg(c).seq();
continue;
}
// rollback
r.push_back('<');
if (is_bg)
r.push_back('@');
r += key;
}
r.push_back(*it);
++it;
Expand Down
4 changes: 2 additions & 2 deletions src/xci/core/TermCtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ class TermCtl {
friend std::ostream& operator<<(std::ostream& os, TermCtl& t) { return os << t.seq(); }

/// Format string, adding colors via special placeholders:
/// <fg:COLOR> where COLOR is default | red | *red ... ("*" = bright)
/// <bg:COLOR> where COLOR is the same as for fg
/// <COLOR> where COLOR is default | red | *red ... ("*" = bright)
/// <@BG_COLOR> where BG_COLOR is the same as for COLOR
/// <MODE> where MODE is bold | underline | normal ... (shortcuts b | u | n ...)
template<typename... T>
std::string format(fmt::format_string<T...> fmt, T&&... args) {
Expand Down
20 changes: 10 additions & 10 deletions src/xci/core/log_termctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ namespace xci::core {
// 5..9 => multi-line continuation for each log level
static constexpr size_t c_cont = 5;
static constexpr const char* c_log_format[] = {
"{0:%F %T} <fg:cyan>{1:6x}<normal> <bold>TRACE<normal> <fg:blue>{2}<normal>\n",
"{0:%F %T} <fg:cyan>{1:6x}<normal> <bold>DEBUG<normal> <fg:white>{2}<normal>\n",
"{0:%F %T} <fg:cyan>{1:6x}<normal> <bold>INFO <normal> <bold><fg:white>{2}<normal>\n",
"{0:%F %T} <fg:cyan>{1:6x}<normal> <bold>WARN <normal> <bold><fg:yellow>{2}<normal>\n",
"{0:%F %T} <fg:cyan>{1:6x}<normal> <bold>ERROR<normal> <bold><fg:red>{2}<normal>\n",
" <bold>...<normal> <fg:blue>{2}<normal>\n",
" <bold>...<normal> <fg:white>{2}<normal>\n",
" <bold>...<normal> <bold><fg:white>{2}<normal>\n",
" <bold>...<normal> <bold><fg:yellow>{2}<normal>\n",
" <bold>...<normal> <bold><fg:red>{2}<normal>\n",
"{0:%F %T} <cyan>{1:6x}<normal> <bold>TRACE<normal> <blue>{2}<normal>\n",
"{0:%F %T} <cyan>{1:6x}<normal> <bold>DEBUG<normal> <white>{2}<normal>\n",
"{0:%F %T} <cyan>{1:6x}<normal> <bold>INFO <normal> <bold><white>{2}<normal>\n",
"{0:%F %T} <cyan>{1:6x}<normal> <bold>WARN <normal> <bold><yellow>{2}<normal>\n",
"{0:%F %T} <cyan>{1:6x}<normal> <bold>ERROR<normal> <bold><red>{2}<normal>\n",
" <bold>...<normal> <blue>{2}<normal>\n",
" <bold>...<normal> <white>{2}<normal>\n",
" <bold>...<normal> <bold><white>{2}<normal>\n",
" <bold>...<normal> <bold><yellow>{2}<normal>\n",
" <bold>...<normal> <bold><red>{2}<normal>\n",
};
static constexpr const char* c_log_intro = "<underline> Date Time TID Level Message <normal>\n";

Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ TEST_CASE( "stripped_width", "[TermCtl]" )
CHECK(TermCtl::stripped_width("test") == 4);
CHECK(TermCtl::stripped_width("") == 2);
TermCtl t(1, TermCtl::IsTty::Always);
CHECK(TermCtl::stripped_width(t.format("<fg:green>test<normal>")) == 4);
CHECK(TermCtl::stripped_width(t.format("<green>test<normal>")) == 4);
CHECK(TermCtl::stripped_width("\x1b[32mtest\x1b(B\x1b[m") == 4);
CHECK(TermCtl::stripped_width("\n") == 1); // newline is 1 column (special handling in EditLine)
}
10 changes: 5 additions & 5 deletions tools/data_archive/dar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void extract_entry(const std::string& name, const VfsFile& file, const fs
{
const auto entry_path = output_path / name;
TermCtl& term = TermCtl::stdout_instance();
term.print("Extracting file\t<fg:yellow>{}<normal> to {}\n", name, entry_path);
term.print("Extracting file\t<yellow>{}<normal> to {}\n", name, entry_path);
auto content = file.content();
if (content) {
fs::create_directories(entry_path.parent_path());
Expand Down Expand Up @@ -152,20 +152,20 @@ int main(int argc, const char* argv[])
} (argv);

if (files.empty()) {
term.print("<bold><fg:yellow>No input files.<normal>\n");
term.print("<bold><yellow>No input files.<normal>\n");
}

Vfs vfs;
for (const auto filename : files) {
term.print("<bold>Extracting archive\t<fg:yellow>{}<normal>\n", filename);
term.print("<bold>Extracting archive\t<yellow>{}<normal>\n", filename);
if (!vfs.mount(filename)) {
term.print("<bold><fg:red>Could not mount {}<normal>\n", filename);
term.print("<bold><red>Could not mount {}<normal>\n", filename);
continue;
}

if (list_entries) {
for (const auto& entry : *vfs.mounts().back().vfs_dir) {
term.print("<fg:yellow>{}<normal>\n", entry.name());
term.print("<yellow>{}<normal>\n", entry.name());
}
continue;
}
Expand Down
52 changes: 26 additions & 26 deletions tools/data_inspect/dati.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,33 @@ static void print_data(TermCtl& term, uint8_t type, const std::byte* data, size_
{
const auto expected_size = BinaryBase::size_by_type(type);
if (expected_size != size_t(-1) && size != expected_size) {
term.print("<fg:red>bad size {}<normal>", size);
term.print("<red>bad size {}<normal>", size);
return;
}

switch (type) {
case BinaryBase::Null: term.print("<fg:yellow>null<normal>"); return;
case BinaryBase::BoolFalse: term.print("<fg:yellow>false<normal>"); return;
case BinaryBase::BoolTrue: term.print("<fg:yellow>true<normal>"); return;
case BinaryBase::Fixed8: term.print("<fg:magenta>{}<normal>", unsigned(*data)); return;
case BinaryBase::Fixed16: term.print("<fg:magenta>{}<normal>", bit_copy<uint16_t>(data)); return;
case BinaryBase::Fixed32: term.print("<fg:magenta>{}<normal>", bit_copy<uint32_t>(data)); return;
case BinaryBase::Fixed64: term.print("<fg:magenta>{}<normal>", bit_copy<uint64_t>(data)); return;
case BinaryBase::Fixed128: term.print("<fg:magenta>{}<normal>", uint128_to_string(bit_copy<uint128>(data))); return;
case BinaryBase::Float32: term.print("<fg:magenta>{}<normal>", bit_copy<float>(data)); return;
case BinaryBase::Float64: term.print("<fg:magenta>{}<normal>", bit_copy<double>(data)); return;
case BinaryBase::VarInt: term.print("<fg:yellow>varint<normal>"); return;
case BinaryBase::Array: term.print("<fg:yellow>array<normal>"); return;
case BinaryBase::Null: term.print("<yellow>null<normal>"); return;
case BinaryBase::BoolFalse: term.print("<yellow>false<normal>"); return;
case BinaryBase::BoolTrue: term.print("<yellow>true<normal>"); return;
case BinaryBase::Fixed8: term.print("<magenta>{}<normal>", unsigned(*data)); return;
case BinaryBase::Fixed16: term.print("<magenta>{}<normal>", bit_copy<uint16_t>(data)); return;
case BinaryBase::Fixed32: term.print("<magenta>{}<normal>", bit_copy<uint32_t>(data)); return;
case BinaryBase::Fixed64: term.print("<magenta>{}<normal>", bit_copy<uint64_t>(data)); return;
case BinaryBase::Fixed128: term.print("<magenta>{}<normal>", uint128_to_string(bit_copy<uint128>(data))); return;
case BinaryBase::Float32: term.print("<magenta>{}<normal>", bit_copy<float>(data)); return;
case BinaryBase::Float64: term.print("<magenta>{}<normal>", bit_copy<double>(data)); return;
case BinaryBase::VarInt: term.print("<yellow>varint<normal>"); return;
case BinaryBase::Array: term.print("<yellow>array<normal>"); return;
case BinaryBase::String:
term.print("<fg:green>\"{}\"<normal>",
term.print("<green>\"{}\"<normal>",
escape(std::string_view((const char*) data, size)));
return;
case BinaryBase::Binary: term.print("<fg:yellow>(size {})<normal>", size); return;
case BinaryBase::Binary: term.print("<yellow>(size {})<normal>", size); return;
case BinaryBase::Master:
term.print("<fg:yellow>(size {})<normal> <bold>{{<normal>", size); return;
case BinaryBase::Control: term.print("<fg:yellow>control<normal>"); return;
term.print("<yellow>(size {})<normal> <bold>{{<normal>", size); return;
case BinaryBase::Control: term.print("<yellow>control<normal>"); return;
}
term.print("<fg:red>unknown<normal>");
term.print("<red>unknown<normal>");
}


Expand Down Expand Up @@ -110,7 +110,7 @@ int main(int argc, const char* argv[])
} (argv);

if (files.empty()) {
term.print("<bold><fg:yellow>No input files.<normal>\n");
term.print("<bold><yellow>No input files.<normal>\n");
}

Schema schema;
Expand All @@ -121,15 +121,15 @@ int main(int argc, const char* argv[])
reader(schema);
reader.finish_and_check();
} catch (const ArchiveError& e) {
term.print("<bold><fg:red>Error reading schema: {}<normal>\n", e.what());
term.print("<bold><red>Error reading schema: {}<normal>\n", e.what());
}
} else if (files.size() == 1 && std::string(files.back()).ends_with(".schema")) {
// get Schema of .schema file
schema("schema", schema);
}

for (const auto& filename : files) {
term.print("<fg:yellow><bold>{}<normal>\n", filename);
term.print("<yellow><bold>{}<normal>\n", filename);
std::ifstream f(filename, std::ios::binary);
try {
BinaryReader reader(f);
Expand Down Expand Up @@ -182,12 +182,12 @@ int main(int argc, const char* argv[])
if (opt_int)
last_int_values[schema_member->name] = *opt_int;
}
term.print("{}<bold><fg:cyan>{} ({}: {})<normal>: {} = ",
term.print("{}<bold><cyan>{} ({}: {})<normal>: {} = ",
std::string(indent, ' '),
int(it.key), schema_member->name, schema_member->type,
type_to_cstr(it.type));
} else {
term.print("{}<bold><fg:cyan>{}<normal>: {} = ",
term.print("{}<bold><cyan>{}<normal>: {} = ",
std::string(indent, ' '),
int(it.key), type_to_cstr(it.type));
}
Expand All @@ -198,9 +198,9 @@ int main(int argc, const char* argv[])
uint32_t stored_crc = 0;
std::memcpy(&stored_crc, it.data.get(), it.size);
if (reader.crc() == stored_crc)
term.print(" <bold><fg:green>(CRC32: OK)<normal>");
term.print(" <bold><green>(CRC32: OK)<normal>");
else
term.print(" <bold><fg:red>(CRC32: expected {})<normal>",
term.print(" <bold><red>(CRC32: expected {})<normal>",
reader.crc());
}
}
Expand Down Expand Up @@ -228,7 +228,7 @@ int main(int argc, const char* argv[])
}
}
} catch (const ArchiveError& e) {
term.print("<bold><fg:red>{}<normal>\n", e.what());
term.print("<bold><red>{}<normal>\n", e.what());
}
}

Expand Down
4 changes: 2 additions & 2 deletions tools/fire_script/Highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,13 @@ auto Highlighter::highlight(std::string_view input, unsigned cursor) -> HlResult
try {
auto root = tao::pegtl::parse_tree::parse< Main, Node, HighlightSelector, tao::pegtl::nothing, Control >( in );
if (root->children.size() != 1)
return {std::string{input} + m_term.format("\n<fg:*red><bold>highlighter parse error:<normal> <fg:*red>no match<normal>"), false};
return {std::string{input} + m_term.format("\n<*red><bold>highlighter parse error:<normal> <*red>no match<normal>"), false};
auto last_color = highlight_node(*root->children[0], HighlightColor{}, cursor);
switch_color(last_color, HighlightColor{});
return {m_output, m_open_bracket};
} catch (tao::pegtl::parse_error& e) {
// The grammar is build in a way that parse error should never happen
return {std::string{input} + m_term.format("\n<fg:*red><bold>highlighter parse error:<normal> <fg:*red>{}<normal>", e.what()), false};
return {std::string{input} + m_term.format("\n<*red><bold>highlighter parse error:<normal> <*red>{}<normal>", e.what()), false};
}
}

Expand Down
12 changes: 6 additions & 6 deletions tools/fire_script/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ namespace xci::script::tool {
using namespace xci::core;

static constexpr const char* intro =
"<bold><fg:magenta>🔥 fire script<normal> <fg:magenta>v{}<normal>\n"
"Type <bold><fg:yellow>.h<normal> for help, <bold><fg:yellow>.q<normal> to quit.\n";
static constexpr const char* prompt = "<fg:green>_{}><normal> ";
"<bold><magenta>🔥 fire script<normal> <magenta>v{}<normal>\n"
"Type <bold><yellow>.h<normal> for help, <bold><yellow>.q<normal> to quit.\n";
static constexpr const char* prompt = "<green>_{}><normal> ";


Program::Program(bool log_debug)
Expand Down Expand Up @@ -171,10 +171,10 @@ void Program::evaluate_input(std::string_view input)
repl_command().eval(input.substr(1));
} catch (const ScriptError& e) {
auto& tout = ctx.term_out;
tout.print("<fg:red>{}: <bold>{}<normal>\n", e.code(), e.what());
tout.print("<red>{}: <bold>{}<normal>\n", e.code(), e.what());
if (!e.detail().empty())
tout.print("<fg:magenta>{}<normal>\n", e.detail());
tout.print("<fg:yellow>Help: .h | .help<normal>\n");
tout.print("<magenta>{}<normal>\n", e.detail());
tout.print("<yellow>Help: .h | .help<normal>\n");
}
return;
}
Expand Down
Loading

0 comments on commit 1c59429

Please sign in to comment.