Skip to content

Commit ec7ff5b

Browse files
keszybzgitster
authored andcommitted
make lineno_width() from blame reusable for others
builtin/blame.c has a helper function to compute how many columns we need to show a line-number, whose implementation is reusable as a more generic helper function to count the number of columns necessary to show any cardinal number. Rename it to decimal_width(), move it to pager.c and export it for use by future callers. Signed-off-by: Zbigniew Jędrzejewski-Szmek <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 58d4203 commit ec7ff5b

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

builtin/blame.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,18 +1828,6 @@ static int read_ancestry(const char *graft_file)
18281828
return 0;
18291829
}
18301830

1831-
/*
1832-
* How many columns do we need to show line numbers in decimal?
1833-
*/
1834-
static int lineno_width(int lines)
1835-
{
1836-
int i, width;
1837-
1838-
for (width = 1, i = 10; i <= lines; width++)
1839-
i *= 10;
1840-
return width;
1841-
}
1842-
18431831
/*
18441832
* How many columns do we need to show line numbers, authors,
18451833
* and filenames?
@@ -1880,9 +1868,9 @@ static void find_alignment(struct scoreboard *sb, int *option)
18801868
if (largest_score < ent_score(sb, e))
18811869
largest_score = ent_score(sb, e);
18821870
}
1883-
max_orig_digits = lineno_width(longest_src_lines);
1884-
max_digits = lineno_width(longest_dst_lines);
1885-
max_score_digits = lineno_width(largest_score);
1871+
max_orig_digits = decimal_width(longest_src_lines);
1872+
max_digits = decimal_width(longest_dst_lines);
1873+
max_score_digits = decimal_width(largest_score);
18861874
}
18871875

18881876
/*

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,7 @@ extern void setup_pager(void);
11761176
extern const char *pager_program;
11771177
extern int pager_in_use(void);
11781178
extern int pager_use_color;
1179+
extern int decimal_width(int);
11791180

11801181
extern const char *editor_program;
11811182
extern const char *askpass_program;

pager.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,15 @@ int pager_in_use(void)
110110
env = getenv("GIT_PAGER_IN_USE");
111111
return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
112112
}
113+
114+
/*
115+
* How many columns do we need to show this number in decimal?
116+
*/
117+
int decimal_width(int number)
118+
{
119+
int i, width;
120+
121+
for (width = 1, i = 10; i <= number; width++)
122+
i *= 10;
123+
return width;
124+
}

0 commit comments

Comments
 (0)