Skip to content

Commit

Permalink
[core] TermCtl: Refactoring - add public render method
Browse files Browse the repository at this point in the history
  • Loading branch information
rbrich committed Jul 14, 2024
1 parent c12a489 commit e862bf0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/xci/core/TermCtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ TermCtl& TermCtl::clear_line_to_end() { return TERM_APPEND(clr_eol); }
TermCtl& TermCtl::soft_reset() { return XCI_TERM_APPEND(seq::send_soft_reset); }


namespace format_parser {
namespace render_parser {

struct Char : pegtl::any {};
struct Escape : pegtl::seq< pegtl::one<'\\'>, Char > {};
Expand Down Expand Up @@ -658,13 +658,13 @@ struct Action< Tag > {
}
};

} // namespace format_parser
} // namespace render_parser

std::string TermCtl::_format(std::string_view fmt)
std::string TermCtl::render(std::string_view markup)
{
pegtl::memory_input in( std::to_address(fmt.begin()), std::to_address(fmt.end()) );
pegtl::memory_input in( std::to_address(markup.begin()), std::to_address(markup.end()) );
std::string r;
pegtl::parse< format_parser::Grammar, format_parser::Action >( in, *this, r );
pegtl::parse< render_parser::Grammar, render_parser::Action >( in, *this, r );
return r;
}

Expand Down
16 changes: 12 additions & 4 deletions src/xci/core/TermCtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,31 @@ class TermCtl {
void write_nl() { m_seq.append(1, '\n'); write(seq()); }
friend std::ostream& operator<<(std::ostream& os, TermCtl& t) { return os << t.seq(); }

/// Format string, adding colors via special placeholders:
/// Translate special markup language to color/mode control sequences:
/// <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 ...)
std::string render(std::string_view markup);

/// Format a string, adding colors via special placeholders - see `render` above.
/// Note that these placeholders are applied ahead of the <fmt> placeholders,
/// so e.g. `{}` cannot expand to `<bold>` which would be recognized. If this is
/// intended, call `fmt::format` separately and pass the result to `TermCtl::render`.
template<typename... T>
std::string format(fmt::format_string<T...> fmt, T&&... args) {
const auto sv = fmt.get();
return fmt::vformat(_format(std::string_view(sv.data(), sv.size())),
return fmt::vformat(render(std::string_view(sv.data(), sv.size())),
fmt::make_format_args(args...));
}

/// Print string with special color/mode placeholders, see `format` above.
/// Print string with special color/mode placeholders, see `render` above.
template<typename... T>
void print(fmt::format_string<T...> fmt, T&&... args) {
write(format(fmt::runtime(fmt), args...));
}
void print(std::string_view markup) {
write(render(markup));
}

void write(std::string_view buf);
void write_raw(std::string_view buf); // doesn't check newline
Expand Down Expand Up @@ -394,7 +403,6 @@ class TermCtl {
TermCtl& _tab_set_all(std::span<const unsigned> n_cols);
TermCtl& _append_seq(const char* seq) { if (seq) m_seq += seq; return *this; } // needed for TermInfo, which returns NULL for unknown seqs
TermCtl& _append_seq(std::string_view seq) { m_seq += seq; return *this; }
std::string _format(std::string_view fmt);

std::string m_seq; // cached capability sequences
WriteCallback m_write_cb;
Expand Down

0 comments on commit e862bf0

Please sign in to comment.