2016-05-02 21:15:29 +02:00
#!/usr/bin/env bash
2016-04-30 02:42:56 +02:00
CURRENT_DIR = " $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd ) "
2016-05-06 00:25:54 +02:00
2016-05-16 19:39:00 +02:00
MATCH_PARSER = "\([0-9]*\):\(.*\)"
2016-05-03 09:05:05 +02:00
2016-04-30 02:42:56 +02:00
HINTS = ( p o i u l k j t r e wj wt wr we ww wq wf wd ws wa qp qo qi qu ql qk qj qt qr qe qw qq qf qd qs qa fp fo fi fu fl fk fj ft fr fe fw fq ff fd fs fa dp do di du dl dk dj dt dr de dw dq df dd ds da sp so si su sl sk sj st sr se sw sq sf sd ss sa ap ao ai au al ak aj at ar ae aw aq af ad as aa)
match_lookup_table = ''
2016-05-06 00:25:54 +02:00
declare -A match_lookup_table
2016-04-30 02:42:56 +02:00
function get_hint( ) {
echo " ${ HINTS [ $1 ] } "
}
function highlight( ) {
printf "\033[1;33m%s\033[0m" " $1 "
}
function lookup_match( ) {
local input = $1
2016-05-06 00:25:54 +02:00
echo ${ match_lookup_table [ $input ] }
2016-04-30 02:42:56 +02:00
}
lines = ''
2016-05-05 23:44:09 +02:00
OLDIFS = $IFS
IFS =
2016-04-30 02:42:56 +02:00
while read -r line
do
lines += " $line \n "
done < /dev/stdin
2016-05-05 23:44:09 +02:00
IFS = $OLDIFS
2016-04-30 02:42:56 +02:00
2016-05-23 07:59:10 +02:00
# POSIX grep does linenumber on every line with both -o and -n flags set
normalize_grep_output = '
BEGIN {
previous_line_no = 0;
}
{
if ( $0 ~ /^[ 0-9] +:/ ) {
split( $0 , split_at_colon, ":" )
previous_line_no = split_at_colon[ 0]
print $0
} else {
printf "%d:%s\n" , previous_line_no, $0
}
}
'
matches = $( echo -e $lines | ( grep -oniE " $PATTERNS " 2> /dev/null) | awk $normalize_grep_output | sort -u)
2016-04-30 02:42:56 +02:00
output = " $lines "
i = 0
OLDIFS = $IFS
IFS = $( echo -en "\n\b" ) # wtf bash?
for match in $matches ; do
hint = $( get_hint $i )
2016-05-23 07:55:10 +02:00
linenumber = $( echo $match | sed " s! ${ MATCH_PARSER //! \\ ! } !\1! " )
text = $( echo $match | sed " s! ${ MATCH_PARSER //! \\ ! } !\2! " )
2016-05-06 00:25:54 +02:00
output = $( echo -ne " $output " | sed " ${ linenumber } s! ${ text //!/ \\ ! } ! $( highlight ${ text //!/ \\ ! } ) $( highlight " [ ${ hint //!/ \\ ! } ] " ) !g " )
match_lookup_table[ $hint ] = $text
2016-04-30 02:42:56 +02:00
i = $(( i + 1 ))
done
IFS = $OLDIFS
function print_hints( ) {
echo -ne " $output "
}