tmux-fingers/scripts/fingers.sh

98 lines
2.0 KiB
Bash
Raw Normal View History

2016-04-24 01:36:09 +02:00
#!/bin/bash
2016-04-26 21:44:18 +02:00
2016-04-30 01:26:43 +02:00
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $CURRENT_DIR/config.sh
source $CURRENT_DIR/actions.sh
2016-04-30 00:35:52 +02:00
2016-04-26 21:44:18 +02:00
#TODO move this out of here!
current_pane_id=$1
fingers_pane_id=$2
tmp_path=$3
2016-04-24 01:36:09 +02:00
ALPHABET=asdfqwertjkluiop
ALPHABET_SIZE=${#ALPHABET}
2016-04-30 01:38:49 +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)
2016-04-30 01:36:21 +02:00
BACKSPACE=$'\177'
2016-04-24 01:36:09 +02:00
2016-04-28 22:27:40 +02:00
function clear_screen() {
clear
tmux clearhist
}
2016-04-24 01:36:09 +02:00
function get_hint() {
echo ${HINTS[$1]}
}
function fancy() {
printf "\033[1;33m$1\033[0m"
}
2016-04-28 22:27:40 +02:00
clear_screen
2016-04-24 01:36:09 +02:00
lines=''
while read -r line
do
lines+="$line\n"
2016-04-26 21:44:18 +02:00
done < /dev/stdin
2016-04-24 01:36:09 +02:00
2016-04-30 00:35:52 +02:00
matches=`echo -e $lines | (grep -oniE "$PATTERNS" 2> /dev/null) | sort -u`
2016-04-24 01:36:09 +02:00
match_count=`echo "$matches" | wc -l`
output="$lines"
i=0
match_lookup=''
2016-04-26 21:44:18 +02:00
OLDIFS=$IFS
IFS=$(echo -en "\n\b") # wtf bash?
2016-04-24 01:36:09 +02:00
for match in $matches ; do
hint=`get_hint $i`
linenumber=`echo $match | cut -f1 -d:`
text=`echo $match | cut -f2 -d:`
2016-04-28 22:27:40 +02:00
output=`echo -ne "$output" | sed "${linenumber}s!$text!$(fancy $text) $(fancy "[$hint]")!g"`
2016-04-24 01:36:09 +02:00
match_lookup="$match_lookup\n$hint:$text"
i=$(($i+1))
done
2016-04-26 21:44:18 +02:00
IFS=$OLDIFS
2016-04-24 01:36:09 +02:00
2016-04-28 22:27:40 +02:00
echo -ne "$output"
2016-04-24 01:36:09 +02:00
2016-04-26 21:44:18 +02:00
function handle_exit() {
tmux swap-pane -s $current_pane_id -t $fingers_pane_id
tmux kill-pane -t $fingers_pane_id
rm -rf $tmp_path
2016-04-26 21:44:18 +02:00
}
trap "handle_exit" EXIT
2016-04-24 01:36:09 +02:00
input=''
2016-04-28 22:27:40 +02:00
while read -r -s -n1 char
2016-04-24 01:36:09 +02:00
do
2016-04-30 01:36:21 +02:00
if [[ $char == $BACKSPACE ]]; then
input=""
else
input="$input$char"
fi
2016-04-24 01:36:09 +02:00
result=`echo -e $match_lookup | grep "^$input:" | cut -f2 -d:`
2016-04-26 21:44:18 +02:00
tmux display-message "$input"
2016-04-24 01:36:09 +02:00
if [[ ! -z $result ]]; then
2016-04-26 21:44:18 +02:00
clear
2016-04-28 22:27:40 +02:00
echo -n "$result"
2016-04-26 21:44:18 +02:00
2016-04-30 01:26:43 +02:00
start_copy_mode
top_of_buffer
start_of_line
start_selection
end_of_line
cursor_left
copy_selection
2016-04-26 21:44:18 +02:00
tmux swap-pane -s $current_pane_id -t $fingers_pane_id
tmux kill-pane -t $fingers_pane_id
2016-04-24 01:36:09 +02:00
exit 0
fi
2016-04-26 21:44:18 +02:00
done < /dev/tty