Skip to content

Commit c707ded

Browse files
ttaylorrgitster
authored andcommitted
grep.c: extract show_line_header()
The grep code invokes show_line() to display the contents of a matched or context line in its output. Part of this execution is to print a line header that includes information such as the kind, the line- and column-number and etc. of that match. To prepare for the addition of an option to print only the matching component(s) of a non-context line, we must prepare for the possibility that a single line may contain multiple matching parts, and thus will need multiple headers printed for a single line. Extracting show_line_header allows us to do just that. In the subsequent commit, it will be used within the colorization loop to print out only the matching parts of a line, optionally with LFs delimiting sub-matches. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 240cf2a commit c707ded

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

grep.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,26 +1410,9 @@ static int next_match(struct grep_opt *opt, char *bol, char *eol,
14101410
return hit;
14111411
}
14121412

1413-
static void show_line(struct grep_opt *opt, char *bol, char *eol,
1414-
const char *name, unsigned lno, ssize_t cno, char sign)
1413+
static void show_line_header(struct grep_opt *opt, const char *name,
1414+
unsigned lno, ssize_t cno, char sign)
14151415
{
1416-
int rest = eol - bol;
1417-
const char *match_color, *line_color = NULL;
1418-
1419-
if (opt->file_break && opt->last_shown == 0) {
1420-
if (opt->show_hunk_mark)
1421-
opt->output(opt, "\n", 1);
1422-
} else if (opt->pre_context || opt->post_context || opt->funcbody) {
1423-
if (opt->last_shown == 0) {
1424-
if (opt->show_hunk_mark) {
1425-
output_color(opt, "--", 2, opt->color_sep);
1426-
opt->output(opt, "\n", 1);
1427-
}
1428-
} else if (lno > opt->last_shown + 1) {
1429-
output_color(opt, "--", 2, opt->color_sep);
1430-
opt->output(opt, "\n", 1);
1431-
}
1432-
}
14331416
if (opt->heading && opt->last_shown == 0) {
14341417
output_color(opt, name, strlen(name), opt->color_filename);
14351418
opt->output(opt, "\n", 1);
@@ -1457,6 +1440,29 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
14571440
output_color(opt, buf, strlen(buf), opt->color_columnno);
14581441
output_sep(opt, sign);
14591442
}
1443+
}
1444+
1445+
static void show_line(struct grep_opt *opt, char *bol, char *eol,
1446+
const char *name, unsigned lno, ssize_t cno, char sign)
1447+
{
1448+
int rest = eol - bol;
1449+
const char *match_color, *line_color = NULL;
1450+
1451+
if (opt->file_break && opt->last_shown == 0) {
1452+
if (opt->show_hunk_mark)
1453+
opt->output(opt, "\n", 1);
1454+
} else if (opt->pre_context || opt->post_context || opt->funcbody) {
1455+
if (opt->last_shown == 0) {
1456+
if (opt->show_hunk_mark) {
1457+
output_color(opt, "--", 2, opt->color_sep);
1458+
opt->output(opt, "\n", 1);
1459+
}
1460+
} else if (lno > opt->last_shown + 1) {
1461+
output_color(opt, "--", 2, opt->color_sep);
1462+
opt->output(opt, "\n", 1);
1463+
}
1464+
}
1465+
show_line_header(opt, name, lno, cno, sign);
14601466
if (opt->color) {
14611467
regmatch_t match;
14621468
enum grep_context ctx = GREP_CONTEXT_BODY;

0 commit comments

Comments
 (0)