blob: cefd37d9f77a0c485d0cec720e694075b01c7b06 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#!/bin/sh
set -eu
usage() {
cat <<-'EOF'
Usage:
highlight -n LINE_NUMBER -e PATTERN -f FILENAME
highlight -h
EOF
}
help() {
cat <<-'EOF'
Options:
-n LINE_NUMBER the specific line number to highligh
-e PATTERN the pattern to highlight all over the file
-f FILENAME the file to be processed
-h, --help show this message
Highlight FILENAME:
a) with an ANSI background color on LINE_NUMBER;
b) with an ANSI foreground color on every occurrence of PATTERN.
Useful as a tool for development workflow, when one wants to
show a specific matched line, and at the same time visualize all
other ocurrences of PATTERN.
Examples:
Highlight the line 10 of Makefile, and all occurences of "lisp":
$ highlight -n10 -e 'lisp' -f Makefile
EOF
}
for flag in "$@"; do
case "$flag" in
(--)
break
;;
(--help)
usage
help
exit
;;
(*)
;;
esac
done
while getopts 'n:e:f:h' flag; do
case "$flag" in
(n)
LINE_NUMBER="$OPTARG"
;;
(e)
PATTERN="$OPTARG"
;;
(f)
FILENAME="$OPTARG"
;;
(h)
usage
help
exit
;;
(*)
usage >&2
exit 2
;;
esac
done
shift $((OPTIND - 1))
eval "$(assert-arg -- "${LINE_NUMBER:-}" '-n LINE_NUMBER')"
eval "$(assert-arg -- "${PATTERN:-}" '-e PATTERN')"
eval "$(assert-arg -- "${FILENAME:-}" '-f FILENAME')"
graybg="$(printf '\033[7;2;40m')"
end="$(printf '\033[0m')"
grep -E --color=always -e ^ -e "$PATTERN" "$FILENAME" |
perl -pe "s/\x1b\[m/${end}${graybg}/g if $. == $LINE_NUMBER" |
sed "${LINE_NUMBER}s|^\(.*\)\$|${graybg}\\1${end}|"
|