joshmedeski / t-smart-tmux-session-manager

t - the smart tmux session manager

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Runtype Refactor

TheSast opened this issue · comments

I'd suggest you make a new issue outlining the areas you want to refactor and we can discuss it. I realize the script is procedural but it has come a long way since it was originally published.

Originally posted by @joshmedeski in #28 (comment)


The refactor would consist in moving the checks for how t was run to the very start.
This would be stored in a variable to then be easily switch-cased on for extra readability.
With this the script could also correctly show which binding should be used to invoke it in Tmux inside the help page if it is different from default.

I separated the help command related refactor to #34

Can you post a general outline of what the switch case would look like? I'm trying to visualize how this would improve the script.

Right now the code is procedural, but we'd be introducing a lot of indirect logic which would require proper naming variables and a flow that is still obvious and easy-to-read for the refactor to be worth it (in my opinion).

The code comments within the conditional logic helped me in understanding the logic, but I'm open to rewriting it if it improves the clarity of what's happening.

I don't want to refactor just to be clever or just for the sake of it, so let's outline a general idea of what it will look at before rewriting it.

Also, I don't have any automated tests right now so a refactor would require exhaustive manual testing so we make sure we don't introduce any regressions.

Can you post a general outline of what the switch case would look like? I'm trying to visualize how this would improve the script.

if [ $# -eq 0 ]; then             # no argument provided
	if [ "$TMUX" = "" ]; then        # not in tmux
		if [ $TMUX_STATUS -eq 0 ]; then # tmux is running
			RESULT=$(
				(get_sessions_by_mru && (zoxide query -l | sed -e "$HOME_REPLACER")) | fzf \
					--bind "$DIR_BIND" \
					--bind "$SESSION_BIND" \
					--bind "$ZOXIDE_BIND" \
					--border-label "$BORDER_LABEL" \
					--header "$HEADER" \
					--no-sort \
					--prompt "$PROMPT"
			)
		else # tmux is not running
			RESULT=$(
				(zoxide query -l | sed -e "$HOME_REPLACER") | fzf \
					--bind "$DIR_BIND" \
					--border-label "$BORDER_LABEL" \
					--header " ctrl-d: directory" \
					--no-sort \
					--prompt "$PROMPT"
			)
		fi
	else # in tmux
		RESULT=$(
			(get_sessions_by_mru && (zoxide query -l | sed -e "$HOME_REPLACER")) | fzf-tmux \
				--bind "$DIR_BIND" \
				--bind "$SESSION_BIND" \
				--bind "$ZOXIDE_BIND" \
				--border-label "$BORDER_LABEL" \
				--header "$HEADER" \
				--no-sort \
				--prompt "$PROMPT" \
				-p 60%,50%
		)
	fi
else # argument provided
    ...
fi

into

if [ $# -eq 0 ]; then             # no argument provided
	case $T_RUNTYPE in
	detached)
		RESULT=$(
			(get_sessions_by_mru && (zoxide query -l | sed -e "$HOME_REPLACER")) | fzf \
				--bind "$DIR_BIND" \
				--bind "$SESSION_BIND" \
				--bind "$ZOXIDE_BIND" \
				--border-label "$BORDER_LABEL" \
				--header "$HEADER" \
				--no-sort \
				--prompt "$PROMPT"
		)
		;;
	serverless)
		RESULT=$(
			(zoxide query -l | sed -e "$HOME_REPLACER") | fzf \
				--bind "$DIR_BIND" \
				--border-label "$BORDER_LABEL" \
				--header " ctrl-d: directory" \
				--no-sort \
				--prompt "$PROMPT"
		)
		;;
	attached)
		RESULT=$(
			(get_sessions_by_mru && (zoxide query -l | sed -e "$HOME_REPLACER")) | fzf-tmux \
				--bind "$DIR_BIND" \
				--bind "$SESSION_BIND" \
				--bind "$ZOXIDE_BIND" \
				--border-label "$BORDER_LABEL" \
				--header "$HEADER" \
				--no-sort \
				--prompt "$PROMPT" \
				-p 60%,50%
		)
		;;
	esac
else # argument provided
    ...
fi

if [ $TMUX_STATUS -eq 0 ]; then                                 # tmux is running
	SESSION=$(tmux list-sessions -F '#S' | grep "^$SESSION_NAME$") # find existing session
else
	SESSION=""
fi

if [ "$TMUX" = "" ]; then                          # not currently in tmux
	if [ "$SESSION" = "" ]; then                      # session does not exist
		tmux new-session -s "$SESSION_NAME" -c "$RESULT" # create session and attach
	else                                              # session exists
		tmux attach -t "$SESSION"                        # attach to session
	fi
else                                                  # currently in tmux
	if [ "$SESSION" = "" ]; then                         # session does not exist
		tmux new-session -d -s "$SESSION_NAME" -c "$RESULT" # create session
		tmux switch-client -t "$SESSION_NAME"               # attach to session
	else                                                 # session exists
		tmux switch-client -t "$SESSION"                    # switch to session
	fi
fi

into

SESSION=$(tmux list-sessions -F '#S' | grep "^$SESSION_NAME$") # find existing session

case $T_RUNTYPE in
detached)
	if [ "$SESSION" = "" ]; then                      # session does not exist
		tmux new-session -s "$SESSION_NAME" -c "$RESULT" # create session and attach
	else                                              # session exists
		tmux attach -t "$SESSION"                        # attach to session
	fi
attached)
	if [ "$SESSION" = "" ]; then                         # session does not exist
		tmux new-session -d -s "$SESSION_NAME" -c "$RESULT" # create session
		tmux switch-client -t "$SESSION_NAME"               # attach to session
	else                                                 # session exists
		tmux switch-client -t "$SESSION"                    # switch to session
	fi
serverless)
	tmux new-session -s "$SESSION_NAME" -c "$RESULT"
esac

although I would recommend if [ "$SESSION" ]; then EXIST else NOTEXIST fi over if [ "$SESSION" = "" ]; then NOTEXIST else EXIST fi.


In this example I kept the ordering the same, I would actually order them attached -> detached -> serverless.

Nice! I do like this approach, thanks for taking the time to write this up.

A couple of notes:

We could put the "if argument" code at the top of the script and just exit to not have to put everything in a conditional

if [ $# -eq 1 ]; then # argument provided
  # ...
endif

case $T_RUNTYPE in
detached)
	# ...
	;;
serverless)
	# ...
	;;
attached)
	# ...
	;;
esac

I'm open to arranging the switch case to whatever feels cleaner.

I got a comment on my YouTube video that said:

You could use tmux new -As mysession to either attach or create the session.

If that's true, we could simplify the conditional logic even more in some places.

If you want to get started I can do code review and do some testing once you've rewritten it.

Alright, will do that.

Should I base my pr off of main or #32 ?

Yes, I fixed the "^a" keybinding to show all and we can adjust the keybindings before merging. I like your suggestions.