Skip to content

Commit

Permalink
v1.x Spec
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdrive committed Sep 18, 2021
1 parent cdd20bc commit 527e213
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 93 deletions.
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@ t

NAME
----
t -- easy tmux wrapper
t - Easy tmux wrapper

USAGE
----
* t session_name [TMUX_OPTIONS] # Find or create tmux-session, and attach this.
* t [-S|-s|--sock] socket_path # Find or create socket, And attach this session.
* t [-l|--list] [session|window] # Show alive tmux sessions.
* t [-k|--kill] session_name # Kill session. (default is current)
* t [-f|--prefix] [key] # Rebind tmux prefix-key.
* t [-d|--detach] # Detach current session.
* t [-m|--mouse] # Mouse mode on/off toggle.

```
* t session_name [TMUX_OPTIONS] # Find or create tmux-session, and detach any other client then attach this.
* [-A|-a|--attach] session_name # Find or create tmux-session, and attach this.
* [--ad|--attach-with-detach] session_name # Find or create tmux-session, and attach this.
* [-S|-s|--sock] socket_path # Find or create socket, And attach this session.
* [-l|--list] [session|window] # Show alive tmux sessions.
* [-k|--kill] session_name # Kill session. (default is current)
* [-f|--prefix] [key] # Rebind tmux prefix-key.
* [-d|--detach] # Detach current session.
* [-m|--mouse] # Mouse mode on/off toggle.
```

ENVIRONMENTS
---
```
T_DEFAULT_SESSIONNAME # Default use session_name (default: "main")
T_DEFAULT_SOCKPATH # Default use socket_path (default: "~/tmp//tmux-socket")
T_DEFAULT_TMUX_OPTION # Default tmux cmd optoin (default: "")
T_DEFAULT_TMUX_ATTACH_WITH_DETACH # Each other tty detach then attach session, when using `t` without specification. (default "on")
```


Author

Copyright (c) 2015 - 2018 Hiroshi IKEGAMI
Copyright (c) 2015 - 2021 Hiroshi IKEGAMI

MIT License

Expand Down
140 changes: 67 additions & 73 deletions t
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
#! /bin/bash

#
# t
# ====
#
# NAME
# ----
# t -- easy tmux wrapper
# t - Easy tmux wrapper
#
# USAGE
# ----
# * t session_name [TMUX_OPTIONS] # Find or create tmux-session, and detach any other client then attach this.
# * t [-a|--attach] session_name # Find or create tmux-session, and attach this.
# * t [-S|-s|--sock] socket_path # Find or create socket, And attach this session.
# * t [-l|--list] [session|window] # Show alive tmux sessions.
# * t [-k|--kill] session_name # Kill session. (default is current)
# * t [-f|--prefix] [key] # Rebind tmux prefix-key.
# * t [-d|--detach] # Detach current session.
# * t [-m|--mouse] # Mouse mode on/off toggle.
# * t session_name [TMUX_OPTIONS] # Find or create tmux-session, and detach any other client then attach this.
# * [-A|-a|--attach] session_name # Find or create tmux-session, and attach this.
# * [--ad|--attach-with-detach] session_name # Find or create tmux-session, and attach this.
# * [-S|-s|--sock] socket_path # Find or create socket, And attach this session.
# * [-l|--list] [session|window] # Show alive tmux sessions.
# * [-k|--kill] session_name # Kill session. (default is current)
# * [-f|--prefix] [key] # Rebind tmux prefix-key.
# * [-d|--detach] # Detach current session.
# * [-m|--mouse] # Mouse mode on/off toggle.
#
# Author
#
# Copyright (c) 2015 - 2020 Hiroshi IKEGAMI
# Copyright (c) 2015 - 2021 Hiroshi IKEGAMI
#
# MIT License
#
Expand Down Expand Up @@ -49,27 +45,29 @@

set -e
export LANG=C
export TMUX_DEFAULT_SESSIONNAME="main"
export TMUX_DEFAULT_SOCKPATH="~/tmp//tmux-socket"
export TMUX_DEFAULT_SUPPORT_COLOURS=256
export T_DEFAULT_SESSIONNAME="${T_DEFAULT_SESSIONNAME:-"main"}"
export T_DEFAULT_SOCKPATH="${T_DEFAULT_SOCKPATH:-"~/tmp//tmux-socket"}"
export T_DEFAULT_TMUX_OPTION="${T_DEFAULT_TMUX_OPTION:-""}"
export T_DEFAULT_TMUX_ATTACH_WITH_DETACH="${T_DEFAULT_TMUX_OPTION:-"on"}"


__usage_exit() {
cat << HELP
t::usage_exit() {
cat << HELP >&2
USAGE:
* t session_name # Find or create tmux-session, and detach any other client then attach this.
* t [-a|--attach] session_name # Find or create tmux-session, and attach this.
* t [-S|-s|--sock] socket_path # Find or create socket, And attach this session.
* t [-l|--list] [session|window] # Show alive tmux sessions.
* t [-k|--kill] session_name # Kill session. (default is current)
* t [-f|--prefix] [key] # Rebind tmux prefix-key.
* t [-d|--detach] # Detach current session.
* t [-m|--mouse] # Mouse mode on/off toggle.
* t session_name [TMUX_OPTIONS] # Find or create tmux-session, and detach any other client then attach this.
* [-A|-a|--attach] session_name # Find or create tmux-session, and attach this.
* [--ad|--attach-with-detach] session_name # Find or create tmux-session, and attach this.
* [-S|-s|--sock] socket_path # Find or create socket, And attach this session.
* [-l|--list] [session|window] # Show alive tmux sessions.
* [-k|--kill] session_name # Kill session. (default is current)
* [-f|--prefix] [key] # Rebind tmux prefix-key.
* [-d|--detach] # Detach current session.
* [-m|--mouse] # Mouse mode on/off toggle.
HELP
exit 1
}

__tmux_rebind_prefix() {
t::tmux_rebind_prefix() {
local _new_key_="C-${1}"
local _old_key_="C-$(tmux list-key | grep send-prefix | perl -p -e "s/.*C-([\\w]).*/\$1/")"

Expand All @@ -86,82 +84,76 @@ HELP
fi
}

__tmux_attach_simple() {
t::tmux_attach() {
if [ ! -z ${TMUX} ];then
echo 'sessions should be nested with care, unset $TMUX to force';
return 1;
fi
local session_name="${1:-"${TMUX_DEFAULT_SESSIONNAME}"}";
local session_name="${1:-"${T_DEFAULT_SESSIONNAME}"}";
if [ "$#" != 0 ];then
shift
fi
session_exists=$(tmux ls 2>&1 | cut -d ':' -f 1 | grep -e "^${session_name}$" | wc -l | perl -pe "s/\s//g")
if [ "${session_exists}" = 0 ]; then
tmux new-session -s "${session_name}"
tmux new-session -s "${session_name}" $@ ${T_DEFAULT_TMUX_OPTION}
else
tmux attach -t "${session_name}"
if [ ${T_TMUX_ATTACH_WITH_DETACH} = "on" ];then
tmux attach -d -t "${session_name}" $@ ${T_DEFAULT_TMUX_OPTION}
else
tmux attach -t "${session_name}" $@ ${T_DEFAULT_TMUX_OPTION}
fi
fi
}

__tmux_attach() {
if [ ! -z ${TMUX} ];then
t::tmux_sock() {
if [[ ! -z ${TMUX} ]];then
echo 'sessions should be nested with care, unset $TMUX to force';
return 1;
fi
local session_name="${1:-"${TMUX_DEFAULT_SESSIONNAME}"}";
if [ "$#" != 0 ];then
shift
fi
session_exists=$(tmux ls 2>&1 | cut -d ':' -f 1 | grep -e "^${session_name}$" | wc -l | perl -pe "s/\s//g")
if [ "${session_exists}" = 0 ]; then
tmux new-session -s "${session_name}" $@
else
tmux attach -d -t "${session_name}" $@
fi
}

__tmux_sock() {
if [ ! -z ${TMUX} ];then
echo 'sessions should be nested with care, unset $TMUX to force';
return 1;
fi
local socket_path="${TMUX_DEFAULT_SOCKPATH}";
local socket_path="${T_DEFAULT_SOCKPATH}";
[ $# -ne '0' ] && socket_path="$1";
if [ -e "${socket_path}" ]; then
mkdir -p "$(dirname "${socket_path}")"
tmux -S "${socket_path}"
tmux -S "${socket_path}" ${T_DEFAULT_TMUX_OPTION}
else
tmux -S "${socket_path}" attach
tmux -S "${socket_path}" attach ${T_DEFAULT_TMUX_OPTION}
fi
}

__tmux_mouse() {
t::tmux_mouse() {

[ "${TMUX_MOUSEMODE}" = '' ] && export TMUX_MOUSEMODE="$(tmux show -g | grep mouse-resize-pane | perl -p -e 's/mouse-resize-pane //')";
[ "${T_TMUX_MOUSEMODE}" = '' ] && \
export T_TMUX_MOUSEMODE="$(tmux show -g | \
grep mouse-resize-pane | \
perl -p -e 's/mouse-resize-pane //')";

if [ "${TMUX_MOUSEMODE}" == 'on' ];then
local switch=off
local switch="off"
else
local switch=on
local switch="on"
fi

tmux set-option -g mouse-select-pane ${switch}
tmux set-option -g mode-mouse ${switch}
tmux set-option -g mouse-resize-pane ${switch}
tmux set-option -g mouse-select-pane ${switch}
export TMUX_MOUSEMODE=${switch}
export T_TMUX_MOUSEMODE=${switch}

}


optspec=":a:f:s:k:-:hldm"
optspec=":A:a:f:S:s:k:-:hldm"
while getopts "$optspec" optchar; do
case "${optchar}" in
-)
case "${OPTARG}" in
attach)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
__tmux_attach_simple "${val}"; exit 0
env T_TMUX_ATTACH_WITH_DETACH=0 t::tmux_attach "${val}"; exit 0
;;
ad|attach-with-detach)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
env T_TMUX_ATTACH_WITH_DETACH=1 t::tmux_attach "${val}"; exit 0
;;
detach)
\tmux detach-client; exit 1
Expand All @@ -171,21 +163,21 @@ while getopts "$optspec" optchar; do
\tmux kill-session -t "${val}"; exit 1
;;
mouse)
__tmux_mouse; exit 0
t::tmux_mouse; exit 0
;;
prefix)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
__tmux_rebind_prefix "${val}"; exit 0
t::tmux_rebind_prefix "${val}"; exit 0
;;
sock)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
__tmux_sock "${val}"; exit 0
t::tmux_sock "${val}"; exit 0
;;
list)
\tmux ls; exit 0
;;
help)
__usage_exit; exit 1
t::usage_exit; exit 1
;;
*)
if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then
Expand All @@ -194,25 +186,25 @@ while getopts "$optspec" optchar; do
;;
esac;;
h)
__usage_exit; exit 1
t::usage_exit; exit 1
;;
l)
\tmux ls; exit 0
;;
a)
__tmux_attach_simple "${OPTARG}"; exit 0
A|a)
t::tmux_attach "${OPTARG}"; exit 0
;;
d)
\tmux detach-client; exit 1
;;
k)
\tmux kill-session -t "${OPTARG}"; exit 1
;;
s)
__tmux_sock "${OPTARG}"; exit 0
S|s)
t::tmux_sock "${OPTARG}"; exit 0
;;
m)
__tmux_mouse; exit 0
t::tmux_mouse; exit 0
;;
*)
if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
Expand All @@ -223,4 +215,6 @@ while getopts "$optspec" optchar; do
esac
done

__tmux_attach $@
export T_TMUX_ATTACH_WITH_DETACH="${T_DEFAULT_TMUX_ATTACH_WITH_DETACH}" && \
t::tmux_attach $@ && exit 0;

60 changes: 50 additions & 10 deletions t.1
Original file line number Diff line number Diff line change
@@ -1,43 +1,83 @@
.nh
.TH t
.SH NAME
.PP
t \-\- easy tmux wrapper
t \- easy tmux wrapper

.SH USAGE
.PP
.RS

.nf
* t session_name [TMUX_OPTIONS] # Find or create tmux-session, and detach any other client then attach this.
* t [-a|--attach] session_name # Find or create tmux-session, and attach this.
* t [\-S|\-s|\-\-sock] socket_path # Find or create socket, And attach this session.
* t [\-l|\-\-list] [session|window] # Show alive tmux sessions.
* t [\-k|\-\-kill] session_name # Kill session. (default is current)
* t [\-f|\-\-prefix] [key] # Rebind tmux prefix\-key.
* t [\-d|\-\-detach] # Detach current session.
* t [\-m|\-\-mouse] # Mouse mode on/off toggle.
* t session\_name [TMUX\_OPTIONS] # Find or create tmux\-session, and detach any other client then attach this.
* [\-A|\-a|\-\-attach] session\_name # Find or create tmux\-session, and attach this.
* [\-\-ad|\-\-attach\-with\-detach] session\_name # Find or create tmux\-session, and attach this.
* [\-S|\-s|\-\-sock] socket\_path # Find or create socket, And attach this session.
* [\-l|\-\-list] [session|window] # Show alive tmux sessions.
* [\-k|\-\-kill] session\_name # Kill session. (default is current)
* [\-f|\-\-prefix] [key] # Rebind tmux prefix\-key.
* [\-d|\-\-detach] # Detach current session.
* [\-m|\-\-mouse] # Mouse mode on/off toggle.

.fi
.RE

.SH ENVIRONMENTS
.PP
.RS

.nf
T\_DEFAULT\_SESSIONNAME # Default use session\_name (default: "main")
T\_DEFAULT\_SOCKPATH # Default use socket\_path (default: "~/tmp//tmux\-socket")
T\_DEFAULT\_TMUX\_OPTION # Default tmux cmd optoin (default: "")
T\_DEFAULT\_TMUX\_ATTACH\_WITH\_DETACH # Each other tty detach then attach session, when using `t` without specification. (default "on")

.fi
.RE

.PP
Author

.PP
Copyright (c) 2015 \- 2020 Hiroshi IKEGAMI
Copyright (c) 2015 \- 2021 Hiroshi IKEGAMI

.PP
MIT License

.PP
Permission is hereby granted, free of charge, to any person obtaining
.br
a copy of this software and associated documentation files (the
.br
"Software"), to deal in the Software without restriction, including
.br
without limitation the rights to use, copy, modify, merge, publish,
.br
distribute, sublicense, and/or sell copies of the Software, and to
.br
permit persons to whom the Software is furnished to do so, subject to
.br
the following conditions:
.br

.PP
The above copyright notice and this permission notice shall be
.br
included in all copies or substantial portions of the Software.
.br

.PP
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
.br
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
.br
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
.br
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
.br
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
.br
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
.br
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.br

0 comments on commit 527e213

Please sign in to comment.