From f3d392f63d7cb249e4c206fae605e525c92f3d2c Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Sat, 30 Apr 2016 00:35:52 +0200 Subject: [PATCH] user configurable patterns --- .gitignore | 2 ++ scripts/config.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ scripts/debug.sh | 7 +++++++ scripts/fingers.sh | 7 ++++--- scripts/utils.sh | 16 ++++++++++++++++ tmux-fingers.tmux | 6 +++++- 6 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100755 scripts/config.sh create mode 100755 scripts/debug.sh create mode 100755 scripts/utils.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ec9423 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +fingers.log +copy-test.txt diff --git a/scripts/config.sh b/scripts/config.sh new file mode 100755 index 0000000..19256df --- /dev/null +++ b/scripts/config.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +DIRNAME="$(dirname "$0")" + +function check_pattern() { + echo "beep beep" | grep -e "$1" 2> /dev/null + + if [[ $? == "2" ]]; then + echo 0 + else + echo 1 + fi +} + +source "$DIRNAME/utils.sh" + +PATTERNS_LIST=( +"((^|^\.|[[:space:]]|[[:space:]]\.|[[:space:]]\.\.|^\.\.)[[:alnum:]~_-]*/[][[:alnum:]_.#$%&+=/@-]*)" +"([[:digit:]]{5,})" +"([0-9a-f]{7}|[0-9a-f]{40})" +) + +IFS=$'\n' +USER_DEFINED_PATTERNS=($(tmux show-options -g | grep ^@fingers-pattern | sed 's/^@fingers-pattern-[0-9] "\(.*\)"$/(\1)/')) +unset IFS + +PATTERNS_LIST=("${PATTERNS_LIST[@]}" "${USER_DEFINED_PATTERNS[@]}") + +i=0 +for pattern in "${PATTERNS_LIST[@]}" ; do + is_pattern_good=$(check_pattern "$pattern") + + if [[ $is_pattern_good == 0 ]]; then + display_message "fingers-error: bad user defined pattern $pattern" 5000 + PATTERNS_LIST[$i]="nope{4000}" + fi + + i=$((i + 1)) +done + +PATTERNS=$(array_join "|" "${PATTERNS_LIST[@]}") +export PATTERNS diff --git a/scripts/debug.sh b/scripts/debug.sh new file mode 100755 index 0000000..eb8632d --- /dev/null +++ b/scripts/debug.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +DIRNAME="$(dirname "$0")" + +function log() { + echo "$1" >> $DIRNAME/../fingers.log +} diff --git a/scripts/fingers.sh b/scripts/fingers.sh index b8bce00..7e53fa7 100755 --- a/scripts/fingers.sh +++ b/scripts/fingers.sh @@ -1,5 +1,8 @@ #!/bin/bash +DIRNAME="$(dirname "$0")" +source $DIRNAME/config.sh + #TODO move this out of here! current_pane_id=$1 fingers_pane_id=$2 @@ -23,15 +26,13 @@ function fancy() { clear_screen -PATTERNS= - lines='' while read -r line do lines+="$line\n" done < /dev/stdin -matches=`echo -e $lines | (grep -oniE "((^|^\.|[[:space:]]|[[:space:]]\.|[[:space:]]\.\.|^\.\.)[[:alnum:]~_-]*/[][[:alnum:]_.#$%&+=/@-]*)|([[:digit:]]{5,})|([0-9a-f]{7,40})" 2> /dev/null) | sort -u` +matches=`echo -e $lines | (grep -oniE "$PATTERNS" 2> /dev/null) | sort -u` match_count=`echo "$matches" | wc -l` output="$lines" diff --git a/scripts/utils.sh b/scripts/utils.sh new file mode 100755 index 0000000..a2c7a03 --- /dev/null +++ b/scripts/utils.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +function array_join() { + local IFS="$1"; shift; echo "$*"; +} + +function array_concat() { + echo "$*" +} + +function display_message() { + local original_display_time=$(tmux show-option -gqv display-time) + tmux set-option -g display-time $2 + tmux display-message "$1" + tmux set-option -g display-time $original_display_time +} diff --git a/tmux-fingers.tmux b/tmux-fingers.tmux index d9212a7..8f36124 100755 --- a/tmux-fingers.tmux +++ b/tmux-fingers.tmux @@ -1,4 +1,8 @@ #!/usr/bin/env bash CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -tmux bind-key F run-shell "tmux capture-pane -p | $CURRENT_DIR/scripts/tmux-fingers.sh" +DEFAULT_FINGERS_KEY="F" +FINGERS_KEY=$(tmux show-option -gqv @fingers-key) +FINGERS_KEY=${FINGERS_KEY:-$DEFAULT_FINGERS_KEY} + +tmux bind-key $FINGERS_KEY run-shell "tmux capture-pane -p | $CURRENT_DIR/scripts/tmux-fingers.sh"