Rewrote the ansi parser to exactly callback on color setting, so now just need to clean this all up and fix a few little bugs.

This commit is contained in:
Zed A. Shaw 2024-11-16 12:10:14 -05:00
parent 96ee16e598
commit 89a70f398a
9 changed files with 274 additions and 126 deletions

View file

@ -3,6 +3,7 @@
#include "dbc.hpp"
#include <SFML/Graphics.hpp>
#include "ansi_parser.hpp"
#include <iostream>
using namespace fmt;
@ -36,7 +37,9 @@ using namespace fmt;
}
}
action color24b { }
action color_out {
color_cb(color, bgcolor);
}
action is_fg {
target = color;
}
@ -45,17 +48,30 @@ using namespace fmt;
}
action out {
write(bgcolor, color, fc);
write_cb(fc);
}
action reset_fg { color = $default_fg; }
action reset_bg { bgcolor = $default_bg; }
action invert {
color = {100,100,100}; // UNDO THIS
bgcolor = $default_bg; // THIS TOO
color_cb(color, bgcolor);
}
action reset_invert {
color = $default_fg;
bgcolor = $default_bg;
color_cb(color, bgcolor);
}
action half_bright {
}
action red { target.r = value; }
action blue { target.g = value; }
action green { target.b = value; }
action start { value = 0; }
action end { }
action end {}
action log { println("command {}", (char)fc); }
ESC = 0x1B;
start = ESC "[";
@ -70,7 +86,16 @@ using namespace fmt;
start %start
(
reset |
(fg|bg) color24b num %red ";" num %blue ";" num %green %color24b
"0" |
"1" |
"2" %half_bright |
"3" |
"4" |
"5" |
"6" |
"7" %invert |
"27" %reset_invert |
(fg|bg) (color24b num %red ";" num %blue ";" num %green ) %color_out
) "m" %end
);
@ -93,7 +118,7 @@ ANSIParser::ANSIParser(sf::Color default_fg, sf::Color default_bg) :
}
}
bool ANSIParser::parse(std::wstring_view codes, WriteCB write) {
bool ANSIParser::parse(std::wstring_view codes, ColorCB color_cb, WriteCB write_cb) {
const wchar_t *start = NULL;
int cs = 0;
unsigned int value = 0;
@ -107,5 +132,15 @@ bool ANSIParser::parse(std::wstring_view codes, WriteCB write) {
%% write init;
%% write exec;
return p - pe == 0;
bool good = p - pe == 0;
if(!good) {
println("FAIL AT {}", p - pe);
// dear cthuhlu, save me from the pain that is wstring
for(int i = 0; i < 100; i++) {
print("{}", char(p[i]));
}
}
return good;
}