#!/bin/bash
#
-# Attach or create tmux session named the same as current directory.
+# Attach or create tmux session
#
-# # Attach
+# Based on Waylon Walker's ta script
#
-# If called with --start or without a directory name ta will create a plain
-# single window layout
+# If called with -s or --ssh, ta will ask which hostname
+# to connect to based on your /etc/hosts file and
+# attach or create a session and ssh to it.
#
-# ╭──────────────────────────────────────────────────────────╮
-# │ my_project on main [!?] via 🐍 v3.8.8 via ©conda-env │
-# │ ❯ │
-# │ │
-# │ │
-# │ │
-# │ │
-# │ │
-# │ │
-# │ │
-# │ │
-# │ │
-# │ │
-# ╰──────────────────────────────────────────────────────────╯
+# If called with -d or --dir DIRECTORY, ta will ask which project
+# to open and then attach or create a session.
#
-# # Split Layout
+# Example:
+# $ ta -d $HOME/dev
+# It will prompt you every directory from $HOME/dev
+# You can ( and should ) tweak with the filtering in get_dir_session_name()
#
-# When called to a specific directory ta will first ask which project to open
-# then attach or create anew session split with neovim on top.
-#
-# ╭──────────────────────────────────────────────────────────╮
-# │ │
-# │ │
-# │ nvim │
-# │ │
-# │ │
-# ├──────────────────────────────────────────────────────────┤
-# │ my_project on main [!?] via 🐍 v3.8.8 via ©conda-env │
-# │ ❯ │
-# │ │
-# │ │
-# │ │
-# │ │
-# ╰──────────────────────────────────────────────────────────╯
-
- not_in_tmux() {
- [ -z "$TMUX" ]
- }
-
-DIR=$1
-
-# If no arguments are passed in try to immediately attach or start without further input
-echo $DIR
-if [ -z "$DIR" ]; then
- if not_in_tmux; then
- tmux attach && exit 1 || DIR="--start"
- else
- exit 1
- fi
-fi
-
-# If --start was passed in immediately start a new session based on the current directory
-if [ "$DIR" == "--start" ]; then
- echo "starting"
- path_name="$(basename "$PWD" | tr . -)"
- session_name=${path_name//./_}
-else
- # ask the user which directory to start in
- _session_name=$(cd $DIR && ls -d */ | sed "s|/||g" | fzf --reverse --header="Select project from $(basename $DIR) >")
- session_name=${_session_name//./_}
- path_name=$DIR/$session_name
-fi
-
-echo session name is \"$session_name\"
-echo path name is $path_name
-
-if [ -z "$session_name" ]; then
- # operation cancelled by user
- exit 1
-fi
+# Dependencies: tmux, fzf, fd, tree, getopt
+
+
+
+SSH=0
+DIR=
+
+usage() {
+ echo "Usage: ta [ -s | --ssh ] [ -d | --dir DIRECTORY ]"
+ exit 2
+}
+
+error_exit() {
+ notify-send "ta $1" "$2"
+ exit 1
+}
session_exists() {
- # checks if the $session_name exists
- tmux has-session -t "=$session_name"
+ tmux has-session -t "=$session_name" 2>/dev/null
+}
+
+create_session() {
+ session_exists && tmux switch-client -t "$session_name"
+ [ "$SSH" -ne 0 ] && create_ssh_session
+ [ -n "$DIR" ] && create_dir_session
+ tmux switch-client -t "$session_name"
+}
+
+create_ssh_session() {
+ tmux new-session -ds "$session_name" "ssh $session_name"
}
-
-create_detached_session() {
-if [ "$DIR" == "--start" ]; then
- (TMUX=''
- tmux new-session -Ad -s "$session_name" -c $path_name
- )
-else
- (TMUX=''
- tmux new-session -Ad -s "$session_name" -c $path_name;
- tmux split-window -vb -t "$session_name" -c $path_name -p 70;
- tmux send-keys -t "$session_name" "nvim '+Telescope find_files'" Enter;
- )
-fi
- }
-
-create_if_needed_and_attach() {
- if not_in_tmux; then
- tmux new-session -As "$session_name" -c $path_name
- else
- if ! session_exists; then
- create_detached_session
- fi
- tmux switch-client -t "$session_name"
- fi
+
+create_dir_session() {
+ tmux new-session -ds "$session_name" -c "$path_name"
+}
+
+get_session_name() {
+ [ "$SSH" -ne 0 ] && get_ssh_session_name
+ [ -n "$DIR" ] && get_dir_session_name
+}
+
+get_ssh_session_name() {
+ session_name=$(sed \
+ -e '/\#/d' -e 's/.localdomain//' -e '/^$/d' /etc/hosts \
+ | awk '{print $2}' | sort | uniq \
+ | fzf --reverse --header="Select server >")
+}
+
+get_dir_session_name() {
+ tmp_name=$(fd -t d \
+ -E node_modules -E target -E src -E objects \
+ --max-depth=4 \
+ --base-directory "$DIR" \
+ | fzf --reverse \
+ --header="Select project >" \
+ --preview "tree -C -I target ${DIR}/{}")
+ [ -z "$tmp_name" ] && exit 1
+ path_name="$DIR/$tmp_name"
+ # [ -z "$path_name" ] && exit 1
+ session_name=$(echo "$path_name" \
+ | awk -F '/' '{print $NF}' \
+ | tr -d '.')
+ # session_name=$(echo "$path_name" \
+ # | awk -F '/' '{print $(NF-1),$NF}')
+ sleep 2
+}
+
+attach_random_session() {
+ # Redirects no server error output
+ session_name=$(tmux list-session -F '#{session_name}' 2>/dev/null | sort --random-sort | head -n 1)
+ tmux attach -t "$session_name"
}
- attatch_to_first_session() {
- tmux attach -t $(tmux list-sessions -F "${session_name}" | head -n 1)
- tmux choose-tree -Za
+attach_or_default_session() {
+ attach_random_session && exit 1
+ tmux new-session -As "$(whoami)" -c "$(whoami)"
}
-
-create_if_needed_and_attach || attatch_to_first_session
+
+PARSED_ARGS=$(getopt -a -n ta -o hsd: --long help,ssh,dir: -- "$@")
+VALID_ARGS=$?
+[ "$VALID_ARGS" -ne 0 ] && usage
+
+eval set -- "$PARSED_ARGS"
+while :; do
+ case "$1" in
+ -h | --help)
+ usage
+ ;;
+ -s | --ssh)
+ SSH=1
+ shift
+ ;;
+ -d | --dir)
+ DIR="$2"
+ shift 2
+ ;;
+ --)
+ shift
+ break
+ ;;
+ *)
+ echo "Unexpected option: $1 - this should not happend."
+ usage
+ ;;
+ esac
+done
+
+[ -z "$TMUX" ] && attach_or_default_session
+[ "$DIR" != "" ] && [ ! -d "$DIR" ] && error_exit "" "Directory $DIR DOES NOT exist."
+[ "$DIR" != "" ] && [ "$SSH" -ne 0 ] && error_exit "" "Cannot use both arguments"
+get_session_name
+[ -z "$session_name" ] && exit 1
+create_session