This commit is contained in:
y4my4my4m 2023-05-02 03:00:19 +09:00
parent 08e0bf8ffa
commit 7beb6c69a4
2 changed files with 36 additions and 164 deletions

View file

@ -1,129 +0,0 @@
#define MODE_TEXT 0
#define MODE_ESCAPE 1
#define MODE_CSI 2
#define CMD_RESET 0
#define CMD_SLOW_BLINK 5
#define CMD_FAST_BLINK 6
#define CMD_FG 3
#define CMD_BG 4
#define CMB_B_FG 9
#define CMD_B_BG 1
U0 PrintANSI(U8 *str, I64 len=-1) {
U64 state = MODE_TEXT;
U64 cmd = -1;
if (len < 0) len = StrLen(str);
U64 i;
for (i=0; i<len; i++) {
start_iter:
U64 ch = str[i];
if (state != MODE_TEXT && ch == 'm') {
state = MODE_TEXT;
goto next_iter;
}
switch (state) {
case MODE_TEXT:
if (ch == 0x1b) {
state = MODE_ESCAPE;
goto next_iter;
}
PutChars(ch);
break;
case MODE_ESCAPE:
if (ch == '[') {
state = MODE_CSI;
goto next_iter;
}
break;
case MODE_CSI:
csi:
if (ch < '0' || ch > '9')
throw('ICSIC');
U64 code = ch - '0';
#define next_ch { if (i+1 >= len) { throw("Expected character"); } ch = str[++i]; code = ch - '0'; }
switch (code) {
case CMD_RESET:
"$$FG$$$$BG$$";
break;
case CMD_SLOW_BLINK:
case CMD_FAST_BLINK:
// FIXME: Add blink
break;
case 'A': // Cursor Up
case 'B': // Cursor Down
case 'C': // Cursor Forward (Right)
case 'D': // Cursor Backward (Left)
case 'E': // Cursor Next Line
case 'F': // Cursor Previous Line
case 'G': // Cursor Horizontal Absolute
case 'H': // Cursor Position (default: top-left corner)
case 'f': // Same as 'H'
case 'J': // Erase Display
ptr++;
DocClear;
break;
case 'K': // Erase Line
case 'S': // Scroll Up
case 'T': // Scroll Down
case '?': // DEC Private Mode
case 'h': // Set Mode
case 'l': // Reset Mode
case 'c': // Device Attributes (Request)
case CMD_FG:
next_ch;
switch (code) {
case 0: "$$BLACK$$"; break;
case 1: "$$RED$$"; break;
case 2: "$$GREEN$$"; break;
case 3: "$$YELLOW$$"; break;
case 4: "$$BLUE$$"; break;
case 5: "$$PURPLE$$"; break;
case 6: "$$CYAN$$"; break;
case 7: "$$WHITE$$"; break;
// FIXME: Handle 8
case 9: "$$FG$$"; break;
}
break;
case CMD_BG:
next_ch;
switch (code) {
case 0: "$$BG,BLACK$$"; break;
case 1: "$$BG,RED$$"; break;
case 2: "$$BG,GREEN$$"; break;
case 3: "$$BG,YELLOW$$"; break;
case 4: "$$BG,BLUE$$"; break;
case 5: "$$BG,PURPLE$$"; break;
case 6: "$$BG,CYAN$$"; break;
case 7: "$$BG,WHITE$$"; break;
// FIXME: Handle 8
case 9: "$$BG$$"; break;
}
break;
default:
"\nCOde: %d\n", code;
throw('ICSI');
}
next_ch;
if (ch == ';') {
next_ch;
goto csi;
}
goto start_iter;
break;
default:
throw('ISTE');
}
next_iter:
}
}
// PrintANSI("He\x1b[33;45mllo\x1b[0m yoooo\n");

View file

@ -302,40 +302,40 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
U8 *line = input_buffer; U8 *line = input_buffer;
U8 *end_of_line = line; U8 *end_of_line = line;
while (1) { while (1) {
switch (KeyGet(&sc)) // switch (KeyGet(&sc))
{ // {
case 0: // case 0:
switch (sc.u8[0]) // switch (sc.u8[0])
{ // {
case SC_CURSOR_UP: // case SC_CURSOR_UP:
TCPSocketSendString(sock, "CU01"); // TCPSocketSendString(sock, "CU01");
SysLog("Cursor up"); // SysLog("Cursor up");
break; // break;
case SC_CURSOR_DOWN: // case SC_CURSOR_DOWN:
TCPSocketSendString(sock, "CD01"); // TCPSocketSendString(sock, "CD01");
SysLog("Cursor down"); // SysLog("Cursor down");
break; // break;
case SC_CURSOR_LEFT: // case SC_CURSOR_LEFT:
TCPSocketSendString(sock, "CB01"); // TCPSocketSendString(sock, "CB01");
SysLog("Cursor left"); // SysLog("Cursor left");
if (line > input_buffer) { // if (line > input_buffer) {
line--; // line--;
"%c", 8; // "%c", 8;
} // }
break; // break;
case SC_CURSOR_RIGHT: // case SC_CURSOR_RIGHT:
SysLog("Cursor right"); // SysLog("Cursor right");
TCPSocketSendString(sock, "CF01"); // TCPSocketSendString(sock, "CF01");
if (line < end_of_line) { // if (line < end_of_line) {
"%c", *line++; // "%c", *line++;
} // }
break; // break;
default: // default:
break; // break;
} // }
default: // default:
break; // break;
} // }
ch = CharGet(, FALSE); ch = CharGet(, FALSE);
if (ch == '\r' || ch == '\n') { if (ch == '\r' || ch == '\n') {
break; break;
@ -366,6 +366,7 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
// TCPSocketSend(sock, line, 16); // TCPSocketSend(sock, line, 16);
} else { } else {
"Force disconnecting...\n"; "Force disconnecting...\n";
goto disconnect;
break; break;
} }
} else { } else {
@ -374,7 +375,7 @@ U0 Telnet(U8 *host, U16 port=TELNET_PORT) {
break; break;
} }
} }
disconnect:
TCPSocketClose(sock); TCPSocketClose(sock);
"Telnet connection closed.\n"; "Telnet connection closed.\n";
} }