Skip to content

Commit

Permalink
auto-open/close on attach/deattach
Browse files Browse the repository at this point in the history
  • Loading branch information
ofirgall committed Feb 11, 2022
1 parent 7d75635 commit f0d4152
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 25 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,27 @@ Set to '1' to disable hook to resurrect (Auto saves the session).
set -g @browser_dont_hook_to_resurrect '0'
```

### `@browser_launch_on_attach`

Set to '1' to launch restored browser on attach

```tmux.conf
set -g @browser_launch_on_attach '0'
```

### `@browser_close_on_deattach`

Set to '1' to close the attached browser on session de-attach

```tmux.conf
set -g @browser_close_on_deattach '1'
```

---

## TODO

Doesn't Require Custom Extensions:
* Auto-open browser on attach (option)
* Auto-close browser on de-attach (option)
* move active tab of current session to other session (+jump?)

Requires Custom Extension:
Expand Down
4 changes: 4 additions & 0 deletions scripts/open_browser_wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
screen -dm -- $CURRENT_DIR/open_browser.sh
15 changes: 15 additions & 0 deletions scripts/save_and_close_browser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
current_session=$(tmux display-message -p '#S')
tab_id_name=localhost:1212/dont_close-tmux-browser_$current_session

bt_list=$($CURRENT_DIR/bt_list_wrapper.sh) || exit $?
if echo "$bt_list" | grep -q "$tab_id_name$"; then
$CURRENT_DIR/save_session.sh $current_session "$(echo "$bt_list")"

window_id=$(echo "$bt_list" | cut -f1,3 | grep "$tab_id_name$" | cut -f1,2 -d ".")
tab_ids=$(echo "$bt_list" | grep "$window_id\\." | cut -f1)

echo $tab_ids | bt close
fi
29 changes: 29 additions & 0 deletions scripts/save_session.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"

SESSIONS_DIR=$(sessions_dir)
mkdir -p $SESSIONS_DIR

session_name=$1
bt_list=$2

tmpfile=$(mktemp /tmp/tmux-browser.XXXXXX)
found_special_tab=0
while read tab; do
tab_url=$(echo $tab | cut -f2 -d " ")
if echo $tab_url | grep -q "dont_close-tmux-browser_"; then
found_special_tab=1
continue
fi

echo $tab_url >> $tmpfile
done < <(echo "$bt_list" | cut -f1,3 | grep "$window_id\.")

# Dont override if the tab didnt found
if [ $found_special_tab -eq 1 ]; then
mv $tmpfile $SESSIONS_DIR/$session_name
else
rm $tmpfile
fi
23 changes: 1 addition & 22 deletions scripts/save_sessions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,13 @@
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"

SESSIONS_DIR=$(sessions_dir)
mkdir -p $SESSIONS_DIR

bt_list=$($CURRENT_DIR/bt_list_wrapper.sh) || exit $?

while read session; do
window_id=$(echo $session | cut -f 1-2 -d ".")
session_name=$(echo $session | grep -Po "(?<=dont_close-tmux-browser_).+$")
# echo "Window ID:$window_id"
# echo "Session Name:$session_name"
# echo "Tabs:"

tmpfile=$(mktemp /tmp/tmux-browser.XXXXXX)
found_special_tab=0
while read tab; do
tab_url=$(echo $tab | cut -f2 -d " ")
if echo $tab_url | grep -q "dont_close-tmux-browser_"; then
found_special_tab=1
continue
fi

echo $tab_url >> $tmpfile
done < <(echo "$bt_list" | cut -f1,3 | grep "$window_id\.")

# Dont override if the tab didnt found
if [ $found_special_tab -eq 1 ]; then
mv $tmpfile $SESSIONS_DIR/$session_name
else
rm $tmpfile
fi
$CURRENT_DIR/save_session.sh $session_name $(echo "$bt_list")
done < <(echo "$bt_list" | cut -f1,3 | grep dont_close-tmux-browser)
10 changes: 9 additions & 1 deletion tmux_browser.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/scripts/helpers.sh"

tmux bind-key "$(tmux_option "@open_browser_key" "B")" run-shell "screen -dm -- $CURRENT_DIR/scripts/open_browser.sh"
tmux bind-key "$(tmux_option "@open_browser_key" "B")" run-shell "$CURRENT_DIR/scripts/open_browser_wrapper.sh"

if [ "$(tmux_option "@browser_dont_hook_to_resurrect" "0")" == "0" ]; then
tmux set -g @resurrect-hook-post-save-all "$CURRENT_DIR/scripts/save_sessions.sh"
fi

if [ "$(tmux_option "@browser_launch_on_attach" "0")" == "1" ]; then
tmux set-hook -g client-attached "run-shell "$CURRENT_DIR/scripts/open_browser_wrapper.sh""
fi

if [ "$(tmux_option "@browser_close_on_deattach" "1")" == "1" ]; then
tmux set-hook -g client-detached "run-shell "$CURRENT_DIR/scripts/save_and_close_browser.sh""
fi

0 comments on commit f0d4152

Please sign in to comment.