16 colors

This commit is contained in:
y4my4my4m 2023-05-11 16:47:41 +09:00
parent 85b5eca553
commit 56fc4020ac

View file

@ -2,15 +2,15 @@
// Public Domain
#define TELNET_PORT 23
#define BUF_SIZE 16384 // way too big?
#define TIMEOUT_DURATION 50000
#define BUF_SIZE 8192 // way too big?
#define TIMEOUT_DURATION 5000
#define NEGOTIATE 0xFF
#define ANSI_ESC 0x1B
#define ANSI_CSI 0x5B // [
#define MAX_ANSI_PARAMS 256
#define MAX_ANSI_PARAMS 32
CTask *input_task = NULL;
Bool force_disconnect = FALSE;
@ -228,7 +228,10 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
ptr++;
if (*ptr == ANSI_CSI) {
ptr++;
I64 ansi_code[MAX_ANSI_PARAMS];
I64 ansi_code[MAX_ANSI_PARAMS], counter;
for (counter = 0; counter < MAX_ANSI_PARAMS; counter++) {
ansi_code[counter] = 0; // Initialize all elements to 0
}
I64 ansi_param_count = 0;
while (IsDigit(*ptr) || *ptr == ';') {
if (IsDigit(*ptr)) {
@ -251,6 +254,7 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
// Handle specific ANSI escape sequences
switch (*ptr) {
case 'n':
SysLog("Case n\n");
if (ansi_code[0] == 5) {
// Respond with terminal readiness
SysLog("reported terminal readiness\n");
@ -274,54 +278,99 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
// colors might be printed in the wrong order?
// like, <Esc>[1;40m and now <Esc>[40m;1m
I64 m;
Bool isBright = FALSE;
for (m = 0; m < ansi_param_count; m++) {
if (ansi_code[m] <= 10) {
switch (ansi_code[m]) {
case 0: "$$BG$$$$FG$$"; break; // reset
// case 1: ""; break; // TODO: bold
// case 2: ""; break; // TODO: dim
// case 3: ""; break; // TODO: italic
// case 4: "$$UL,1$$" + string + "$$UL,0$$"; break; // TODO: underline
// case 5: "$$"; break; // TODO: blink
// case 6: ""; break; // TODO: fast blink
// case 7: "$$IV,1$$" + string + "$$IV,0$$"; break; // TODO: invert
// case 8: ""; break; // TODO: hide (rare)
// case 9: ""; break; // TODO: strikethrough
// case 10: ""; break; // TODO: primary font
if (ansi_param_count == 0)
{
switch (ansi_code[m]) {
case 0: "$$BG$$$$FG$$"; break; // reset
// case 0: "$$BG,TRANSPARENT$$$$WHITE$$"; break; // reset
// case 1: ""; break; // TODO: bold
// case 2: ""; break; // TODO: dim
// case 3: ""; break; // TODO: italic
// case 4: "$$UL,1$$" + string + "$$UL,0$$"; break; // TODO: underline
// case 5: "$$"; break; // TODO: blink
// case 6: ""; break; // TODO: fast blink
// case 7: "$$IV,1$$" + string + "$$IV,0$$"; break; // TODO: invert
// case 8: ""; break; // TODO: hide (rare)
// case 9: ""; break; // TODO: strikethrough
// case 10: ""; break; // TODO: primary font
}
}
else {
switch (ansi_code[m]) {
case 1: isBright = TRUE; break;
}
}
}
else if (ansi_code[m] >= 30 && ansi_code[m] <= 37) {
// Set foreground color
// SysLog("ansi_code[%d] = %d\n", m, ansi_code[m]);
switch (ansi_code[m]) {
case 30: "$$BLACK$$"; break;
case 31: "$$RED$$"; break;
case 32: "$$GREEN$$"; break;
case 33: "$$YELLOW$$"; break;
case 34: "$$BLUE$$"; break;
case 35: "$$PURPLE$$"; break;
case 36: "$$CYAN$$"; break;
case 37: "$$WHITE$$"; break;
case 39: "$$FG$$"; break; // reset
default: break;
if(!isBright){
switch (ansi_code[m]) {
case 30: "$$BLACK$$"; break;
case 31: "$$RED$$"; break;
case 32: "$$GREEN$$"; break;
case 33: "$$YELLOW$$"; break;
case 34: "$$BLUE$$"; break;
case 35: "$$PURPLE$$"; break;
case 36: "$$CYAN$$"; break;
case 37: "$$WHITE$$"; break;
case 39: "$$WHITE$$"; break; // reset
// case 39: "$$FG$$"; break; // would normally call $$FG$$ but telnet actually resets to white
default: break;
}
}
else {
switch (ansi_code[m]) {
case 30: "$$DKGRAY$$"; break;
case 31: "$$LTRED$$"; break;
case 32: "$$LTGREEN$$"; break;
case 33: "$$YELLOW$$"; break;
case 34: "$$LTBLUE$$"; break;
case 35: "$$LTPURPLE$$"; break;
case 36: "$$LTCYAN$$"; break;
case 37: "$$LTGRAY$$"; break;
case 39: "$$LTGRAY$$"; break; // reset
default: break;
}
}
}
// this is a dumb approach, just do a CatPrint or something
// until we properly catch the `;` it will stay fucked
// until we properly catch the `;` it will stay buggy
else if (ansi_code[m] >= 40 && ansi_code[m] <= 47) {
// Set background color
// SysLog("ansi_code[%d] = %d\n", m, ansi_code[m]);
switch (ansi_code[m]) {
case 40: "$$BG,BLACK$$"; break;
case 41: "$$BG,RED$$"; break;
case 42: "$$BG,GREEN$$"; break;
case 43: "$$BG,YELLOW$$"; break;
case 44: "$$BG,BLUE$$"; break;
case 45: "$$BG,PURPLE$$"; break;
case 46: "$$BG,CYAN$$"; break;
case 47: "$$BG,WHITE$$"; break;
case 49: "$$BG$$"; break; // reset
default: break;
if(!isBright){
switch (ansi_code[m]) {
case 40: "$$BG,BLACK$$"; break;
case 41: "$$BG,RED$$"; break;
case 42: "$$BG,GREEN$$"; break;
case 43: "$$BG,YELLOW$$"; break;
case 44: "$$BG,BLUE$$"; break;
case 45: "$$BG,PURPLE$$"; break;
case 46: "$$BG,CYAN$$"; break;
case 47: "$$BG,WHITE$$"; break;
case 49: "$$BG,BLACK$$"; break; // reset
// case 49: "$$BG$$"; break; // would normally call $$BG$$ but telnet actually resets to black
default: break;
}
}
else {
switch (ansi_code[m]) {
case 40: "$$BG,DKGRAY$$"; break;
case 41: "$$BG,LTRED$$"; break;
case 42: "$$BG,LTGREEN$$"; break;
case 43: "$$BG,YELLOW$$"; break;
case 44: "$$BG,LTBLUE$$"; break;
case 45: "$$BG,LTPURPLE$$"; break;
case 46: "$$BG,LTCYAN$$"; break;
case 47: "$$BG,LTGRAY$$"; break;
case 49: "$$BG,DKGRAY$$"; break; // reset
// case 49: "$$BG$$"; break; // would normally call $$BG$$ but telnet actually resets to black
default: break;
}
}
}
}
@ -343,7 +392,7 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
break;
case 'C':
// Cursor Right
// SysLog("Cursor Right %d %d\n", ansi_param_count, ansi_code[0]);
SysLog("Cursor Right %d %d\n", ansi_param_count, ansi_code[0]);
// "$$CM+LX,+%d,0$$", ansi_code[0];
// "$$CM,%d,0$$", ansi_code[0];
I64 C;
@ -389,15 +438,19 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
if(ansi_code[1] != 1)
col = ansi_code[1];
// Not sure if really should do -1...
if (row > window_height)
row = window_height-1;
if (col > window_width)
col = window_width-1;
// if (row == 0 && col == 0) {
// ptr++;
// break;
// }
SysLog("H or f AFTER row:%d, col:%d, cnt:%d\n", row, col, ansi_param_count);
// "$$CM,%d,%d$$", row, col;
"$$CM,LE=%d,RE=%d$$", row, col;
if (row > window_height)
row = window_height-2;
if (col > window_width)
col = window_width;
"$$CM,%d,%d$$", row, col;
// "$$CM,LE=%d,RE=%d$$", row, col;
ptr++;
break;
case 'J':
@ -518,11 +571,11 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
ptr++;
}
// Reset ansi_param_count and ansi_code
ansi_param_count = 0;
I64 wtv;
for (wtv = 0; wtv < MAX_ANSI_PARAMS; wtv++) {
ansi_code[wtv] = 0;
}
// ansi_param_count = 0;
// I64 wtv;
// for (wtv = 0; wtv < MAX_ANSI_PARAMS; wtv++) {
// ansi_code[wtv] = 0;
// }
}
} else {