diff --git a/bantam.hpp b/bantam.hpp index 5cf672e..30952a1 100644 --- a/bantam.hpp +++ b/bantam.hpp @@ -20,6 +20,7 @@ #define BANTAM_HPP #include +#include #include #include #include @@ -171,8 +172,26 @@ std::string_view utf8_truncate(std::string_view s, size_t max_bytes); std::string filter_text(std::string_view s); int color_enabled(const Cfg &cfg); -std::string cstr(std::string_view text, std::span codes); -void cprint(std::string_view text, std::span codes); +template +inline std::string cstr(std::string_view text, const R& codes) { + if (!g_col || codes.empty() || text.empty()) return std::string(text); + std::string s; + s.reserve(text.size() + 16); + s += "\033["; + for (size_t i = 0; i < codes.size(); i++) { + if (i) s += ';'; + s += sfmt("%d", (int)codes[i]); + } + s += "m"; + s += text; + s += "\033[0m"; + return s; +} +template +inline void cprint(std::string_view text, const R& codes) { + std::string w = cstr(text, codes); + std::fputs(w.c_str(), stdout); +} int visible_len(std::string_view s); int rune_width(int cp); diff --git a/render.cpp b/render.cpp index b8a669a..d8cab80 100644 --- a/render.cpp +++ b/render.cpp @@ -59,8 +59,9 @@ static bool is_word_byte(unsigned char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; } +template static void wrap_pairs(std::string &dst, std::string_view src, std::string_view open, - std::string_view close, std::span codes, + std::string_view close, const R& codes, bool boundary = false) { size_t olen = open.size(), clen = close.size(); size_t p = 0; diff --git a/util.cpp b/util.cpp index 512c5f5..c7bf5e8 100644 --- a/util.cpp +++ b/util.cpp @@ -115,25 +115,6 @@ std::string filter_text(std::string_view s) { int g_col = 0; volatile int g_interrupted = 0; -std::string cstr(std::string_view text, std::span codes) { - if (!g_col || codes.empty() || text.empty()) return std::string(text); - std::string s; - s.reserve(text.size() + 16); - s += "\033["; - for (size_t i = 0; i < codes.size(); i++) { - if (i) s += ';'; - s += sfmt("%d", codes[i]); - } - s += "m"; - s += text; - s += "\033[0m"; - return s; -} -void cprint(std::string_view text, std::span codes) { - std::string w = cstr(text, codes); - std::fputs(w.c_str(), stdout); -} - int visible_len(std::string_view s) { int n = 0; const unsigned char *p = (const unsigned char *)s.data();