5. support colorrules[] to match strings against window titles, applycolorrules(), and call madtty_set_dflt_colors()
On 03/25/2011 10:25 PM, Ross Mohn wrote: > I'm going to post a series of 6 patches for dvtm over the next several > minutes. They all apply to the current HEAD of the dvtm git repository > (committed 2011-01-07). I think I've teased them all apart pretty well > so that each one is separate and complete. > > 1. support for compiling under AIX > 2. support the 8 basic highlighted colors, plus a couple of color_hash fixes > 3. fix a scrolling issue and add in "ESC #" to call interpret_esc_SCS() > 4. support for "ESC 6 n", get cursor position, which calls a new > function, send_curs() > 5. support colorrules[] to match strings against window titles, > applycolorrules(), and call madtty_set_dflt_colors() > 6. support to backfill text from the buffer when a windows is resized > with more rows > > Comments, testing, and bugfixes sincerely welcome! > > Cheers! -Ross > >
Index: madtty.c =================================================================== --- madtty.c (.../vendor/current) (revision 47) +++ madtty.c (.../trunk) (revision 47) @@ -105,8 +105,8 @@ /* geometry */ int rows, cols, maxcols; - unsigned curattrs, savattrs; - short curfg, curbg, savfg, savbg; + unsigned curattrs, savattrs, dfltattrs; + short curfg, curbg, savfg, savbg, dfltfg, dfltbg; /* scrollback buffer */ struct t_row_t *scroll_buf; @@ -197,13 +199,13 @@ >> NCURSES_ATTR_SHIFT; } -static void t_row_set(t_row_t *row, int start, int len, madtty_t *t) +static void t_row_set(t_row_t *row, int start, int len, madtty_t *t, bool dfltcolor) { row->dirty = true; wmemset(row->text + start, 0, len); - attr_t attr = t ? build_attrs(t->curattrs) : 0; - short fg = t ? t->curfg : -1; - short bg = t ? t->curbg : -1; + attr_t attr = build_attrs(dfltcolor ? t->dfltattrs : t->curattrs); + short fg = dfltcolor ? t->dfltfg : t->curfg; + short bg = dfltcolor ? t->dfltbg : t->curbg; for (int i = start; i < len + start; i++) { row->attr[i] = attr; row->fg [i] = fg; @@ -316,7 +318,7 @@ static void cursor_line_down(madtty_t *t) { - t_row_set(t->curs_row, t->cols, t->maxcols - t->cols, 0); + t_row_set(t->curs_row, t->cols, t->maxcols - t->cols, t, 1); t->curs_row++; if (t->curs_row < t->scroll_bot) return; @@ -325,7 +327,7 @@ t->curs_row = t->scroll_bot - 1; fill_scroll_buf(t, 1); - t_row_set(t->curs_row, 0, t->cols, t); + t_row_set(t->curs_row, 0, t->cols, t, 0); } static void new_escape_sequence(madtty_t *t) @@ -354,16 +356,18 @@ { if (pcount == 0) { /* special case: reset attributes */ - t->curattrs = A_NORMAL; - t->curfg = t->curbg = -1; + t->curattrs = t->dfltattrs; + t->curfg = t->dfltfg; + t->curbg = t->dfltbg; return; } for (int i = 0; i < pcount; i++) { switch (param[i]) { case 0: - t->curattrs = A_NORMAL; - t->curfg = t->curbg = -1; + t->curattrs = t->dfltattrs; + t->curfg = t->dfltfg; + t->curbg = t->dfltbg; break; case 1: t->curattrs |= A_BOLD; @@ -404,7 +408,7 @@ i += 2; break; case 39: - t->curfg = -1; + t->curfg = t->dfltfg; break; case 40 ... 47: /* bg */ t->curbg = param[i] - 40; @@ -415,8 +419,14 @@ i += 2; break; case 49: - t->curbg = -1; + t->curbg = t->dfltbg; break; case 90 ... 97: /* hi fg */ t->curfg = param[i] - 82; @@ -429,8 +439,9 @@ t_row_t *row, *start, *end; save_attrs(t); - t->curattrs = A_NORMAL; - t->curfg = t->curbg = -1; + t->curattrs = t->dfltattrs; + t->curfg = t->dfltfg; + t->curbg = t->dfltbg; /* decide range */ if (pcount && param[0] == 2) { @@ -439,15 +450,15 @@ } else if (pcount && param[0] == 1) { start = t->lines; end = t->curs_row; - t_row_set(t->curs_row, 0, t->curs_col + 1, t); + t_row_set(t->curs_row, 0, t->curs_col + 1, t, 0); } else { - t_row_set(t->curs_row, t->curs_col, t->cols - t->curs_col, t); + t_row_set(t->curs_row, t->curs_col, t->cols - t->curs_col, t, 0); start = t->curs_row + 1; end = t->lines + t->rows; } for (row = start; row < end; row++) - t_row_set(row, 0, t->cols, t); + t_row_set(row, 0, t->cols, t, 0); restore_attrs(t); } @@ -495,13 +506,13 @@ { switch (pcount ? param[0] : 0) { case 1: - t_row_set(t->curs_row, 0, t->curs_col + 1, t); + t_row_set(t->curs_row, 0, t->curs_col + 1, t, 0); break; case 2: - t_row_set(t->curs_row, 0, t->cols, t); + t_row_set(t->curs_row, 0, t->cols, t, 0); break; default: - t_row_set(t->curs_row, t->curs_col, t->cols - t->curs_col, t); + t_row_set(t->curs_row, t->curs_col, t->cols - t->curs_col, t, 0); break; } } @@ -522,7 +533,7 @@ row->fg [i] = row->fg [i - n]; } - t_row_set(row, t->curs_col, n, t); + t_row_set(row, t->curs_col, n, t, 0); } /* Interpret the 'delete chars' sequence (DCH) */ @@ -541,7 +552,7 @@ row->fg [i] = row->fg [i + n]; } - t_row_set(row, t->cols - n, n, t); + t_row_set(row, t->cols - n, n, t, 0); } /* Interpret an 'insert line' sequence (IL) */ @@ -551,11 +562,11 @@ if (t->curs_row + n >= t->scroll_bot) { for (t_row_t *row = t->curs_row; row < t->scroll_bot; row++) - t_row_set(row, 0, t->cols, t); + t_row_set(row, 0, t->cols, t, 0); } else { t_row_roll(t->curs_row, t->scroll_bot, -n); for (t_row_t *row = t->curs_row; row < t->curs_row + n; row++) - t_row_set(row, 0, t->cols, t); + t_row_set(row, 0, t->cols, t, 0); } } @@ -566,11 +577,11 @@ if (t->curs_row + n >= t->scroll_bot) { for (t_row_t *row = t->curs_row; row < t->scroll_bot; row++) - t_row_set(row, 0, t->cols, t); + t_row_set(row, 0, t->cols, t, 0); } else { t_row_roll(t->curs_row, t->scroll_bot, n); for (t_row_t *row = t->scroll_bot - n; row < t->scroll_bot; row++) - t_row_set(row, 0, t->cols, t); + t_row_set(row, 0, t->cols, t, 0); } } @@ -582,7 +593,7 @@ if (t->curs_col + n < t->cols) n = t->cols - t->curs_col; - t_row_set(t->curs_row, t->curs_col, n, t); + t_row_set(t->curs_row, t->curs_col, n, t, 0); } /* Interpret a 'set scrolling region' (DECSTBM) sequence */ @@ -660,8 +671,9 @@ t->curshid = false; break; case 47: /* use alternate screen buffer */ - t->curattrs = A_NORMAL; - t->curfg = t->curbg = -1; + t->curattrs = t->dfltattrs; + t->curfg = t->dfltfg; + t->curbg = t->dfltbg; break; case 1000: /* enable normal mouse tracking */ t->mousetrack = true; @@ -679,8 +691,9 @@ t->curshid = true; break; case 47: /* use normal screen buffer */ - t->curattrs = A_NORMAL; - t->curfg = t->curbg = -1; + t->curattrs = t->dfltattrs; + t->curfg = t->dfltfg; + t->curbg = t->dfltbg; break; case 1000: /* disable normal mouse tracking */ t->mousetrack = false; @@ -748,7 +764,7 @@ t->curs_row--; else { t_row_roll(t->scroll_top, t->scroll_bot, -1); - t_row_set(t->scroll_top, 0, t->cols, t); + t_row_set(t->scroll_top, 0, t->cols, t, 0); } } @@ -1077,6 +1095,13 @@ return 0; } +void madtty_set_dflt_colors(madtty_t *t, unsigned attrs, short fg, short bg) +{ + t->dfltattrs = attrs; + t->dfltfg = fg; + t->dfltbg = bg; +} + madtty_t *madtty_create(int rows, int cols, int scroll_buf_sz) { madtty_t *t; @@ -1112,7 +1137,7 @@ t->curs_row = t->lines; t->curs_col = 0; t->curattrs = A_NORMAL; /* white text over black background */ - t->curfg = t->curbg = -1; + t->curfg = t->curbg = t->dfltfg = t->dfltbg = -1; /* initial scrolling area is the whole window */ t->scroll_top = t->lines; @@ -1133,9 +1158,9 @@ t->scroll_amount = 0; /* clear the screen */ - t_row_set(t->curs_row, t->curs_col, t->cols - t->curs_col, t); + t_row_set(t->curs_row, t->curs_col, t->cols - t->curs_col, t, 0); for (t_row_t *row = t->curs_row + 1; row < t->lines + t->rows; row++) - t_row_set(row, 0, t->cols, t); + t_row_set(row, 0, t->cols, t, 0); return t; } @@ -1153,7 +1178,7 @@ if (t->rows != rows) { if (t->curs_row > lines+rows) { /* scroll up instead of simply chopping off bottom */ - fill_scroll_buf(t, t->rows - rows); + fill_scroll_buf(t, (t->curs_row - t->lines) - rows + 1); } while (t->rows > rows) { free(lines[t->rows - 1].text); @@ -1171,7 +1196,7 @@ lines[row].fg = realloc(lines[row].fg, sizeof(short) * cols); lines[row].bg = realloc(lines[row].bg, sizeof(short) * cols); if (t->cols < cols) - t_row_set(lines + row, t->cols, cols - t->cols, 0); + t_row_set(lines + row, t->cols, cols - t->cols, t, 1); lines[row].dirty = true; } t_row_t *sbuf = t->scroll_buf; @@ -1181,7 +1206,7 @@ sbuf[row].fg = realloc(sbuf[row].fg, sizeof(short) * cols); sbuf[row].bg = realloc(sbuf[row].bg, sizeof(short) * cols); if (t->cols < cols) - t_row_set(sbuf + row, t->cols, cols - t->cols, 0); + t_row_set(sbuf + row, t->cols, cols - t->cols, t, 1); } t->maxcols = cols; t->cols = cols; Index: madtty.h =================================================================== --- madtty.h (.../vendor/current) (revision 47) +++ madtty.h (.../trunk) (revision 47) @@ -58,6 +58,7 @@ void madtty_set_handler(madtty_t *, madtty_handler_t); void madtty_set_data(madtty_t *, void *); void *madtty_get_data(madtty_t *); +void madtty_set_dflt_colors(madtty_t *, unsigned attrs, short fg, short bg); madtty_t *madtty_create(int rows, int cols, int scroll_buf_sz); void madtty_resize(madtty_t *, int rows, int cols); Index: config.h =================================================================== --- config.h (.../vendor/current) (revision 47) +++ config.h (.../trunk) (revision 47) @@ -98,6 +104,12 @@ { MOD, '?', { create, { "man dvtm", "dvtm help" } } }, }; +static const ColorRule colorrules[] = { + /* title attrs fgcolor bgcolor */ + { "epicprod", A_NORMAL, COLOR_BLACK, 224 }, + { "epicshad", A_NORMAL, COLOR_BLACK, 195 }, +}; + /* possible values for the mouse buttons are listed below: * * BUTTON1_PRESSED mouse button 1 down Index: dvtm.c =================================================================== --- dvtm.c (.../vendor/current) (revision 47) +++ dvtm.c (.../trunk) (revision 47) @@ -57,10 +61,15 @@ Client *prev; }; +typedef struct { + const char *title; + unsigned attrs; + short fg; + short bg; +} ColorRule; + #define ALT(k) ((k) + (161 - 'a')) #define CONTROL(k) ((k) & 0x1F) #define CTRL_ALT(k) ((k) + (129 - 'a')) @@ -99,6 +108,7 @@ #define countof(arr) (sizeof(arr) / sizeof((arr)[0])) #define sstrlen(str) (sizeof(str) - 1) #define max(x, y) ((x) > (y) ? (x) : (y)) +#define LENGTH(x) (sizeof x / sizeof x[0]) #ifdef NDEBUG #define debug(format, args...) @@ -608,6 +619,24 @@ kill(-sel->pid, SIGKILL); } +static void +applycolorrules(madtty_t *term, char *title) { + unsigned int i; + unsigned attrs = A_NORMAL; + short fg = -1, bg = -1; + const ColorRule *r; + + for(i = 0; i < LENGTH(colorrules); i++) { + r = &colorrules[i]; + if (strstr(title, r->title)) { + attrs = r->attrs; + fg = r->fg; + bg = r->bg; + } + } + madtty_set_dflt_colors(term, attrs, fg, bg); +} + static int title_escape_seq_handler(madtty_t *term, char *es) { Client *c; @@ -621,6 +650,7 @@ strncpy(c->title, es + 3, sizeof(c->title)); draw_border(c); debug("window title: %s\n", c->title); + applycolorrules(term, c->title); return MADTTY_HANDLER_OK; }