diff --git a/harpoon b/harpoon index e66ba53..a74c560 100755 --- a/harpoon +++ b/harpoon @@ -19,7 +19,9 @@ help() { echo "Options:" echo " -a Track current tmux session" echo " -A Track pane within current tmux session" - echo " -r [session_name] Stop tracking session with session name. If" + echo " -r [index] Replace tracked entry at index with current session" + echo " -R [index] Replace tracked entry at index with current pane within session" + echo " -d [session_name] Stop tracking session with session name. If" echo " session_name is not passed then remove current session" echo " -l List tracked sessions" echo " -s [index] Switch to the session at the specified index in the" @@ -47,12 +49,39 @@ view() { _switch "$name" "$path" } -add() { - extended="$1" +_getBookmark() { + extended=$1 + base_info='#{session_name}=#{session_path}' pane_info=':#{window_index}.#{pane_index}' - tmux display -p "$base_info$([ "$extended" = 1 ] && echo "$pane_info")" >>"$cachefile" + tmux display -p "$base_info$([ "$extended" = true ] && echo "$pane_info")" +} + +_dedupe() { awk -i inplace '!seen[$0]++' "$cachefile" # de-duplicate +} + +replace() { + bookmark=$(_getBookmark "$1") + index=$2 + + linenumbers=$(wc -l <"$cachefile") + if [ "$linenumbers" -gt "$index" ]; then + escaped_bookmark=$(echo "$bookmark" | sed 's/\//\\\//g') + sed -i "${index}s/.*/$escaped_bookmark/" "$cachefile" + else + echo "$bookmark" >>"$cachefile" + fi + + _dedupe + exec tmux display "Tracking session #{session_name} in index $index" +} + +add() { + bookmark=$(_getBookmark "$1") + + echo "$bookmark" >>"$cachefile" + _dedupe exec tmux display 'Tracking session #{session_name}' } @@ -90,12 +119,14 @@ list_sessions() { tmux ls -F '#{session_name}=#{session_path}'; } bold() { printf "\033[1m%s\033[0m\n" "$*"; } main() { - while getopts ":haAr:ls:e" opt; do + while getopts ":haAr:R:d:ls:e" opt; do case "$opt" in h | :) help && exit 0 ;; - a) add ;; - A) add 1 ;; - r) remove "$OPTARG" ;; + a) add false ;; + A) add true ;; + r) replace false "$OPTARG" ;; + R) replace true "$OPTARG" ;; + d) remove "$OPTARG" ;; l) view ;; s) switch "$OPTARG" ;; e) edit_file ;; diff --git a/readme.md b/readme.md index 00266c9..8f19331 100644 --- a/readme.md +++ b/readme.md @@ -23,7 +23,9 @@ Usage: Options: -a Track current tmux session -A Track pane within current tmux session - -r [session_name] Stop tracking session with session name. If + -r [index] Replace tracked entry at index with current session + -R [index] Replace tracked entry at index with current pane within session + -d [session_name] Stop tracking session with session name. If session_name is not passed then remove current session -l List tracked sessions -s [index] Switch to the session at the specified index in the @@ -42,10 +44,14 @@ bind -n M-b run 'harpoon -a' bind -n . run 'harpoon -A' bind -n M-v run 'harpoon -l' bind -n M-i run 'harpoon -e' -bind -n M-n run 'harpoon -s 1' -bind -n M-e run 'harpoon -s 2' -bind -n M-o run 'harpoon -s 3' -bind -n M-s run 'harpoon -s 4' +bind -n M-q run 'harpoon -s 1' # alt+q goes to index 1 +bind M-q run 'harpoon -r 1' # prefix alt+q replace entry index 1 with current session +bind -n M-w run 'harpoon -s 2' +bind M-w run 'harpoon -R 2' # replace entry at index 2 with current pane within session +bind -n M-e run 'harpoon -s 3' +bind -n M-r run 'harpoon -s 4' + +# Note: If there is no entry at the given index, it is appended to the list instead.# adds pane instead of session to index 2 ``` ## Example