From 589090ad044c779ee0d253eb57bad212aab632c7 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Thu, 9 Dec 2021 15:43:55 +0200 Subject: [PATCH] Some functions for grid/IDE tmux layouts. IDE: ` $ ide [name_of_workspace]` Grid: ` $ grid 2x3` --- files/dotfiles/shellconfig/common.sh | 91 ++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/files/dotfiles/shellconfig/common.sh b/files/dotfiles/shellconfig/common.sh index 74b6613..34310e0 100644 --- a/files/dotfiles/shellconfig/common.sh +++ b/files/dotfiles/shellconfig/common.sh @@ -38,4 +38,95 @@ export EDITOR=vim; # Shell functions ############################################ +function pre_check_tmux_for_layout() { + ERROR_PREFIX="[ERROR]: " + + if [[ -z $(which tmux) ]]; then + echo "" + echo "$ERROR_PREFIX Missing dependencie of tmux"; + echo "" + return 1 + fi + + if [[ -z ${TMUX-} ]];then + echo "" + echo "$ERROR_PREFIX Must run in a tmux session"; + echo "" + return 1 + fi + + if [ $(tmux list-panes | wc -l) -ne 1 ]; then + echo "" + echo "$ERROR_PREFIX Please run '$1' in a clean tmux window (No panes)" + echo "" + return 1 + fi +} +# Creates a tmux layout of 3 columns with mid col has a vertical split for use as well +# Usage: $ ide +# $ ide name_for_workspace +function ide() { + CURRENT_DIR=$(echo "${PWD##*/}") + IDE_NAME="[⌨️ IDE: ${1:=$CURRENT_DIR}]" + + pre_check_tmux_for_layout "ide" || return 1 + + tmux send-keys "l" C-m + tmux split-window -h -p 80 + tmux split-window -h -p 30 + tmux send-keys "lazygit" C-m + tmux select-pane -t 1 + tmux split-window -v -p 20 + tmux select-pane -t 1 + tmux send-keys "vim" C-m + tmux rename-window ${IDE_NAME} +} + +# Create a tmux grid layout. default 2x2 +# Usage: $ grid +# $ grid 3x3 +function grid() { + #WIP + pre_check_tmux_for_layout "grid" || return 1 + INPUT=${1:=0x0} + GRID_COLS=$(echo $INPUT | grep -oP '^[\d]+') + GRID_ROWS=$(echo $INPUT | grep -oP '[\d]+$') + echo "COLS: $GRID_COLS" + echo "ROWS: $GRID_ROWS" + if [[ $GRID_ROWS == "0" ]] || [[ $GRID_COLS == "0" ]]; then + echo "Defaulting to 2x2 grid" + GRID_COLS="2" + GRID_ROWS="2" + fi + V_PANES=$((GRID_ROWS)) + H_PANES=$((GRID_COLS)) + V_RATIO=$((100 / V_PANES)) + H_RATIO=$((100 / H_PANES)) + + echo "COLS: $GRID_COLS" + echo "ROWS: $GRID_ROWS" + + for h in {1..$((H_PANES - 1))} + do + H_SIZE=$((100 - (h * H_RATIO))) + tmux split-window -h -p $((H_SIZE)) + echo " $h H_SIZE $H_SIZE" + done + + for cell in {$((H_PANES - 1))..0} + do + tmux select-pane -t ${cell} + CURRENT_PANE=$(tmux list-panes | grep '(active)$' | grep -oP '^[\d]+') + echo Current pane: $CURRENT_PANE + for v in {1..$((V_PANES - 1))} + do + V_SIZE=$((100 - (v * V_RATIO))) + tmux split-window -v -p $((V_SIZE)) + echo " $v V_SIZE $V_SIZE" + done + done + + # Select top left cell (Starting point) + tmux select-pane -t 0 +}