-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight.sh
More file actions
executable file
·38 lines (31 loc) · 1.21 KB
/
highlight.sh
File metadata and controls
executable file
·38 lines (31 loc) · 1.21 KB
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
#!/usr/bin/env bash
#Modified from a response by akovia on Stack Overflow https://stackoverflow.com/questions/34376884/highlight-string-differences
# Using stdin input, outputs each char. on its own line, with actual newlines
# in the input represented as literal '\n'.
toSingleCharLines() {
sed 's/\(.\)/\1\'$'\n''/g; s/\n$/\'$'\n''\\n/'
}
# Using stdin input, reassembles a string split into 1-character-per-line output
# by toSingleCharLines().
fromSingleCharLines() {
awk '$0=="\\n" { printf "\n"; next} { printf "%s", $0 }'
}
# Prints a colored string read from stdin by interpreting embedded color references
# such as '${RED}'.
printColored() {
local str=$(</dev/stdin)
local RED="$(tput setaf 1)" CYA="$(tput setaf 6)" RST="$(tput sgr0)"
str=${str//'${RED}'/${RED}}
str=${str//'${CYA}'/${CYA}}
str=${str//'${RST}'/${RST}}
printf '%s\n' "$str"
}
# The non-ASCII input string.
strOrg=$1
# Create its ASCII-chars.-only transliteration.
strTransLit=$2
# Print the ORIGINAL string with the characters that NEED transliteration
# highlighted in RED.
diff --changed-group-format='${RED}%=${RST}' \
<(toSingleCharLines <<<"$strOrg") <(toSingleCharLines <<<"$strTransLit") |
fromSingleCharLines | printColored