exec mode

This commit is contained in:
Jorge Morante 2016-04-30 02:42:56 +02:00
parent 97199817ec
commit 797784b16f
5 changed files with 125 additions and 63 deletions

32
scripts/exec.sh Executable file
View File

@ -0,0 +1,32 @@
#!/bin/bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $CURRENT_DIR/utils.sh
function contains_placeholders() {
local input=$1
echo "$input" | grep -c "%"
}
function replace_placeholders() {
local template=$1
local result=$2
echo $template | sed "s/%/$result/g"
}
command_template=$1
result=$2
current_pane_id="%$3"
fingers_pane_id="%$4"
if [[ $(contains_placeholders "$command_template") == 1 ]]; then
pane_exec "$current_pane_id" "$(replace_placeholders "$command_template" "$result")"
else
pane_exec "$current_pane_id" "$command_template $result"
fi
revert_to_original_pane $current_pane_id $fingers_pane_id
exit 0

View File

@ -3,15 +3,12 @@
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $CURRENT_DIR/config.sh
source $CURRENT_DIR/actions.sh
source $CURRENT_DIR/hints.sh
#TODO move this out of here!
current_pane_id=$1
fingers_pane_id=$2
tmp_path=$3
ALPHABET=asdfqwertjkluiop
ALPHABET_SIZE=${#ALPHABET}
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)
BACKSPACE=$'\177'
function clear_screen() {
@ -19,42 +16,12 @@ function clear_screen() {
tmux clearhist
}
function get_hint() {
echo ${HINTS[$1]}
}
function fancy() {
printf "\033[1;33m$1\033[0m"
function has_capitals() {
echo $1 | grep [A-Z] | wc -l
}
clear_screen
lines=''
while read -r line
do
lines+="$line\n"
done < /dev/stdin
matches=`echo -e $lines | (grep -oniE "$PATTERNS" 2> /dev/null) | sort -u`
match_count=`echo "$matches" | wc -l`
output="$lines"
i=0
match_lookup=''
OLDIFS=$IFS
IFS=$(echo -en "\n\b") # wtf bash?
for match in $matches ; do
hint=`get_hint $i`
linenumber=`echo $match | cut -f1 -d:`
text=`echo $match | cut -f2 -d:`
output=`echo -ne "$output" | sed "${linenumber}s!$text!$(fancy $text) $(fancy "[$hint]")!g"`
match_lookup="$match_lookup\n$hint:$text"
i=$(($i+1))
done
IFS=$OLDIFS
echo -ne "$output"
print_hints
function handle_exit() {
tmux swap-pane -s $current_pane_id -t $fingers_pane_id
@ -62,36 +29,45 @@ function handle_exit() {
rm -rf $tmp_path
}
function copy_result() {
local result=$1
clear
echo -n "$result"
start_copy_mode
top_of_buffer
start_of_line
start_selection
end_of_line
cursor_left
copy_selection
}
trap "handle_exit" EXIT
input=''
while read -r -s -n1 char
do
if [[ $char == $BACKSPACE ]]; then
if [[ $char == "$BACKSPACE" ]]; then
input=""
else
input="$input$char"
fi
result=`echo -e $match_lookup | grep "^$input:" | cut -f2 -d:`
result=$(lookup_match "$input")
tmux display-message "$input"
if [[ ! -z $result ]]; then
clear
echo -n "$result"
start_copy_mode
top_of_buffer
start_of_line
start_selection
end_of_line
cursor_left
copy_selection
tmux swap-pane -s $current_pane_id -t $fingers_pane_id
tmux kill-pane -t $fingers_pane_id
exit 0
if [[ -z $result ]]; then
continue
fi
if [[ $(has_capitals $input) == "1" ]]; then
#TODO wtf spaces?
tmux command-prompt -p "fingers-exec:" "run-shell -t $fingers_pane_id \"$CURRENT_DIR/exec.sh '%%' '$result' '${current_pane_id//%}' '${fingers_pane_id//%}'\""
else
copy_result "$result"
revert_to_original_pane $current_pane_id $fingers_pane_id
fi
exit 0
done < /dev/tty

45
scripts/hints.sh Executable file
View File

@ -0,0 +1,45 @@
#!/bin/bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
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=''
function get_hint() {
echo "${HINTS[$1]}"
}
function highlight() {
printf "\033[1;33m%s\033[0m" "$1"
}
function lookup_match() {
local input=$1
echo -e "$match_lookup_table" | grep -i "^$input:" | cut -f2 -d:
}
lines=''
while read -r line
do
lines+="$line\n"
done < /dev/stdin
matches=$(echo -e $lines | (grep -oniE "$PATTERNS" 2> /dev/null) | sort -u)
output="$lines"
i=0
OLDIFS=$IFS
IFS=$(echo -en "\n\b") # wtf bash?
for match in $matches ; do
hint=$(get_hint $i)
linenumber=$(echo $match | cut -f1 -d:)
text=$(echo $match | cut -f2 -d:)
output=$(echo -ne "$output" | sed "${linenumber}s!$text!$(highlight $text) $(highlight "[$hint]")!g")
match_lookup_table="$match_lookup_table\n$hint:$text"
i=$((i + 1))
done
IFS=$OLDIFS
function print_hints() {
echo -ne "$output"
}

View File

@ -1,14 +1,7 @@
#!/bin/bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
function pane_exec() {
local pane_id=$1
local pane_command=$2
tmux send-keys -t $pane_id "$pane_command"
tmux send-keys -t $pane_id Enter
}
source $CURRENT_DIR/utils.sh
function init_fingers_pane() {
local pane_id=`tmux new-window -F "#{pane_id}" -P -d -n "!fingers"`

View File

@ -14,3 +14,19 @@ function display_message() {
tmux display-message "$1"
tmux set-option -g display-time $original_display_time
}
function revert_to_original_pane() {
local current_pane_id=$1
local fingers_pane_id=$2
tmux swap-pane -s $current_pane_id -t $fingers_pane_id
tmux kill-pane -t $fingers_pane_id
}
function pane_exec() {
local pane_id=$1
local pane_command=$2
tmux send-keys -t $pane_id "$pane_command"
tmux send-keys -t $pane_id Enter
}