Refactored the ansi_parser into a class that can be reused between render calls.

This commit is contained in:
Zed A. Shaw 2024-11-02 16:56:46 -04:00
parent e864e14eab
commit fd8180bc61
6 changed files with 68 additions and 27 deletions

View file

@ -49,8 +49,8 @@ using namespace fmt;
write(bgcolor, color, fc);
}
action reset_fg { color = default_fg; }
action reset_bg { bgcolor = default_bg; }
action reset_fg { color = $default_fg; }
action reset_bg { bgcolor = $default_bg; }
action red { target.r = value; }
action blue { target.g = value; }
@ -82,15 +82,28 @@ using namespace fmt;
%% write data;
bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default_bg, WriteCB write) {
ANSIParser::ANSIParser(sf::Color default_fg, sf::Color default_bg) :
$default_fg(default_fg),
$default_bg(default_bg)
{
}
bool ANSIParser::parse(const std::string &screen, WriteCB write) {
std::wstring screen_utf8 = $converter.from_bytes(screen);
return parse(screen_utf8, write);
}
bool ANSIParser::parse(std::wstring_view codes, WriteCB write) {
const wchar_t *start = NULL;
int cs = 0;
unsigned int value = 0;
const wchar_t *p = codes.data();
const wchar_t *pe = p + codes.size();
const wchar_t *eof = pe;
sf::Color bgcolor(default_bg);
sf::Color color(default_fg);
sf::Color bgcolor($default_bg);
sf::Color color($default_fg);
sf::Color &target = color;
%% write init;