tmux-fingers/scripts/fingers.sh

208 lines
4.1 KiB
Bash
Raw Normal View History

2016-05-02 21:15:29 +02:00
#!/usr/bin/env bash
2016-04-26 21:44:18 +02:00
2017-05-01 09:06:06 +02:00
eval "$(tmux show-env -g -s | grep ^FINGERS)"
2017-04-23 11:11:24 +02:00
2016-04-30 01:26:43 +02:00
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2016-04-30 02:42:56 +02:00
source $CURRENT_DIR/hints.sh
source $CURRENT_DIR/utils.sh
source $CURRENT_DIR/help.sh
2016-04-30 00:35:52 +02:00
2016-04-26 21:44:18 +02:00
current_pane_id=$1
fingers_pane_id=$2
last_pane_id=$3
fingers_window_id=$4
pane_input_temp=$5
original_rename_setting=$6
2016-04-24 01:36:09 +02:00
2016-04-30 01:36:21 +02:00
BACKSPACE=$'\177'
2016-04-24 01:36:09 +02:00
2019-01-02 10:36:55 +01:00
input=''
result=''
if [[ "$OSTYPE" == "linux-gnu" ]]; then
EXEC_PREFIX="nohup"
else
EXEC_PREFIX=""
fi
# TODO not sure this is truly working
function force_dim_support() {
tmux set -sa terminal-overrides ",*:dim=\\E[2m"
}
2016-07-16 10:40:49 +02:00
function is_pane_zoomed() {
local pane_id=$1
tmux list-panes \
-F "#{pane_id}:#{?pane_active,active,nope}:#{?window_zoomed_flag,zoomed,nope}" \
| grep -c "^${pane_id}:active:zoomed$"
}
function zoom_pane() {
local pane_id=$1
tmux resize-pane -Z -t "$pane_id"
2016-04-24 01:36:09 +02:00
}
function revert_to_original_pane() {
2016-05-02 21:15:29 +02:00
tmux swap-pane -s "$current_pane_id" -t "$fingers_pane_id"
tmux set-window-option automatic-rename "$original_rename_setting"
if [[ ! -z "$last_pane_id" ]]; then
tmux select-pane -t "$last_pane_id"
tmux select-pane -t "$current_pane_id"
fi
2018-08-28 11:36:52 +02:00
[[ $pane_was_zoomed == "1" ]] && zoom_pane "$current_pane_id"
}
function handle_exit() {
rm -rf "$pane_input_temp" "$pane_output_temp" "$match_lookup_table"
2019-01-02 10:36:55 +01:00
revert_to_original_pane
if [[ ! -z "$result" ]]; then
run_fingers_copy_command "$result" "$input"
fi
tmux kill-window -t "$fingers_window_id"
2016-04-26 21:44:18 +02:00
}
function is_valid_input() {
local input=$1
local is_valid=1
if [[ $input == "" ]] || [[ $input == "<ESC>" ]] || [[ $input == "?" ]]; then
is_valid=1
else
for (( i=0; i<${#input}; i++ )); do
char=${input:$i:1}
if [[ ! $(is_alpha $char) == "1" ]]; then
is_valid=0
break
fi
done
fi
echo $is_valid
}
function hide_cursor() {
echo -n $(tput civis)
}
2016-04-26 21:44:18 +02:00
trap "handle_exit" EXIT
compact_state=$FINGERS_COMPACT_HINTS
help_state=0
force_dim_support
pane_was_zoomed=$(is_pane_zoomed "$current_pane_id")
show_hints_and_swap $current_pane_id $fingers_pane_id $compact_state
[[ $pane_was_zoomed == "1" ]] && zoom_pane "$fingers_pane_id"
hide_cursor
input=''
2016-10-17 21:44:28 +02:00
function toggle_compact_state() {
if [[ $compact_state == "0" ]]; then
compact_state=1
else
2016-10-17 21:44:28 +02:00
compact_state=0
fi
}
2017-10-06 18:18:32 +02:00
function toggle_help_state() {
if [[ $help_state == "0" ]]; then
help_state=1
else
help_state=0
fi
}
function copy_result() {
local result="$1"
local hint="$2"
tmux set-buffer "$result"
}
function run_fingers_copy_command() {
local result="$1"
local hint="$2"
is_uppercase=$(echo "$input" | grep -E '^[a-z]+$' &> /dev/null; echo $?)
if [[ $is_uppercase == "1" ]] && [ ! -z "$FINGERS_COPY_COMMAND_UPPERCASE" ]; then
command_to_run="$FINGERS_COPY_COMMAND_UPPERCASE"
elif [ ! -z "$FINGERS_COPY_COMMAND" ]; then
command_to_run="$FINGERS_COPY_COMMAND"
fi
if [[ ! -z "$command_to_run" ]]; then
tmux run-shell -b "export IS_UPPERCASE=\"$is_uppercase\" HINT=\"$hint\" && printf \"$result\" | $EXEC_PREFIX $command_to_run"
fi
}
2019-01-02 10:33:32 +01:00
# %BENCHMARK_END%
while read -rsn1 char; do
# Escape sequence, flush input
if [[ "$char" == $'\x1b' ]]; then
read -rsn1 -t 0.1 next_char
if [[ "$next_char" == "[" ]]; then
read -rsn1 -t 0.1
continue
elif [[ "$next_char" == "" ]]; then
char="<ESC>"
else
continue
fi
fi
if [[ ! $(is_valid_input "$char") == "1" ]]; then
continue
fi
2017-10-06 18:18:32 +02:00
prev_help_state="$help_state"
prev_compact_state="$compact_state"
2016-04-30 02:42:56 +02:00
if [[ $char == "$BACKSPACE" ]]; then
2016-04-30 01:36:21 +02:00
input=""
continue
elif [[ $char == "<ESC>" ]]; then
if [[ $help_state == "1" ]]; then
2017-10-06 18:18:32 +02:00
toggle_help_state
else
exit
fi
elif [[ $char == "" ]]; then
2016-10-17 21:44:28 +02:00
toggle_compact_state
elif [[ $char == "?" ]]; then
2017-10-06 18:18:32 +02:00
toggle_help_state
2016-04-30 01:36:21 +02:00
else
input="$input$char"
fi
if [[ $help_state == "1" ]]; then
show_help "$fingers_pane_id"
else
2017-10-06 18:18:32 +02:00
if [[ "$prev_compact_state" != "$compact_state" ]]; then
show_hints "$fingers_pane_id" "$compact_state"
fi
fi
result=$(lookup_match "$input")
2016-04-26 21:44:18 +02:00
2016-04-30 02:42:56 +02:00
if [[ -z $result ]]; then
continue
fi
2016-04-26 21:44:18 +02:00
copy_result "$result" "$input"
2019-01-02 10:36:55 +01:00
exit 0
2016-04-26 21:44:18 +02:00
done < /dev/tty