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 <zbyszek@in.waw.pl> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Zbigniew Jędrzejewski-Szmek committed
Feb 12, 2012 at 15:16 UTC
ec7ff5ba272b565ed093a98dc13dd5cd26aeac92
3 files changed
+16
-15
builtin/blame.c
+3
-15
@@ -1828,18 +1828,6 @@ static int read_ancestry(const char *graft_file)
1828
return 0;
1829
}
1830
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
-
1831
/*
1832
* How many columns do we need to show line numbers, authors,
1833
* and filenames?
@@ -1880,9 +1868,9 @@ static void find_alignment(struct scoreboard *sb, int *option)
1868
if (largest_score < ent_score(sb, e))
1869
largest_score = ent_score(sb, e);
1870
}
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);
1874
}
1875
1876
/*
cache.h
+1
@@ -1176,6 +1176,7 @@ extern void setup_pager(void);
1176
extern const char *pager_program;
1177
extern int pager_in_use(void);
1178
extern int pager_use_color;
1179
+extern int decimal_width(int);
1180
1181
extern const char *editor_program;
1182
extern const char *askpass_program;
pager.c
+12
@@ -110,3 +110,15 @@ int pager_in_use(void)
110
env = getenv("GIT_PAGER_IN_USE");
111
return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
112
}
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
+}