From ec682c6fe8570b24d3b2dfca161807f57cce84c1 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 11 May 2025 23:43:49 +0200 Subject: [PATCH 1/4] feat(tmux): improve session creation script Add a simpre README to document it --- modules/home-manager/cli/tmux/files/README.md | 68 ++++++++++++++++++ .../cli/tmux/files/create-tmux-session.sh | 71 +++++++++++++++++-- 2 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 modules/home-manager/cli/tmux/files/README.md diff --git a/modules/home-manager/cli/tmux/files/README.md b/modules/home-manager/cli/tmux/files/README.md new file mode 100644 index 0000000..45a2b50 --- /dev/null +++ b/modules/home-manager/cli/tmux/files/README.md @@ -0,0 +1,68 @@ +# Create a tmux session + +This script help me to create a tmux session and manage related windows, panes and commands. + +## Usage + +``` +create-tmux-session -n -i -p + +-n --name define a session name +-i --id define a session id, +-r --repo define the repo used as base directory if not defined + 'ghq' and 'fzf' will be use to let you select one + + a command launched in the new created session. you can + use it many time. +``` + +## Commands + +Commands create windows and panes, launch program and change current directory related to git project (managed by [ghq][ghq]). +Each command can take a parameter which is mandatpry if no default value exists. +If parameter contains spaces, must be surrounded by quotes / double quote. + +* `vsplit`: split current pane vertically, parameter define the vertical size of the created pane in percent (default 20). + ```bash + # split first windows, new created pane will take 50% of vertical space + create-tmux-session.sh -n mysession vsplit:50 + ``` +* `hsplit`: split current pane horizontally + ```bash + # split first windows, new created pane will take 50% of horizontal space + create-tmux-session.sh -n mysession hsplit:50 + ``` +* `neww`: create a new windows, parameter define the new windows name (not mandatory) + ```bash + # Create a new window name 'shell' + create-tmux-session.sh -n mysession -r blog neww:shell + ``` +* `run`: launch a command in last created element (window / pane ), parameter define command. + ```bash + # Just launch ls + create-tmux-session.sh -n mysession -r blog run:ls + + # This time with parameters + create-tmux-session.sh -n mysession -r blog "run:ls -la" + ``` +* `repo`: change current directory to given git repository in parameter. + If not parameter is provided, script will launch a menu to select one with `fzf`. + New directory will take effect for new created panes / windows after `repo` command. + ```bash + # change current directory ti ephase:nix repo + create-tmux-session.sh -n mysession -r blog repo:ephase/nix neww + ``` + +## Example + +Create a new session for my blog repository, launch Neovim, split pane to create a terminal pane bellow text editor then create a new window to lanche development server + +```bash +create-tmux-session.sh -n blog -r ephase/blog 'run:nvim .' vsplit neww 'run:task serve' +``` + +Create a new session for my blog like previous example but open new windows with my nix repository and launch Neovim + +```bash +create-tmux-session.sh -n blog -r ephase/blog 'run:nvim .' vsplit neww 'run:task serve' repo:ephase/nix neww:nix 'run:nvim .' +``` diff --git a/modules/home-manager/cli/tmux/files/create-tmux-session.sh b/modules/home-manager/cli/tmux/files/create-tmux-session.sh index 4e242eb..ae1e744 100755 --- a/modules/home-manager/cli/tmux/files/create-tmux-session.sh +++ b/modules/home-manager/cli/tmux/files/create-tmux-session.sh @@ -5,6 +5,47 @@ set -o pipefail DEBUG=0 declare -a COMMANDS +help() { + cat < -i -p + +-n --name define a session name +-i --id define a session id, +-r --repo define the repo used as base directory if not defined + 'ghq' and 'fzf' will be use to let you select one + + 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) + -p | --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 terraform; 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 From 0ac07880c0529f46affb983ba972367fe99264c7 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 11 May 2025 23:43:49 +0200 Subject: [PATCH 2/4] feat(tmux): improve session creation script Add a simpre README to document it --- modules/home-manager/cli/tmux/files/README.md | 68 ++++++++++++++++++ .../cli/tmux/files/create-tmux-session.sh | 71 +++++++++++++++++-- 2 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 modules/home-manager/cli/tmux/files/README.md diff --git a/modules/home-manager/cli/tmux/files/README.md b/modules/home-manager/cli/tmux/files/README.md new file mode 100644 index 0000000..45a2b50 --- /dev/null +++ b/modules/home-manager/cli/tmux/files/README.md @@ -0,0 +1,68 @@ +# Create a tmux session + +This script help me to create a tmux session and manage related windows, panes and commands. + +## Usage + +``` +create-tmux-session -n -i -p + +-n --name define a session name +-i --id define a session id, +-r --repo define the repo used as base directory if not defined + 'ghq' and 'fzf' will be use to let you select one + + a command launched in the new created session. you can + use it many time. +``` + +## Commands + +Commands create windows and panes, launch program and change current directory related to git project (managed by [ghq][ghq]). +Each command can take a parameter which is mandatpry if no default value exists. +If parameter contains spaces, must be surrounded by quotes / double quote. + +* `vsplit`: split current pane vertically, parameter define the vertical size of the created pane in percent (default 20). + ```bash + # split first windows, new created pane will take 50% of vertical space + create-tmux-session.sh -n mysession vsplit:50 + ``` +* `hsplit`: split current pane horizontally + ```bash + # split first windows, new created pane will take 50% of horizontal space + create-tmux-session.sh -n mysession hsplit:50 + ``` +* `neww`: create a new windows, parameter define the new windows name (not mandatory) + ```bash + # Create a new window name 'shell' + create-tmux-session.sh -n mysession -r blog neww:shell + ``` +* `run`: launch a command in last created element (window / pane ), parameter define command. + ```bash + # Just launch ls + create-tmux-session.sh -n mysession -r blog run:ls + + # This time with parameters + create-tmux-session.sh -n mysession -r blog "run:ls -la" + ``` +* `repo`: change current directory to given git repository in parameter. + If not parameter is provided, script will launch a menu to select one with `fzf`. + New directory will take effect for new created panes / windows after `repo` command. + ```bash + # change current directory ti ephase:nix repo + create-tmux-session.sh -n mysession -r blog repo:ephase/nix neww + ``` + +## Example + +Create a new session for my blog repository, launch Neovim, split pane to create a terminal pane bellow text editor then create a new window to lanche development server + +```bash +create-tmux-session.sh -n blog -r ephase/blog 'run:nvim .' vsplit neww 'run:task serve' +``` + +Create a new session for my blog like previous example but open new windows with my nix repository and launch Neovim + +```bash +create-tmux-session.sh -n blog -r ephase/blog 'run:nvim .' vsplit neww 'run:task serve' repo:ephase/nix neww:nix 'run:nvim .' +``` diff --git a/modules/home-manager/cli/tmux/files/create-tmux-session.sh b/modules/home-manager/cli/tmux/files/create-tmux-session.sh index 4e242eb..7100d39 100755 --- a/modules/home-manager/cli/tmux/files/create-tmux-session.sh +++ b/modules/home-manager/cli/tmux/files/create-tmux-session.sh @@ -5,6 +5,47 @@ set -o pipefail DEBUG=0 declare -a COMMANDS +help() { + cat < -i -p + +-n --name define a session name +-i --id define a session id, +-r --repo define the repo used as base directory if not defined + 'ghq' and 'fzf' will be use to let you select one + + 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) + -p | --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 From 201c8dacaa54baae33c002582c7f80e174f1ac24 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Mon, 12 May 2025 08:42:36 +0200 Subject: [PATCH 3/4] chore(morty): create tmux keybindings for shortcuts --- hosts/work/home-config.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/hosts/work/home-config.nix b/hosts/work/home-config.nix index 4ba15cc..70820e8 100644 --- a/hosts/work/home-config.nix +++ b/hosts/work/home-config.nix @@ -19,7 +19,14 @@ }; neovim.enable = true; starship.enable = true; - tmux.enable = true; + tmux = { + enable = true; + extraConfig = '' + bind -n M-F9 run 'create-tmux-session -n quipu -p quipuapp "run:nvim ." project:quipu-infrastructure neww:quipu-infra project:sellsy hsplit:50 run:k9s project:charts neww:charts' + bind -n M-F8 run 'create-tmux-session -n verifactu -p verifactu "run:nvim ." project:verifactu-infrastructure neww:verifactu-infra project:sellsy hsplit:50 run:k9s project:charts neww:charts' + ''; + }; + utils.enable = true; vifm.enable = true; zellij.enable = true; From d1805654f7fde853b45c163397735c9e8eb1ce99 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Mon, 12 May 2025 08:52:47 +0200 Subject: [PATCH 4/4] chore(work): update session commands regarding session script modifications --- hosts/work/home-config.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hosts/work/home-config.nix b/hosts/work/home-config.nix index 70820e8..94033f8 100644 --- a/hosts/work/home-config.nix +++ b/hosts/work/home-config.nix @@ -22,8 +22,8 @@ tmux = { enable = true; extraConfig = '' - bind -n M-F9 run 'create-tmux-session -n quipu -p quipuapp "run:nvim ." project:quipu-infrastructure neww:quipu-infra project:sellsy hsplit:50 run:k9s project:charts neww:charts' - bind -n M-F8 run 'create-tmux-session -n verifactu -p verifactu "run:nvim ." project:verifactu-infrastructure neww:verifactu-infra project:sellsy hsplit:50 run:k9s project:charts neww:charts' + bind -n M-F9 run 'create-tmux-session -n quipu -r quipuapp "run:nvim ." repo:quipu-infrastructure neww:quipu-infra p:sellsy hsplit:50 run:k9s repo:charts neww:charts' + bind -n M-F8 run 'create-tmux-session -n verifactu -r verifactu "run:nvim ." repo:verifactu-infrastructure neww:verifactu-infra repo:sellsy hsplit:50 run:k9s repo:charts neww:charts' ''; };