From 12c9cbfb54b1e0a9b79ba23b5197f92ba881c40a Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Tue, 22 Apr 2008 14:08:34 +0200 Subject: [PATCH] Introduce draw_field() helper for drawing main and blame fields It will draw spaces if the passed string is NULL. --- tig.c | 59 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/tig.c b/tig.c index d9bd9da..7144fdb 100644 --- a/tig.c +++ b/tig.c @@ -1515,6 +1515,24 @@ draw_text(struct view *view, enum line_type type, const char *string, return len; } +static int +draw_space(struct view *view, enum line_type type, int max, int spaces) +{ + static char space[] = " "; + int col = 0; + + spaces = MIN(max, spaces); + + while (spaces > 0) { + int len = MIN(spaces, sizeof(space) - 1); + + col += draw_text(view, type, space, spaces, FALSE); + spaces -= len; + } + + return col; +} + static int draw_lineno(struct view *view, unsigned int lineno, int max) { @@ -1576,30 +1594,33 @@ draw_graphic(struct view *view, enum line_type type, chtype graphic[], size_t si return col; } +static int +draw_field(struct view *view, enum line_type type, char *text, int len, int max_len, bool trim) +{ + int max = MIN(max_len, len); + int col; + + if (text) + col = draw_text(view, type, text, max - 1, trim); + else + col = draw_space(view, type, max - 1, max - 1); + + col += draw_space(view, LINE_DEFAULT, max - col, max - col); + return col; +} + static int draw_date(struct view *view, struct tm *time, int max) { char buf[DATE_COLS]; - int col; + char *date; int timelen = 0; - if (max > DATE_COLS) - max = DATE_COLS; if (time) timelen = strftime(buf, sizeof(buf), DATE_FORMAT, time); - if (!timelen) { - memset(buf, ' ', sizeof(buf) - 1); - buf[sizeof(buf) - 1] = 0; - } + date = timelen ? buf : NULL; - col = draw_text(view, LINE_DATE, buf, max, FALSE); - if (col < max) { - set_view_attr(view, LINE_DEFAULT); - waddch(view->win, ' '); - col++; - } - - return col; + return draw_field(view, LINE_DATE, date, DATE_COLS, max, FALSE); } static bool @@ -3656,15 +3677,11 @@ blame_draw(struct view *view, struct line *line, unsigned int lineno) } { - int max = MIN(ID_COLS - 1, view->width - col); + int max = view->width - col; - set_view_attr(view, LINE_BLAME_ID); - if (id) - draw_text(view, LINE_BLAME_ID, id, max, FALSE); - col += ID_COLS; + col += draw_field(view, LINE_BLAME_ID, id, ID_COLS, max, FALSE); if (col >= view->width) return TRUE; - wmove(view->win, lineno, col); } col += draw_lineno(view, lineno, view->width - col); -- 2.30.2