Skip to content

Commit

Permalink
detect tmux sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jan 5, 2024
1 parent c7745fe commit cb40bcc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ The following environment variables can be set in `~/.bash_funk_rc` to customize
- `BASH_FUNK_PROMPT_PREFIX` - text that shall be shown at the beginning of the Bash prompt, e.g. a stage identifier (DEV/TEST/PROD)
- `BASH_FUNK_PROMPT_DATE` - prompt escape sequence for the date section, default is `\t`, which displays current time. See http://tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html
- `BASH_FUNK_PROMPT_NO_JOBS` - if set to any value the Bash prompt will not display the number of shell jobs.
- `BASH_FUNK_PROMPT_NO_SCREENS` - if set to any value the Bash prompt will not display the number of detached screens
- `BASH_FUNK_PROMPT_NO_SCREENS` - if set to any value the Bash prompt will not display the number of detached screen/tmux sessions
- `BASH_FUNK_PROMPT_NO_TTY` - if set to any value the Bash prompt will not display the current tty.
- `BASH_FUNK_PROMPT_NO_KUBECTL` - if set to any value the Bash prompt will not display [kubectl](https://kubernetes.io/docs/reference/kubectl/)'s current context
- `BASH_FUNK_PROMPT_NO_GIT` - if set to any value the Bash prompt will not display GIT branch and modification information.
Expand Down
16 changes: 12 additions & 4 deletions bash-funk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,19 @@ EOL
echo "You are ready to go. Enjoy"'!'

# show information about detached screens
if hash screen &>/dev/null; then
__screens="$(screen -list | grep "etached)" | sort | sed -En "s/\s+(.*)\s+.*/ screen -r \1/p")"
if [[ $__screens ]]; then
if [[ -z $STY && -z $TMUX ]]; then
if hash screen &>/dev/null; then
__screens=$(screen -list | grep "etached)" | sort | sed -En "s/\s+(.*)\s+.*/ screen -r \1/p")
fi
if hash tmux &>/dev/null; then
# https://github.com/tmux/tmux/wiki/Formats#summary-of-modifiers
[[ -z $__screens ]] || __screens+="\n"
__screens+=$(tmux list-sessions -f "#{==:#{session_attached},0}" -F " tmux attach-session -t #{session_name}")
fi

if [[ -n $__screens ]]; then
echo
echo "The following detached screens have been detected:"
echo "The following detached sessions have been detected:"

if [[ $TERM == "cygwin" ]]; then
echo -e "\033[1m\033[30m${__screens}\033[0m"
Expand Down
39 changes: 26 additions & 13 deletions modules/bash-prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ else
BASH_FUNK_DIRS_COLOR="${BASH_FUNK_DIRS_COLOR:-0;94}"
fi

if ! hash screen &>/dev/null; then
BASH_FUNK_PROMPT_NO_SCREENS=1
fi
if ! hash svn &>/dev/null; then
BASH_FUNK_PROMPT_NO_SVN=1
fi
Expand Down Expand Up @@ -154,23 +151,39 @@ function __-bash-prompt() {
local p_screens
if [[ ! ${BASH_FUNK_PROMPT_NO_SCREENS:-} ]]; then
# determine number of attached and detached screens
p_screens=$(screen -ls 2>/dev/null | grep "tached)" | wc -l);
if [[ ${STY:-} ]]; then
# don't count the current screen session
(( p_screens-- ))
if hash screen &>/dev/null; then
p_screens=$(screen -ls 2>/dev/null | grep "tached)" | wc -l)
if [[ -n $STY ]]; then
# don't count the current screen session
(( p_screens-- ))
fi
case "$p_screens" in
0) p_screens= ;;
1) p_screens="| ${C_FG_LIGHT_YELLOW}1 screen${C_FG_BLACK} " ;;
*) p_screens="| ${C_FG_LIGHT_YELLOW}$p_screens screens${C_FG_BLACK} " ;;
esac
fi
if hash tmux &>/dev/null; then
p_tmux=$(tmux list-sessions | wc -l)
if [[ -n $TMUX ]]; then
# don't count the current screen session
(( p_tmux-- ))
fi
case $_ptmux in
0) p_tmux= ;;
1) p_tmux="| ${C_FG_LIGHT_YELLOW}1 tmux${C_FG_BLACK} " ;;
*) p_tmux="| ${C_FG_LIGHT_YELLOW}$p_tmux tmux${C_FG_BLACK} " ;;
esac
fi
case "$p_screens" in
0) p_screens= ;;
1) p_screens="| ${C_FG_LIGHT_YELLOW}1 screen${C_FG_BLACK} " ;;
*) p_screens="| ${C_FG_LIGHT_YELLOW}$p_screens screens${C_FG_BLACK} " ;;
esac
fi


local p_tty
if [[ ! ${BASH_FUNK_PROMPT_NO_TTY:-} ]]; then
if [[ ${STY:-} ]]; then
p_tty="| tty #\l ${C_FG_LIGHT_YELLOW}(screen)${C_FG_BLACK} "
elif [[ ${TMUX:-} ]]; then
p_tty="| tty #\l ${C_FG_LIGHT_YELLOW}(tmux)${C_FG_BLACK} "
else
p_tty="| tty #\l "
fi
Expand Down Expand Up @@ -330,7 +343,7 @@ function __-bash-prompt() {
p_prefix="${C_RESET}${p_bg}"
fi

local LINE1="${p_prefix}${p_lastRC}${p_user}${p_host}${p_date}${p_jobs}${p_screens}${p_tty}${p_kubectl}${p_scm}${C_RESET}"
local LINE1="${p_prefix}${p_lastRC}${p_user}${p_host}${p_date}${p_jobs}${p_screens}${p_tmux}${p_tty}${p_kubectl}${p_scm}${C_RESET}"
local LINE2="[\033[${BASH_FUNK_DIRS_COLOR}m${pwd}${C_RESET}]"
local LINE3="$ "
PS1="\n$LINE1\n$LINE2\n$LINE3"
Expand Down

0 comments on commit cb40bcc

Please sign in to comment.