2016-09-07 00:01:56 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2016-09-10 11:00:26 +00:00
|
|
|
#include <getopt.h>
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
#define DEFAULT_FG 7
|
|
|
|
#define DEFAULT_BG 0
|
2016-09-10 10:58:31 +00:00
|
|
|
#define DEFAULT_BOLD false
|
2016-09-07 00:01:56 +00:00
|
|
|
#define DEFAULT_ICE false
|
|
|
|
|
|
|
|
/* technically theres no limit in the format */
|
|
|
|
#define MAX_PARAMS 8
|
|
|
|
|
|
|
|
/* 4 bytes + '\0' */
|
|
|
|
#define MAX_UTFSTR 5
|
|
|
|
|
|
|
|
typedef struct cell_s {
|
|
|
|
char *utfchar;
|
|
|
|
int fg;
|
|
|
|
int bg;
|
|
|
|
bool bold;
|
|
|
|
bool ice;
|
|
|
|
} cell_t;
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
typedef struct canvas_s {
|
|
|
|
/* canvas state */
|
|
|
|
cell_t **cell;
|
|
|
|
char fg;
|
|
|
|
char bg;
|
|
|
|
bool bold;
|
|
|
|
bool ice;
|
|
|
|
|
|
|
|
/* cursor state */
|
|
|
|
int row;
|
|
|
|
int col;
|
|
|
|
int bottomrow;
|
|
|
|
int oldrow;
|
|
|
|
int oldcol;
|
|
|
|
|
|
|
|
/* options */
|
|
|
|
int max_cols;
|
|
|
|
bool use_color;
|
|
|
|
bool show_unp;
|
|
|
|
int tab_size;
|
|
|
|
int lcrop;
|
|
|
|
int rcrop;
|
|
|
|
int default_fg;
|
|
|
|
int default_bg;
|
|
|
|
} canvas_t;
|
|
|
|
|
2016-09-07 00:01:56 +00:00
|
|
|
int get_fgcolor(cell_t *cell);
|
|
|
|
int get_bgcolor(cell_t *cell);
|
2016-09-21 17:40:19 +00:00
|
|
|
void print_color(canvas_t *c, cell_t *cell, int col);
|
|
|
|
void inc_row(canvas_t *c);
|
|
|
|
void draw_cell(canvas_t *c, char *inchar, int fg, int bg, bool bold, bool ice);
|
2016-09-07 00:01:56 +00:00
|
|
|
void usage(void);
|
|
|
|
|
|
|
|
/* lol globals */
|
|
|
|
|
|
|
|
/* ANSI COLORS BLK RED GRN YEL BLU MAG CYN WHT */
|
|
|
|
/* 0 1 2 3 4 5 6 7 */
|
|
|
|
/* MIRC COLORS ------------------------------- */
|
|
|
|
const char color[8] = { 1, 5, 3, 7, 2, 6, 10, 15 };
|
|
|
|
const char color_bold[8] = { 14, 4, 9, 8, 12, 13, 11, 0 };
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int opt;
|
|
|
|
FILE *fh;
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
canvas_t *c = (canvas_t *)calloc(1, sizeof(canvas_t));
|
|
|
|
|
|
|
|
c->fg = DEFAULT_FG;
|
|
|
|
c->bg = DEFAULT_BG;
|
|
|
|
c->bold = false;
|
|
|
|
c->ice = false;
|
|
|
|
|
|
|
|
c->row = -1;
|
|
|
|
c->col = 0;
|
|
|
|
c->bottomrow = -1;
|
|
|
|
c->oldrow = 0;
|
|
|
|
c->oldcol = 0;
|
|
|
|
|
|
|
|
c->max_cols = 80;
|
|
|
|
c->use_color = true;
|
|
|
|
c->show_unp = false;
|
|
|
|
c->tab_size = 8;
|
|
|
|
c->lcrop = 0;
|
|
|
|
c->rcrop = 0;
|
|
|
|
c->default_fg = 7;
|
|
|
|
c->default_bg = 0;
|
|
|
|
|
2016-09-07 00:01:56 +00:00
|
|
|
while((opt = getopt(argc, argv, "npl:r:w:t:")) != -1) {
|
|
|
|
switch(opt) {
|
|
|
|
case 'n':
|
2016-09-21 17:40:19 +00:00
|
|
|
c->use_color = false;
|
2016-09-07 00:01:56 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
2016-09-21 17:40:19 +00:00
|
|
|
c->show_unp = true;
|
2016-09-07 00:01:56 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
2016-09-21 17:40:19 +00:00
|
|
|
c->lcrop = atoi(optarg);
|
2016-09-07 00:01:56 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
2016-09-21 17:40:19 +00:00
|
|
|
c->rcrop = atoi(optarg);
|
2016-09-07 00:01:56 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
2016-09-21 17:40:19 +00:00
|
|
|
c->max_cols = atoi(optarg);
|
2016-09-07 00:01:56 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2016-09-21 17:40:19 +00:00
|
|
|
c->tab_size = atoi(optarg);
|
2016-09-07 00:01:56 +00:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
/* fallthrough */
|
|
|
|
case 'h':
|
|
|
|
/* fallthrough */
|
|
|
|
default:
|
|
|
|
usage();
|
2016-09-21 17:40:19 +00:00
|
|
|
return (EXIT_FAILURE);
|
2016-09-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2016-09-07 00:16:45 +00:00
|
|
|
/* init the first line */
|
2016-09-21 17:40:19 +00:00
|
|
|
inc_row(c);
|
|
|
|
|
2016-09-07 00:01:56 +00:00
|
|
|
if (argc == 1) {
|
|
|
|
fh = fopen(argv[0], "r");
|
|
|
|
|
|
|
|
} else if (argc == 0) {
|
|
|
|
fh = stdin;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fh) {
|
|
|
|
perror(NULL);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *charp = malloc(1);
|
|
|
|
if (!charp) {
|
|
|
|
perror(NULL);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *space = " ";
|
|
|
|
|
|
|
|
int rc = 0;
|
|
|
|
int pcnt = 0;
|
|
|
|
int param[MAX_PARAMS];
|
|
|
|
uint32_t ch = 0;
|
|
|
|
|
|
|
|
while ((ch = fgetc(fh)) != EOF) {
|
|
|
|
|
|
|
|
/* ignore sauce record */
|
|
|
|
if (ch == 0x1A) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drawable character */
|
|
|
|
if (ch != 0x1B) {
|
|
|
|
*charp = (char)ch;
|
|
|
|
|
|
|
|
/* convert tabs to spaces */
|
|
|
|
if (ch == '\t') {
|
2016-09-21 17:40:19 +00:00
|
|
|
for (int i = 0; i < c->tab_size; i++) {
|
|
|
|
draw_cell(c, space, c->fg, c->bg, c->bold, c->ice);
|
2016-09-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (ch == '\0') {
|
2016-09-21 17:40:19 +00:00
|
|
|
draw_cell(c, space, DEFAULT_FG, DEFAULT_BG, c->bold, c->ice);
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
} else if (ch != '\r' && ch != '\n') {
|
2016-09-21 17:40:19 +00:00
|
|
|
draw_cell(c, charp, c->fg, c->bg, c->bold, c->ice);
|
2016-09-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == '\n') {
|
2016-09-21 17:40:19 +00:00
|
|
|
while (c->col != 0) {
|
|
|
|
draw_cell(c, space, c->fg, c->bg, c->bold, c->ice);
|
|
|
|
}
|
2016-09-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* escape code */
|
|
|
|
} else {
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
2016-09-21 17:40:19 +00:00
|
|
|
|
2016-09-07 00:01:56 +00:00
|
|
|
/* no rational way to recover tbqh imho */
|
|
|
|
if ((ch = fgetc(fh)) != '[') {
|
|
|
|
fprintf(stderr, "Invalid escape code, aborting at line %d\n",
|
2016-09-21 17:40:19 +00:00
|
|
|
c->row + 1);
|
2016-09-07 00:01:56 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ch = fgetc(fh)) != EOF) {
|
2016-09-21 17:40:19 +00:00
|
|
|
|
2016-09-07 00:01:56 +00:00
|
|
|
/* parameters */
|
|
|
|
if (isdigit(ch)) {
|
|
|
|
*charp = (char)ch;
|
|
|
|
rc = (rc * 10) + atoi(charp);
|
|
|
|
|
|
|
|
/* oddball parameter prefixes */
|
|
|
|
} else if (ch == '?' || ch == '=' || ch == '>') {
|
|
|
|
/* just eat them */
|
|
|
|
|
|
|
|
/* end of parameter, havent encountered ':' but its legit */
|
|
|
|
} else if (ch == ';' || ch == ':') {
|
|
|
|
param[pcnt] = rc;
|
|
|
|
rc = 0;
|
|
|
|
pcnt++;
|
|
|
|
|
|
|
|
/* spacing */
|
|
|
|
} else if (ch == 'C') {
|
|
|
|
|
|
|
|
/* default to one space */
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < rc; i++) {
|
2016-09-21 17:40:19 +00:00
|
|
|
draw_cell(c, space, DEFAULT_FG, DEFAULT_BG,
|
2016-09-07 00:01:56 +00:00
|
|
|
DEFAULT_BOLD, DEFAULT_ICE);
|
|
|
|
|
|
|
|
}
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* SGR sequence */
|
|
|
|
} else if (ch == 'm') {
|
|
|
|
param[pcnt] = rc;
|
|
|
|
pcnt++;
|
|
|
|
for (int i = 0; i < pcnt; i++) {
|
|
|
|
|
|
|
|
/* reset */
|
|
|
|
if (param[i] == 0) {
|
2016-09-21 17:40:19 +00:00
|
|
|
c->bold = false;
|
|
|
|
c->ice = false;
|
|
|
|
c->fg = DEFAULT_FG;
|
|
|
|
c->bg = DEFAULT_BG;
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
} else if (param[i] == 1) {
|
2016-09-21 17:40:19 +00:00
|
|
|
c->bold = true;
|
2016-09-07 00:01:56 +00:00
|
|
|
} else if (param[i] == 5) {
|
2016-09-21 17:40:19 +00:00
|
|
|
c->ice = true;
|
2016-09-07 00:01:56 +00:00
|
|
|
} else if (param[i] == 21 || param[i] == 22) {
|
2016-09-21 17:40:19 +00:00
|
|
|
c->bold = false;
|
2016-09-07 00:01:56 +00:00
|
|
|
} else if (param[i] == 25) {
|
2016-09-21 17:40:19 +00:00
|
|
|
c->ice = false;
|
2016-09-07 00:01:56 +00:00
|
|
|
} else if (param[i] >= 30 && param[i] <= 37) {
|
2016-09-21 17:40:19 +00:00
|
|
|
c->fg = param[i] - 30;
|
2016-09-07 00:01:56 +00:00
|
|
|
} else if (param[i] >= 40 && param[i] <= 47) {
|
2016-09-21 17:40:19 +00:00
|
|
|
c->bg = param[i] - 40;
|
2016-09-07 00:01:56 +00:00
|
|
|
} else if (param[i] == 39) {
|
2016-09-21 17:40:19 +00:00
|
|
|
c->fg = DEFAULT_FG;
|
2016-09-07 00:01:56 +00:00
|
|
|
} else if (param[i] == 49) {
|
2016-09-21 17:40:19 +00:00
|
|
|
c->bg = DEFAULT_BG;
|
2016-09-07 00:01:56 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Ignored SGR parameter %d at line %d\n",
|
2016-09-21 17:40:19 +00:00
|
|
|
param[i], c->row + 1);
|
|
|
|
}
|
2016-09-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* clear screen */
|
|
|
|
/* todo flesh this out */
|
2016-09-21 17:40:19 +00:00
|
|
|
} else if (ch == 'J') {
|
|
|
|
c->row = 0;
|
|
|
|
c->col = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i <= c->bottomrow; i++) {
|
|
|
|
for (int j = 0; j < c->max_cols; j++) {
|
|
|
|
c->cell[i][j].fg = DEFAULT_FG;
|
|
|
|
c->cell[i][j].bg = DEFAULT_BG;
|
|
|
|
c->cell[i][j].bold = DEFAULT_BOLD;
|
|
|
|
c->cell[i][j].ice = DEFAULT_ICE;
|
|
|
|
|
|
|
|
if (c->cell[i][j].utfchar) {
|
|
|
|
free(c->cell[i][j].utfchar);
|
2016-09-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* move up */
|
|
|
|
} else if (ch == 'A') {
|
|
|
|
|
|
|
|
/* default is 1 */
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (rc > 0) {
|
|
|
|
|
|
|
|
/* found an ansi that tried this */
|
|
|
|
/* probably from a bbs prelogin that assumed */
|
|
|
|
/* frontdoor was eating the first few lines */
|
2016-09-21 17:40:19 +00:00
|
|
|
if (c->row == 0) {
|
2016-09-07 00:01:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
c->row--;
|
2016-09-07 00:01:56 +00:00
|
|
|
rc--;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* move down */
|
|
|
|
} else if (ch == 'B') {
|
|
|
|
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (rc > 0) {
|
2016-09-21 17:40:19 +00:00
|
|
|
inc_row(c);
|
2016-09-07 00:01:56 +00:00
|
|
|
rc--;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* move forward */
|
|
|
|
} else if (ch == 'C') {
|
|
|
|
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (rc > 0) {
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
if (c->col == c->max_cols - 1) {
|
2016-09-07 00:01:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
c->col++;
|
2016-09-07 00:01:56 +00:00
|
|
|
rc--;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* move back */
|
|
|
|
} else if (ch == 'D') {
|
|
|
|
|
|
|
|
if (rc == 0) {
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (rc > 0) {
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
if (c->col == 0) {
|
2016-09-07 00:01:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
c->col--;
|
2016-09-07 00:01:56 +00:00
|
|
|
rc--;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* goto */
|
2016-09-21 17:40:19 +00:00
|
|
|
/* todo add check for inc_row */
|
2016-09-07 00:01:56 +00:00
|
|
|
} else if (ch == 'f' || ch == 'H') {
|
|
|
|
|
|
|
|
if (pcnt != 2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
c->row = param[0];
|
|
|
|
c->col = param[1];
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* set mode */
|
|
|
|
} else if (ch == 'h' || ch == 'n') {
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* save position */
|
|
|
|
} else if (ch == 's') {
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
c->oldrow = c->row;
|
|
|
|
c->oldcol = c->col;
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* restore position */
|
|
|
|
} else if (ch == 'u') {
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
c->row = c->oldrow;
|
|
|
|
c->col = c->oldcol;
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* sequences we dont care and/or know about */
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Ignored escape code [");
|
|
|
|
for (int i = 0; i < pcnt; ++i) {
|
|
|
|
fprintf(stderr, "%d%s", param[pcnt],
|
|
|
|
i + 1 < pcnt ? ";" : "");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "0x%02x at line %d col %d\n", ch,
|
2016-09-21 17:40:19 +00:00
|
|
|
c->row + 1, c->col + 1);
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
pcnt = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
for (int i = 0; i <= c->row; i++) {
|
|
|
|
for (int j = c->lcrop; j < c->max_cols - c->rcrop; j++) {
|
|
|
|
cell_t *cell = &c->cell[i][j];
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
if (!cell->utfchar) {
|
|
|
|
printf("\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
if (c->use_color) {
|
|
|
|
print_color(c, cell, j);
|
2016-09-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s", cell->utfchar);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
fclose(fh);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage(void) {
|
2016-09-07 00:40:17 +00:00
|
|
|
fprintf(stderr, "Usage: a2m [options] [input.ans]\n");
|
2016-09-07 00:01:56 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "Options:");
|
2016-09-21 17:40:19 +00:00
|
|
|
fprintf(stderr, "\n");
|
2016-09-07 00:01:56 +00:00
|
|
|
fprintf(stderr, " -l n Crop n lines from the left side.\n");
|
|
|
|
fprintf(stderr, " -r n Crop n lines from the right side.\n");
|
|
|
|
fprintf(stderr, " -n Disable color output.\n");
|
|
|
|
fprintf(stderr, " -p Print unprintable characters.\n");
|
2016-09-21 17:40:19 +00:00
|
|
|
fprintf(stderr, " -t size Specify tab size, default is 8.\n");
|
|
|
|
fprintf(stderr, " -w width Specify width, default is 80.\n");
|
2016-09-07 00:01:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_fgcolor(cell_t *cell) {
|
|
|
|
return cell->bold ? color_bold[cell->fg] : color[cell->fg];
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_bgcolor(cell_t *cell) {
|
|
|
|
return cell->ice ? color_bold[cell->bg] : color[cell->bg];
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
void print_color(canvas_t *c, cell_t *cell, int col) {
|
2016-09-07 00:01:56 +00:00
|
|
|
static cell_t *prev = NULL;
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
int oldfg = (!prev || col == 0 + c->lcrop) ? DEFAULT_FG : prev->fg;
|
|
|
|
int oldbg = (!prev || col == 0 + c->lcrop) ? DEFAULT_BG : prev->bg;
|
|
|
|
int oldbold = (!prev || col == 0 + c->lcrop) ? DEFAULT_BOLD : prev->bold;
|
|
|
|
int oldice = (!prev || col == 0 + c->lcrop) ? DEFAULT_ICE : prev->ice;
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
if (cell->fg != oldfg ||
|
|
|
|
cell->bg != oldbg ||
|
|
|
|
cell->bold != oldbold ||
|
|
|
|
cell->ice != oldice) {
|
|
|
|
|
|
|
|
printf("\x03");
|
|
|
|
|
|
|
|
if (cell->fg != oldfg || cell->bold != oldbold) {
|
|
|
|
printf("%d", get_fgcolor(cell));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->bg != oldbg || (cell->ice != oldice)) {
|
|
|
|
printf(",%d", get_bgcolor(cell));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = cell;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
void inc_row(canvas_t *c) {
|
|
|
|
c->row++;
|
2016-09-07 00:01:56 +00:00
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
if (c->row <= c->bottomrow) {
|
2016-09-07 00:01:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
c->bottomrow = c->row;
|
2016-09-07 00:01:56 +00:00
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
cell_t **newrows = (cell_t **)calloc(c->row + 1, sizeof(cell_t *));
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
if (!newrows) {
|
|
|
|
perror(NULL);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
for (int i = 0; i < c->row; i++) {
|
|
|
|
newrows[i] = c->cell[i];
|
2016-09-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
newrows[c->row] = (cell_t *)calloc(c->max_cols, sizeof(cell_t));
|
2016-09-07 00:01:56 +00:00
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
free(c->cell);
|
|
|
|
c->cell = newrows;
|
2016-09-07 00:01:56 +00:00
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
for (int j = 0; j < c->max_cols; j++) {
|
|
|
|
c->cell[c->row][j].fg = DEFAULT_FG;
|
|
|
|
c->cell[c->row][j].bg = DEFAULT_BG;
|
|
|
|
c->cell[c->row][j].bold = DEFAULT_BOLD;
|
|
|
|
c->cell[c->row][j].ice = DEFAULT_ICE;
|
2016-09-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
void draw_cell(canvas_t *c, char *inchar, int fg, int bg, bool bold, bool ice) {
|
2016-09-07 00:01:56 +00:00
|
|
|
static iconv_t conv = (iconv_t)0;
|
|
|
|
|
|
|
|
char *outcharp = calloc(1, MAX_UTFSTR);
|
|
|
|
if (!outcharp) {
|
|
|
|
perror(NULL);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
char *oldoutcharp = outcharp;
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
if ((unsigned char)inchar[0] < 32 && !c->show_unp) {
|
2016-09-07 00:01:56 +00:00
|
|
|
inchar[0] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t incharsize = 1;
|
|
|
|
size_t outcharsize = MAX_UTFSTR;
|
|
|
|
|
|
|
|
if (!conv) {
|
|
|
|
conv = iconv_open("UTF-8", "CP437");
|
|
|
|
}
|
|
|
|
|
|
|
|
iconv(conv, &inchar, &incharsize, &outcharp, &outcharsize);
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
c->cell[c->row][c->col].utfchar = calloc(1, MAX_UTFSTR);
|
|
|
|
if (!c->cell[c->row][c->col].utfchar) {
|
2016-09-07 00:01:56 +00:00
|
|
|
perror(NULL);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
strcpy(c->cell[c->row][c->col].utfchar, oldoutcharp);
|
2016-09-07 00:01:56 +00:00
|
|
|
|
|
|
|
free(oldoutcharp);
|
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
c->cell[c->row][c->col].fg = fg;
|
|
|
|
c->cell[c->row][c->col].bg = bg;
|
|
|
|
c->cell[c->row][c->col].bold = bold;
|
|
|
|
c->cell[c->row][c->col].ice = ice;
|
2016-09-07 00:01:56 +00:00
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
c->col++;
|
2016-09-07 00:01:56 +00:00
|
|
|
|
2016-09-21 17:40:19 +00:00
|
|
|
if (c->col == c->max_cols) {
|
|
|
|
inc_row(c);
|
|
|
|
c->col = 0;
|
2016-09-07 00:01:56 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|