Commit 4d5f2434495aa105ba5bbe6e68fbab35e2b1cf02

Refactor management of the current draw column and max draw width

Do it in the draw_* functions instead of all the view draw functions.

Commit diff

tig.c

 
14011401 /* Drawing */
14021402 struct line *curline; /* Line currently being drawn. */
14031403 enum line_type curtype; /* Attribute currently used for drawing. */
1404 unsigned long col; /* Column when drawing. */
14041405
14051406 /* Loading */
14061407 FILE *pipe;
14831483}
14841484
14851485static int
1486draw_text(struct view *view, enum line_type type, const char *string,
1487 int max_len, bool use_tilde)
1486draw_chars(struct view *view, enum line_type type, const char *string,
1487 int max_len, bool use_tilde)
14881488{
14891489 int len = 0;
14901490 int col = 0;
15281528 while (spaces > 0) {
15291529 int len = MIN(spaces, sizeof(space) - 1);
15301530
1531 col += draw_text(view, type, space, spaces, FALSE);
1531 col += draw_chars(view, type, space, spaces, FALSE);
15321532 spaces -= len;
15331533 }
15341534
15351535 return col;
15361536}
15371537
1538static int
1539draw_lineno(struct view *view, unsigned int lineno, int max)
1538static bool
1539draw_lineno(struct view *view, unsigned int lineno)
15401540{
1541 static char fmt[] = "%1ld";
1542 char number[10] = " ";
1541 char number[10];
15431542 int digits3 = view->digits < 3 ? 3 : view->digits;
15441543 int max_number = MIN(digits3, STRING_SIZE(number));
1545 bool showtrimmed = FALSE;
1544 int max = view->width - view->col;
15461545 int col;
15471546
1547 if (max < max_number)
1548 max_number = max;
1549
15481550 lineno += view->offset + 1;
15491551 if (lineno == 1 || (lineno % opt_num_interval) == 0) {
1552 static char fmt[] = "%1ld";
1553
15501554 if (view->digits <= 9)
15511555 fmt[1] = '0' + digits3;
15521556
15531557 if (!string_format(number, fmt, lineno))
15541558 number[0] = 0;
1555 showtrimmed = TRUE;
1559 col = draw_chars(view, LINE_LINE_NUMBER, number, max_number, TRUE);
1560 } else {
1561 col = draw_space(view, LINE_LINE_NUMBER, max_number, max_number);
15561562 }
15571563
1558 if (max < max_number)
1559 max_number = max;
1560
1561 col = draw_text(view, LINE_LINE_NUMBER, number, max_number, showtrimmed);
15621564 if (col < max) {
15631565 set_view_attr(view, LINE_DEFAULT);
15641566 waddch(view->win, line_graphics[LINE_GRAPHIC_VLINE]);
15651567 col++;
15661568 }
1567 if (col < max) {
1568 waddch(view->win, ' ');
1569 col++;
1570 }
15711569
1572 return col;
1570 if (col < max)
1571 col += draw_space(view, LINE_DEFAULT, max - col, 1);
1572 view->col += col;
1573
1574 return view->width - view->col <= 0;
15731575}
15741576
1575static int
1576draw_graphic(struct view *view, enum line_type type, chtype graphic[], size_t size, size_t max)
1577static bool
1578draw_text(struct view *view, enum line_type type, const char *string, bool trim)
1579{
1580 view->col += draw_chars(view, type, string, view->width - view->col, trim);
1581 return view->width - view->col <= 0;
1582}
1583
1584static bool
1585draw_graphic(struct view *view, enum line_type type, chtype graphic[], size_t size)
15771586{
1587 int max = view->width - view->col;
15781588 int i;
1579 int col;
15801589
15811590 if (max < size)
15821591 size = max;
15961596 for (i = 0; i < size; i++)
15971597 waddch(view->win, graphic[i]);
15981598
1599 col = size;
1599 view->col += size;
16001600 if (size < max) {
16011601 waddch(view->win, ' ');
1602 col++;
1602 view->col++;
16031603 }
16041604
1605 return col;
1605 return view->width - view->col <= 0;
16061606}
16071607
1608static int
1609draw_field(struct view *view, enum line_type type, char *text, int len, int max_len, bool trim)
1608static bool
1609draw_field(struct view *view, enum line_type type, char *text, int len, bool trim)
16101610{
1611 int max = MIN(max_len, len);
1611 int max = MIN(view->width - view->col, len);
16121612 int col;
16131613
16141614 if (text)
1615 col = draw_text(view, type, text, max - 1, trim);
1615 col = draw_chars(view, type, text, max - 1, trim);
16161616 else
16171617 col = draw_space(view, type, max - 1, max - 1);
16181618
1619 col += draw_space(view, LINE_DEFAULT, max - col, max - col);
1620 return col;
1619 view->col += col + draw_space(view, LINE_DEFAULT, max - col, max - col);
1620 return view->width - view->col <= 0;
16211621}
16221622
1623static int
1624draw_date(struct view *view, struct tm *time, int max)
1623static bool
1624draw_date(struct view *view, struct tm *time)
16251625{
16261626 char buf[DATE_COLS];
16271627 char *date;
16311631 timelen = strftime(buf, sizeof(buf), DATE_FORMAT, time);
16321632 date = timelen ? buf : NULL;
16331633
1634 return draw_field(view, LINE_DATE, date, DATE_COLS, max, FALSE);
1634 return draw_field(view, LINE_DATE, date, DATE_COLS, FALSE);
16351635}
16361636
16371637static bool
16491649 line = &view->line[view->offset + lineno];
16501650
16511651 wmove(view->win, lineno, 0);
1652 view->col = 0;
16521653 view->curline = line;
16531654 view->curtype = LINE_NONE;
16541655 line->selected = FALSE;
28652865pager_draw(struct view *view, struct line *line, unsigned int lineno)
28662866{
28672867 char *text = line->data;
2868 int col = 0;
28692868
2870 if (opt_line_number) {
2871 col += draw_lineno(view, lineno, view->width);
2872 if (col >= view->width)
2873 return TRUE;
2874 }
2869 if (opt_line_number && draw_lineno(view, lineno))
2870 return TRUE;
28752871
2876 draw_text(view, line->type, text, view->width - col, TRUE);
2872 draw_text(view, line->type, text, TRUE);
28772873 return TRUE;
28782874}
28792875
36583658 struct blame *blame = line->data;
36593659 struct tm *time = NULL;
36603660 char *id = NULL, *author = NULL;
3661 int col = 0;
36623661
36633662 if (blame->commit && *blame->commit->filename) {
36643663 id = blame->commit->id;
36653665 time = &blame->commit->time;
36663666 }
36673667
3668 if (opt_date) {
3669 col += draw_date(view, time, view->width);
3670 if (col >= view->width)
3671 return TRUE;
3672 }
3673
3674 if (opt_author) {
3675 int max = view->width - col;
3676
3677 col += draw_field(view, LINE_MAIN_AUTHOR, author, AUTHOR_COLS, max, TRUE);
3678 if (col >= view->width)
3679 return TRUE;
3680 }
3668 if (opt_date && draw_date(view, time))
3669 return TRUE;
36813670
3682 {
3683 int max = view->width - col;
3671 if (opt_author &&
3672 draw_field(view, LINE_MAIN_AUTHOR, author, AUTHOR_COLS, TRUE))
3673 return TRUE;
36843674
3685 col += draw_field(view, LINE_BLAME_ID, id, ID_COLS, max, FALSE);
3686 if (col >= view->width)
3687 return TRUE;
3688 }
3675 if (draw_field(view, LINE_BLAME_ID, id, ID_COLS, FALSE))
3676 return TRUE;
36893677
3690 col += draw_lineno(view, lineno, view->width - col);
3691 if (col >= view->width)
3678 if (draw_lineno(view, lineno))
36923679 return TRUE;
36933680
3694 col += draw_text(view, LINE_DEFAULT, blame->text, view->width - col, TRUE);
3681 draw_text(view, LINE_DEFAULT, blame->text, TRUE);
36953682 return TRUE;
36963683}
36973684
40424042 struct status *status = line->data;
40434043 enum line_type type;
40444044 char *text;
4045 int col = 0;
40464045
40474046 if (!status) {
40484047 switch (line->type) {
40764076 } else {
40774077 char buf[] = { status->status, ' ', ' ', ' ', 0 };
40784078
4079 col += draw_text(view, line->type, buf, view->width, TRUE);
4079 if (draw_text(view, line->type, buf, TRUE))
4080 return TRUE;
40804081 type = LINE_DEFAULT;
40814082 text = status->new.name;
40824083 }
40834084
4084 draw_text(view, type, text, view->width - col, TRUE);
4085 draw_text(view, type, text, TRUE);
40854086 return TRUE;
40864087}
40874088
48644864main_draw(struct view *view, struct line *line, unsigned int lineno)
48654865{
48664866 struct commit *commit = line->data;
4867 int col = 0;
48684867
48694868 if (!*commit->author)
48704869 return FALSE;
48714870
4872 if (opt_date) {
4873 col += draw_date(view, &commit->time, view->width);
4874 if (col >= view->width)
4875 return TRUE;
4876 }
4877
4878 if (opt_author) {
4879 int max = view->width - col;
4871 if (opt_date && draw_date(view, &commit->time))
4872 return TRUE;
48804873
4881 col += draw_field(view, LINE_MAIN_AUTHOR, commit->author, AUTHOR_COLS, max, TRUE);
4882 if (col >= view->width)
4883 return TRUE;
4884 }
4874 if (opt_author &&
4875 draw_field(view, LINE_MAIN_AUTHOR, commit->author, AUTHOR_COLS, TRUE))
4876 return TRUE;
48854877
4886 if (opt_rev_graph && commit->graph_size) {
4887 col += draw_graphic(view, LINE_MAIN_REVGRAPH,
4888 commit->graph, commit->graph_size,
4889 view->width - col);
4890 if (col >= view->width)
4891 return TRUE;
4892 }
4878 if (opt_rev_graph && commit->graph_size &&
4879 draw_graphic(view, LINE_MAIN_REVGRAPH, commit->graph, commit->graph_size))
4880 return TRUE;
48934881
48944882 if (opt_show_refs && commit->refs) {
48954883 size_t i = 0;
48984898 else
48994899 type = LINE_MAIN_REF;
49004900
4901 col += draw_text(view, type, "[", view->width - col, TRUE);
4902 col += draw_text(view, type, commit->refs[i]->name, view->width - col, TRUE);
4903 col += draw_text(view, type, "]", view->width - col, TRUE);
4901 if (draw_text(view, type, "[", TRUE) ||
4902 draw_text(view, type, commit->refs[i]->name, TRUE) ||
4903 draw_text(view, type, "]", TRUE))
4904 return TRUE;
49044905
4905 col += draw_text(view, LINE_DEFAULT, " ", view->width - col, TRUE);
4906 if (col >= view->width)
4906 if (draw_text(view, LINE_DEFAULT, " ", TRUE))
49074907 return TRUE;
49084908 } while (commit->refs[i++]->next);
49094909 }
49104910
4911 draw_text(view, LINE_DEFAULT, commit->title, view->width - col, TRUE);
4911 draw_text(view, LINE_DEFAULT, commit->title, TRUE);
49124912 return TRUE;
49134913}
49144914
toggle raw diff