feat(tmux): improve session creation script

Add a simple README to document it
This commit is contained in:
Yorick Barbanneau 2025-05-11 23:43:49 +02:00
parent 72f0ec1c2c
commit 0492b3189a
No known key found for this signature in database
GPG key ID: 4447A19BBEDB8DBA
3 changed files with 135 additions and 6 deletions

View file

@ -5,6 +5,47 @@ set -o pipefail
DEBUG=0
declare -a COMMANDS
help() {
cat <<EOF
${0}
Create a tmux session with defined elements (windows, panes, launch command).
This script need 'ghq' and 'fzf',
USAGE
-----
create-tmux-session -n <name> -i <id> -p <project> <command> <command>
-n --name <name> define a session name
-i --id <id> define a session id,
-r --repo <project> define the repo used as base directory if not defined
'ghq' and 'fzf' will be use to let you select one
<command> a command launched in the new created session. you can
use it many time.
COMMANDS
--------
All commands take only one parameter separated rom command by coma (':') for
example 'neww:shell'.
vsplit Split current pane vertically
parameter: new pane vertical size in % (default 20)
hsplit Split current pane horizontally (default 20)
parameter: new pane horizontal size in % (default 20)
neww Create a new window, parameter: windows name
run Run command into last created element (pane, window)
parameter: command
repo Change repo, useful to create a pane / window with a different base
directory
parameter: repo name
EOF
}
error() {
local message
printf -v message "\e[31mERROR:\e[0m %s\n" "$1"
@ -30,13 +71,17 @@ process_args() {
SESSION_NAME="$2"
shift
;;
-p | --project-name | --window)
-r | --repository-name | --repo)
PROJECT="$2"
shift
;;
-d | --debug)
DEBUG=1
;;
-h | --help)
help
exit 0
;;
*)
COMMANDS+=("$1")
;;
@ -45,11 +90,19 @@ process_args() {
done
}
check_command() {
local command="$1"
if ! command -v "${command}" >/dev/null 2>&1; then
error "Command '$command' not available"
exit 2
fi
}
has_session() {
tmux has-session -t "${SESSION_NAME:-$SESSION_ID}" 2>/dev/null
}
get_project_path() {
get_repo_path() {
local identifier="${1}"
debug "get project path with identifier '$identifier'"
if [[ -z "$identifier" ]]; then
@ -67,6 +120,13 @@ is_running_in_tmux() {
fi
}
check_available_commands() {
# Check commands dependencies
for c in fzf ghq; do
check_command "$c"
done
}
check_script_inputs() {
debug "check script inputs"
if [[ -z "$SESSION_ID" ]] && [[ -z "$SESSION_NAME" ]]; then
@ -125,10 +185,10 @@ tmux_run() {
fi
}
tmux_project() {
tmux_repo() {
local arg="$1"
debug "execute project with arg '${arg}'"
get_project_path "$arg"
get_repo_path "$arg"
}
create_session() {
@ -139,11 +199,12 @@ create_session() {
}
main() {
check_available_commands
process_args "$@"
is_running_in_tmux
check_script_inputs
if ! has_session "${SESSION_NAME}"; then
get_project_path "${PROJECT}"
get_repo_path "${PROJECT}"
create_session
# Process tmux commands passed with command line
for command in "${COMMANDS[@]}"; do