Skip to content

Commit

Permalink
Merge pull request #2 from oysandvik94/feat/overwrite_indexes
Browse files Browse the repository at this point in the history
feat: add option for overwriting indexes
  • Loading branch information
Chaitanyabsprip authored Jun 10, 2024
2 parents 504afb1 + 4583039 commit faaa278
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
47 changes: 39 additions & 8 deletions harpoon
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}'
}

Expand Down Expand Up @@ -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 ;;
Expand Down
16 changes: 11 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit faaa278

Please sign in to comment.