Commit df711b9cfd8deccd1a307613c1d2ffaefab2f438

Simplify line attribute handling

- Add set_view_attr() that wraps calls to wattrset() and does the right
thing for the selected line. For successive calls without a change of
the attribute nothing will be done.

- Make draw_text() take a line attribute since all callers were already
calling set_view_attr().

Note: all attribute updates will now also call wchgat() in addition to
wattrset().

Commit diff

tig.c

 
612612enum line_type {
613613#define LINE(type, line, fg, bg, attr) \
614614 LINE_##type
615 LINE_INFO
615 LINE_INFO,
616 LINE_NONE
616617#undef LINE
617618};
618619
13981398 size_t line_size; /* Total number of used lines */
13991399 unsigned int digits; /* Number of digits in the lines member. */
14001400
1401 /* Drawing */
1402 struct line *curline; /* Line currently being drawn. */
1403 enum line_type curtype; /* Attribute currently used for drawing. */
1404
14011405 /* Loading */
14021406 FILE *pipe;
14031407 time_t start_time;
14151415 /* Read one line; updates view->line. */
14161416 bool (*read)(struct view *view, char *data);
14171417 /* Draw one line; @lineno must be < view->height. */
1418 bool (*draw)(struct view *view, struct line *line, unsigned int lineno, bool selected);
1418 bool (*draw)(struct view *view, struct line *line, unsigned int lineno);
14191419 /* Depending on view handle a special requests. */
14201420 enum request (*request)(struct view *view, enum request request, struct line *line);
14211421 /* Search for regex in a line. */
14711471 /* LINE_GRAPHIC_VLINE: */ '|'
14721472};
14731473
1474static inline void
1475set_view_attr(struct view *view, enum line_type type)
1476{
1477 if (!view->curline->selected && view->curtype != type) {
1478 wattrset(view->win, get_line_attr(type));
1479 wchgat(view->win, -1, 0, type, NULL);
1480 view->curtype = type;
1481 }
1482}
1483
14741484static int
1475draw_text(struct view *view, const char *string, int max_len,
1476 bool use_tilde, bool selected)
1485draw_text(struct view *view, enum line_type type, const char *string,
1486 int max_len, bool use_tilde)
14771487{
14781488 int len = 0;
14791489 int trimmed = FALSE;
15041504 }
15051505 }
15061506
1507 set_view_attr(view, type);
15071508 waddnstr(view->win, string, len);
15081509 if (trimmed && use_tilde) {
1509 if (!selected)
1510 wattrset(view->win, get_line_attr(LINE_DELIMITER));
1510 set_view_attr(view, LINE_DELIMITER);
15111511 waddch(view->win, '~');
15121512 len++;
15131513 }
15161516}
15171517
15181518static int
1519draw_lineno(struct view *view, unsigned int lineno, int max, bool selected)
1519draw_lineno(struct view *view, unsigned int lineno, int max)
15201520{
15211521 static char fmt[] = "%1ld";
15221522 char number[10] = " ";
15381538 if (max < max_number)
15391539 max_number = max;
15401540
1541 if (!selected)
1542 wattrset(view->win, get_line_attr(LINE_LINE_NUMBER));
1543 col = draw_text(view, number, max_number, showtrimmed, selected);
1541 col = draw_text(view, LINE_LINE_NUMBER, number, max_number, showtrimmed);
15441542 if (col < max) {
1545 if (!selected)
1546 wattrset(view->win, A_NORMAL);
1543 set_view_attr(view, LINE_DEFAULT);
15471544 waddch(view->win, line_graphics[LINE_GRAPHIC_VLINE]);
15481545 col++;
15491546 }
15531553}
15541554
15551555static int
1556draw_date(struct view *view, struct tm *time, int max, bool selected)
1556draw_date(struct view *view, struct tm *time, int max)
15571557{
15581558 char buf[DATE_COLS];
15591559 int col;
15681568 buf[sizeof(buf) - 1] = 0;
15691569 }
15701570
1571 if (!selected)
1572 wattrset(view->win, get_line_attr(LINE_DATE));
1573 col = draw_text(view, buf, max, FALSE, selected);
1571 col = draw_text(view, LINE_DATE, buf, max, FALSE);
15741572 if (col < max) {
1575 if (!selected)
1576 wattrset(view->win, get_line_attr(LINE_DEFAULT));
1573 set_view_attr(view, LINE_DEFAULT);
15771574 waddch(view->win, ' ');
15781575 col++;
15791576 }
15931593 line = &view->line[view->offset + lineno];
15941594
15951595 wmove(view->win, lineno, 0);
1596 view->curline = line;
1597 view->curtype = LINE_NONE;
1598 line->selected = FALSE;
15961599
15971600 if (selected) {
1601 set_view_attr(view, LINE_CURSOR);
15981602 line->selected = TRUE;
15991603 view->ops->select(view, line);
1600 wchgat(view->win, -1, 0, LINE_CURSOR, NULL);
1601 wattrset(view->win, get_line_attr(LINE_CURSOR));
16021604 } else if (line->selected) {
1603 line->selected = FALSE;
16041605 wclrtoeol(view->win);
16051606 }
16061607
16071608 scrollok(view->win, FALSE);
1608 draw_ok = view->ops->draw(view, line, lineno, selected);
1609 draw_ok = view->ops->draw(view, line, lineno);
16091610 scrollok(view->win, TRUE);
16101611
16111612 return draw_ok;
28052805 */
28062806
28072807static bool
2808pager_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
2808pager_draw(struct view *view, struct line *line, unsigned int lineno)
28092809{
28102810 char *text = line->data;
28112811 int col = 0;
28122812
28132813 if (opt_line_number) {
2814 col += draw_lineno(view, lineno, view->width, selected);
2814 col += draw_lineno(view, lineno, view->width);
28152815 if (col >= view->width)
28162816 return TRUE;
28172817 }
28182818
2819 if (!selected)
2820 wattrset(view->win, get_line_attr(line->type));
2821
2822 draw_text(view, text, view->width - col, TRUE, selected);
2819 draw_text(view, line->type, text, view->width - col, TRUE);
28232820 return TRUE;
28242821}
28252822
36003600}
36013601
36023602static bool
3603blame_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
3603blame_draw(struct view *view, struct line *line, unsigned int lineno)
36043604{
36053605 struct blame *blame = line->data;
36063606 int col = 0;
36093609 struct tm *time = blame->commit && *blame->commit->filename
36103610 ? &blame->commit->time : NULL;
36113611
3612 col += draw_date(view, time, view->width, selected);
3612 col += draw_date(view, time, view->width);
36133613 if (col >= view->width)
36143614 return TRUE;
36153615 }
36173617 if (opt_author) {
36183618 int max = MIN(AUTHOR_COLS - 1, view->width - col);
36193619
3620 if (!selected)
3621 wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
3620 set_view_attr(view, LINE_MAIN_AUTHOR);
36223621 if (blame->commit)
3623 draw_text(view, blame->commit->author, max, TRUE, selected);
3622 draw_text(view, LINE_MAIN_AUTHOR, blame->commit->author, max, TRUE);
36243623 col += AUTHOR_COLS;
36253624 if (col >= view->width)
36263625 return TRUE;
36293629 {
36303630 int max = MIN(ID_COLS - 1, view->width - col);
36313631
3632 if (!selected)
3633 wattrset(view->win, get_line_attr(LINE_BLAME_ID));
3632 set_view_attr(view, LINE_BLAME_ID);
36343633 if (blame->commit)
3635 draw_text(view, blame->commit->id, max, FALSE, -1);
3634 draw_text(view, LINE_BLAME_ID, blame->commit->id, max, FALSE);
36363635 col += ID_COLS;
36373636 if (col >= view->width)
36383637 return TRUE;
36393638 wmove(view->win, lineno, col);
36403639 }
36413640
3642 col += draw_lineno(view, lineno, view->width - col, selected);
3641 col += draw_lineno(view, lineno, view->width - col);
36433642 if (col >= view->width)
36443643 return TRUE;
36453644
3646 col += draw_text(view, blame->text, view->width - col, TRUE, selected);
3645 col += draw_text(view, LINE_DEFAULT, blame->text, view->width - col, TRUE);
36473646 return TRUE;
36483647}
36493648
40014001}
40024002
40034003static bool
4004status_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
4004status_draw(struct view *view, struct line *line, unsigned int lineno)
40054005{
40064006 struct status *status = line->data;
4007 enum line_type type;
40074008 char *text;
40084009 int col = 0;
40094010
4010 if (selected) {
4011 /* No attributes. */
4012
4013 } else if (line->type == LINE_STAT_HEAD) {
4014 wattrset(view->win, get_line_attr(LINE_STAT_HEAD));
4015 wchgat(view->win, -1, 0, LINE_STAT_HEAD, NULL);
4016
4017 } else if (!status && line->type != LINE_STAT_NONE) {
4018 wattrset(view->win, get_line_attr(LINE_STAT_SECTION));
4019 wchgat(view->win, -1, 0, LINE_STAT_SECTION, NULL);
4020
4021 } else {
4022 wattrset(view->win, get_line_attr(line->type));
4023 }
4024
40254011 if (!status) {
40264012 switch (line->type) {
40274013 case LINE_STAT_STAGED:
4014 type = LINE_STAT_SECTION;
40284015 text = "Changes to be committed:";
40294016 break;
40304017
40314018 case LINE_STAT_UNSTAGED:
4019 type = LINE_STAT_SECTION;
40324020 text = "Changed but not updated:";
40334021 break;
40344022
40354023 case LINE_STAT_UNTRACKED:
4024 type = LINE_STAT_SECTION;
40364025 text = "Untracked files:";
40374026 break;
40384027
40394028 case LINE_STAT_NONE:
4029 type = LINE_DEFAULT;
40404030 text = " (no files)";
40414031 break;
40424032
40434033 case LINE_STAT_HEAD:
4034 type = LINE_STAT_HEAD;
40444035 text = status_onbranch;
40454036 break;
40464037
40414041 } else {
40424042 char buf[] = { status->status, ' ', ' ', ' ', 0 };
40434043
4044 col += draw_text(view, buf, view->width, TRUE, selected);
4045 if (!selected)
4046 wattrset(view->win, A_NORMAL);
4044 col += draw_text(view, line->type, buf, view->width, TRUE);
4045 type = LINE_DEFAULT;
40474046 text = status->new.name;
40484047 }
40494048
4050 draw_text(view, text, view->width - col, TRUE, selected);
4049 draw_text(view, type, text, view->width - col, TRUE);
40514050 return TRUE;
40524051}
40534052
48254825 */
48264826
48274827static bool
4828main_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
4828main_draw(struct view *view, struct line *line, unsigned int lineno)
48294829{
48304830 struct commit *commit = line->data;
4831 enum line_type type;
48324831 int col = 0;
48334832
48344833 if (!*commit->author)
48354834 return FALSE;
48364835
4837 if (selected) {
4838 type = LINE_CURSOR;
4839 } else {
4840 type = LINE_MAIN_COMMIT;
4841 }
4842
48434836 if (opt_date) {
4844 col += draw_date(view, &commit->time, view->width, selected);
4837 col += draw_date(view, &commit->time, view->width);
48454838 if (col >= view->width)
48464839 return TRUE;
48474840 }
4848 if (type != LINE_CURSOR)
4849 wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
48504841
48514842 if (opt_author) {
48524843 int max_len;
48454845 max_len = view->width - col;
48464846 if (max_len > AUTHOR_COLS - 1)
48474847 max_len = AUTHOR_COLS - 1;
4848 draw_text(view, commit->author, max_len, TRUE, selected);
4848 draw_text(view, LINE_MAIN_AUTHOR, commit->author, max_len, TRUE);
48494849 col += AUTHOR_COLS;
48504850 if (col >= view->width)
48514851 return TRUE;
48554855 size_t graph_size = view->width - col;
48564856 size_t i;
48574857
4858 if (type != LINE_CURSOR)
4859 wattrset(view->win, get_line_attr(LINE_MAIN_REVGRAPH));
4858 set_view_attr(view, LINE_MAIN_REVGRAPH);
48604859 wmove(view->win, lineno, col);
48614860 if (graph_size > commit->graph_size)
48624861 graph_size = commit->graph_size;
48694869 return TRUE;
48704870 waddch(view->win, ' ');
48714871 }
4872 if (type != LINE_CURSOR)
4873 wattrset(view->win, A_NORMAL);
48744872
4873 set_view_attr(view, LINE_DEFAULT);
48754874 wmove(view->win, lineno, col);
48764875
48774876 if (opt_show_refs && commit->refs) {
48784877 size_t i = 0;
48794878
48804879 do {
4881 if (type == LINE_CURSOR)
4882 ;
4883 else if (commit->refs[i]->head)
4884 wattrset(view->win, get_line_attr(LINE_MAIN_HEAD));
4880 enum line_type type;
4881
4882 if (commit->refs[i]->head)
4883 type = LINE_MAIN_HEAD;
48854884 else if (commit->refs[i]->ltag)
4886 wattrset(view->win, get_line_attr(LINE_MAIN_LOCAL_TAG));
4885 type = LINE_MAIN_LOCAL_TAG;
48874886 else if (commit->refs[i]->tag)
4888 wattrset(view->win, get_line_attr(LINE_MAIN_TAG));
4887 type = LINE_MAIN_TAG;
48894888 else if (commit->refs[i]->tracked)
4890 wattrset(view->win, get_line_attr(LINE_MAIN_TRACKED));
4889 type = LINE_MAIN_TRACKED;
48914890 else if (commit->refs[i]->remote)
4892 wattrset(view->win, get_line_attr(LINE_MAIN_REMOTE));
4891 type = LINE_MAIN_REMOTE;
48934892 else
4894 wattrset(view->win, get_line_attr(LINE_MAIN_REF));
4895
4896 col += draw_text(view, "[", view->width - col, TRUE, selected);
4897 col += draw_text(view, commit->refs[i]->name, view->width - col,
4898 TRUE, selected);
4899 col += draw_text(view, "]", view->width - col, TRUE, selected);
4900 if (type != LINE_CURSOR)
4901 wattrset(view->win, A_NORMAL);
4902 col += draw_text(view, " ", view->width - col, TRUE, selected);
4893 type = LINE_MAIN_REF;
4894
4895 col += draw_text(view, type, "[", view->width - col, TRUE);
4896 col += draw_text(view, type, commit->refs[i]->name, view->width - col, TRUE);
4897 col += draw_text(view, type, "]", view->width - col, TRUE);
4898
4899 col += draw_text(view, LINE_DEFAULT, " ", view->width - col, TRUE);
49034900 if (col >= view->width)
49044901 return TRUE;
49054902 } while (commit->refs[i++]->next);
49064903 }
49074904
4908 if (type != LINE_CURSOR)
4909 wattrset(view->win, get_line_attr(type));
4910
4911 draw_text(view, commit->title, view->width - col, TRUE, selected);
4905 draw_text(view, LINE_DEFAULT, commit->title, view->width - col, TRUE);
49124906 return TRUE;
49134907}
49144908
toggle raw diff