commit 74a2587f4c4a35c064c63e8434ec02b805caa63c from: Omar Polo date: Mon Jun 21 17:35:49 2021 UTC first scratch for colour support! commit - 770de0789dd17560929a4844ff26c0bc83bf51d3 commit + 74a2587f4c4a35c064c63e8434ec02b805caa63c blob - b4ebfe7a8d6975644cdd3dae99905bcae3f81dff blob + 4cc3d76a1df4f2ac16293f6ceee899224b20170e --- defaults.c +++ defaults.c @@ -26,6 +26,24 @@ int fill_column = INT_MAX; int olivetti_mode = 0; int enable_colors = 1; +static struct lineface_descr { + int prfx_used, used; + int prfx_pair, pair; + int prfx_bg, bg; + int prfx_fg, fg; +} linefaces_descr[] = { + [LINE_TEXT] = { 0, 0, PAIR_TEXT_PRFX, PAIR_TEXT, 0, 0, 0, 0 }, + [LINE_LINK] = { 0, 0, PAIR_LINK_PRFX, PAIR_LINK, 0, 0, 0, 0 }, + [LINE_TITLE_1] = { 0, 0, PAIR_TITLE_1_PRFX, PAIR_TITLE_1, 0, 0, 0, 0 }, + [LINE_TITLE_2] = { 0, 0, PAIR_TITLE_2_PRFX, PAIR_TITLE_1, 0, 0, 0, 0 }, + [LINE_TITLE_3] = { 0, 0, PAIR_TITLE_3_PRFX, PAIR_TITLE_3, 0, 0, 0, 0 }, + [LINE_ITEM] = { 0, 0, PAIR_ITEM_PRFX, PAIR_ITEM, 0, 0, 0, 0 }, + [LINE_QUOTE] = { 0, 0, PAIR_QUOTE_PRFX, PAIR_QUOTE, 0, 0, 0, 0 }, + [LINE_PRE_START] = { 0, 0, PAIR_PRE_START_PRFX, PAIR_TEXT, 0, 0, 0, 0 }, + [LINE_PRE_CONTENT] = { 0, 0, PAIR_PRE_PRFX, PAIR_PRE, 0, 0, 0, 0 }, + [LINE_PRE_END] = { 0, 0, PAIR_PRE_END_PRFX, PAIR_PRE_END, 0, 0, 0, 0 }, +}; + struct lineprefix line_prefixes[] = { [LINE_TEXT] = { "", "" }, [LINE_LINK] = { "=> ", " " }, @@ -66,46 +84,58 @@ struct minibuffer_face minibuffer_face = { .background = A_NORMAL, }; +struct mapping { + const char *label; + int linetype; + int facetype; + int facetype_prfx; +} mappings[] = { + {"text", LINE_TEXT, PAIR_TEXT, PAIR_TEXT_PRFX}, + {"link", LINE_LINK, PAIR_LINK, PAIR_LINK_PRFX}, + {"title1", LINE_TITLE_1, PAIR_TITLE_1, PAIR_TITLE_1_PRFX}, + {"title2", LINE_TITLE_2, PAIR_TITLE_2, PAIR_TITLE_2_PRFX}, + {"title3", LINE_TITLE_3, PAIR_TITLE_3, PAIR_TITLE_3_PRFX}, + {"item", LINE_ITEM, PAIR_ITEM, PAIR_ITEM_PRFX}, + {"quote", LINE_QUOTE, PAIR_QUOTE, PAIR_QUOTE_PRFX}, + {"pre.start", LINE_PRE_START, PAIR_PRE_START, PAIR_PRE_START_PRFX}, + {"pre", LINE_PRE_CONTENT, PAIR_PRE, PAIR_PRE_PRFX}, + {"pre.end", LINE_PRE_END, PAIR_PRE_END, PAIR_PRE_END_PRFX}, +}; + +static struct mapping * +mapping_by_name(const char *name) +{ + size_t i; + + for (i = 0; i < sizeof(mappings)/sizeof(mappings[0]); ++i) { + if (!strcmp(name, mappings[i].label)) + return &mappings[i]; + } + + return NULL; +} + int config_setprfx(const char *name, int cont, const char *str) { - size_t i; struct lineprefix *p; - struct mapping { - const char *label; - int id; - } mappings[] = { - {"text", LINE_TEXT}, - {"link", LINE_LINK}, - {"title1", LINE_TITLE_1}, - {"title2", LINE_TITLE_2}, - {"title3", LINE_TITLE_3}, - {"item", LINE_ITEM}, - {"quote", LINE_QUOTE}, - {"pre.start", LINE_PRE_START}, - {"pre", LINE_PRE_CONTENT}, - {"pre.end", LINE_PRE_END}, - }; + struct mapping *m; if (!has_prefix(name, "line.")) return 0; name += 5; - for (i = 0; i < sizeof(mappings)/sizeof(mappings[0]); ++i) { - if (!strcmp(name, mappings[i].label)) { - name += strlen(mappings[i].label); - p = &line_prefixes[mappings[i].id]; + if ((m = mapping_by_name(name)) == NULL) + return 0; - if (cont) - p->prfx2 = str; - else - p->prfx1 = str; + p = &line_prefixes[m->linetype]; - return 1; - } - } + if (cont) + p->prfx2 = str; + else + p->prfx1 = str; - return 0; + return 1; } int @@ -136,3 +166,59 @@ config_setvars(const char *var, char *val) return 0; return 1; } + +int +config_setcolor(const char *name, int prfx, int bg, int color) +{ + struct mapping *m; + struct lineface_descr *d; + + if (!has_prefix(name, "line.")) + return 0; + name += 5; + + if ((m = mapping_by_name(name)) == NULL) + return 0; + + d = &linefaces_descr[m->linetype]; + + if (prfx) { + d->prfx_used = 1; + if (bg) + d->prfx_bg = color; + else + d->prfx_fg = color; + } else { + d->used = 1; + if (bg) + d->bg = color; + else + d->fg = color; + } + + return 1; +} + +void +config_apply_colors(void) +{ + size_t i, len; + struct lineface_descr *d; + struct line_face *f; + + len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]); + for (i = 0; i < len; ++i) { + d = &linefaces_descr[i]; + f = &line_faces[i]; + + if (d->prfx_used) { + init_pair(d->prfx_pair, d->prfx_fg, d->prfx_bg); + f->prefix_prop = COLOR_PAIR(d->prfx_pair); + } + + if (d->used) { + init_pair(d->pair, d->fg, d->bg); + f->text_prop = COLOR_PAIR(d->pair); + } + } +} blob - e4ffbe003932bced6230759deadea7ce6a293965 blob + fdf77d8b09ba20ee67de5e5c8f2fc2325a7ab835 --- parse.y +++ parse.y @@ -22,8 +22,10 @@ #include "telescope.h" +#include #include #include +#include #include #include #include @@ -40,6 +42,8 @@ typedef struct { #define YYSTYPE yystype static char *current_style; +static int current_bg, current_fg; + static const char *path; FILE *yyfp; @@ -51,6 +55,8 @@ static int yylex(void); static void setprfx(int, const char *); static void setvari(char *, int); static void setvars(char *, char *); +static int colorname(const char *); +static void setcolor(int, int, const char *); %} @@ -70,7 +76,10 @@ grammar : /* empty */ ; rule : set - | style { free(current_style); current_style = NULL; } + | style { + free(current_style); + current_style = NULL; + } | bind | unbind ; @@ -79,19 +88,26 @@ set : TSET TSTRING '=' TSTRING { setvars($2, $4); } | TSET TSTRING '=' TNUMBER { setvari($2, $4); } ; -style : TSTYLE TSTRING { current_style = $2; } styleopt - | TSTYLE TSTRING { current_style = $2; } '{' styleopts '}' - ; +style : TSTYLE TSTRING { current_style = $2; } stylespec ; +stylespec : styleopt | '{' styleopts '}' ; styleopts : /* empty */ | styleopts '\n' | styleopts styleopt '\n' ; -styleopt : TPRFX TSTRING { setprfx(0, $2); } - | TCONT TSTRING { setprfx(1, $2); } - | TBG TSTRING { printf("style background setted to \"%s\"\n", $2); } - | TFG TSTRING { printf("style foreground setted to \"%s\"\n", $2); } +styleopt : TPRFX TSTRING { setprfx(0, $2); } + | TCONT TSTRING { setprfx(1, $2); } + | TBG TSTRING { + setcolor(0, 1, $2); + setcolor(1, 1, $2); + free($2); + } + | TFG TSTRING { + setcolor(0, 0, $2); + setcolor(1, 0, $2); + free($2); + } | TATTR TBOLD { printf("style attr setted to bold\n"); } | TATTR TUNDERLINE { printf("style attr setted to underline\n"); } ; @@ -125,8 +141,8 @@ static struct keyword { { "style", TSTYLE }, { "prefix", TPRFX }, { "cont", TCONT }, - { "background", TBG }, - { "foreground", TFG }, + { "bg", TBG }, + { "fg", TFG }, { "attr", TATTR }, { "bold", TBOLD }, { "underline", TUNDERLINE }, @@ -273,10 +289,7 @@ eof: static void setprfx(int cont, const char *name) { - if (current_style == NULL) { - warnx("current_style = NULL!"); - abort(); - } + assert(current_style != NULL); if (!config_setprfx(current_style, cont, name)) yyerror("invalid style %s", name); @@ -300,11 +313,60 @@ setvars(char *var, char *val) var, val); free(var); +} + +static int +colorname(const char *name) +{ + struct { + const char *name; + short val; + } *i, colors[] = { + { "default", 0 }, + { "black", COLOR_BLACK }, + { "red", COLOR_RED }, + { "green", COLOR_GREEN }, + { "yellow", COLOR_YELLOW }, + { "blue", COLOR_BLUE }, + { "magenta", COLOR_MAGENTA }, + { "cyan", COLOR_CYAN }, + { "white", COLOR_WHITE }, + { NULL, 0 }, + }; + + for (i = colors; i->name != NULL; ++i) { + if (!strcmp(i->name, name)) + return i->val; + } + + yyerror("unknown color name \"%s\"", name); + return -1; } void +setcolor(int prfx, int bg, const char *color) +{ + int c; + + assert(current_style != NULL); + + if ((c = colorname(color)) == -1) + return; + + if (!config_setcolor(current_style, prfx, bg, c)) + yyerror("can't set color \"%s\" for %s", color, + current_style); +} + +void parseconfig(const char *filename, int fonf) { + /* + char c; + printf("proceed?\n"); + read(0, &c, 1); + */ + if ((yyfp = fopen(filename, "r")) == NULL) { if (fonf) err(1, "%s", filename); blob - 68e9606c8f93065480a4908e4b14d30d00257f34 blob + 90f4104950dd1220eefcc8e57d8720f7ee1c9212 --- telescope.h +++ telescope.h @@ -248,6 +248,8 @@ struct keymap { int config_setprfx(const char *, int, const char *); int config_setvari(const char *, int); int config_setvars(const char *, char *); +int config_setcolor(const char *, int, int, int); +void config_apply_colors(void); /* fs.c */ int fs_init(void); @@ -322,6 +324,29 @@ extern int body_lines; extern int body_cols; extern int in_minibuffer; +enum { + PAIR_TEXT = 1, + PAIR_TEXT_PRFX, + PAIR_LINK, + PAIR_LINK_PRFX, + PAIR_TITLE_1, + PAIR_TITLE_1_PRFX, + PAIR_TITLE_2, + PAIR_TITLE_2_PRFX, + PAIR_TITLE_3, + PAIR_TITLE_3_PRFX, + PAIR_ITEM, + PAIR_ITEM_PRFX, + PAIR_QUOTE, + PAIR_QUOTE_PRFX, + PAIR_PRE_START, + PAIR_PRE_START_PRFX, + PAIR_PRE, + PAIR_PRE_PRFX, + PAIR_PRE_END, + PAIR_PRE_END_PRFX, +}; + struct thiskey { short meta; int key; blob - d0a37bd5a5cfc08f94752fec9eee724684788010 blob + cb176f8ca7286abb387c26d88054040ccb26bcba --- ui.c +++ ui.c @@ -1320,6 +1320,9 @@ ui_init(int argc, char * const *argv) const char *url = NEW_TAB_URL; int ch, configtest = 0, fonf = 0; + if (getenv("NO_COLOR") != NULL) + enable_colors = 0; + strlcpy(path, getenv("HOME"), sizeof(path)); strlcat(path, "/.telescope/config", sizeof(path)); @@ -1374,10 +1377,27 @@ ui_init(int argc, char * const *argv) current_map = &global_map; load_default_keys(); + /* { */ + /* char c; */ + /* printf("proceed?"); */ + /* read(0, &c, 1); */ + /* } */ + initscr(); + + if (enable_colors) { + if (has_colors()) { + start_color(); + config_apply_colors(); + } else { + printf("has_color failed\n"); + enable_colors = 0; + } + } else + printf("color disabled\n"); + raw(); noecho(); - nonl(); intrflush(stdscr, FALSE);