Created
August 6, 2021 22:10
-
-
Save apolopena/69ea26552e53cddb9ff501ae503aa350 to your computer and use it in GitHub Desktop.
Creates new (or attaches to existing) tmux sessions using an '.icon' file. This script can be called from inside or outside of tmux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# SPDX-License-Identifier: MIT | |
# Copyright © 2021 Apolo Pena | |
# | |
# tmux-named-sessions.sh | |
# Description: | |
# Creates new tmux sessions using an '.icon' file | |
# The .icon file is an text file next to this script with an emoji in it | |
# This script can be called from inside or outside of tmux | |
# If a session does not exist a new one will be created | |
# If a session already exists then that session will be attached | |
# | |
icon_root=$(dirname "$(realpath "$0")") | |
setSession() { | |
default_icon="🖥️" | |
icon="$icon_root/.icon-$1" | |
if [[ -f $icon ]]; then | |
icon=$(cat "$icon") | |
else | |
echo -e "Could not find an .icon file in $icon, using the default icon $default_icon️️️" | |
icon="$default_icon" | |
fi | |
session="session $icon" | |
} | |
setSession "$1" | |
if [[ -z $TMUX ]]; then | |
if tmux has-session -t "$session" 2>/dev/null; then | |
echo "Attaching to $session" | |
tmux attach-session -d -t "$session" | |
else | |
echo "Creating new $session" | |
tmux new-session -s "$session" | |
fi | |
else | |
# We're inside Tmux, create a new session if it does not exist | |
sesh="$(tmux ls | grep "$session")" | |
if [[ -z $sesh ]]; then | |
tmux new -s "$session" -d | |
fi | |
# Switch to the session if we are not already in it | |
if [[ $(tmux display-message -p "#S") != "$session" ]]; then | |
tmux switch-client -t "$session" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment