linux-config/files/dotfiles/shellconfig/common.sh
Sagi Dayan f4f315df42
tmux <prefix>-b to change git branches fast
Uses gitB function from common.sh

Also added confirmation to delete workspace
2022-01-23 15:31:34 +02:00

208 lines
5.1 KiB
Bash

#!/bin/bash
########################################################
# DO NOT EDIT - WILL BE DISCARDED IN NEXT PLAYBOOK RUN #
########################################################
############################################
# Shell aliases
############################################
alias :q=exit
alias :Q=exit
alias ls='lsd'
alias f='printf "\033c"'
alias top=btm
alias t=tmux
############################################
# Environment Variables
############################################
# bat theme (New cat) $ bat --list-themes
gruvbox="gruvbox"
monokai="Monokai Extended"
nord="Nord"
export BAT_THEME="$nord"
export VISUAL=vim;
export EDITOR=vim;
############################################
# Shell functions
############################################
function gitB() {
if [ -d ./.git ]; then
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ $(git branch | wc -l) -lt 2 ]; then
echo "⚠️ Only ${CURRENT_BRANCH} exists."
return 1
fi
BRANCH=$(git branch -l |sed '/\*/d'| fzf --reverse --header="Current Branch: ${CURRENT_BRANCH}. Select Branch to checkout")
if [ -z ${BRANCH} ]; then
echo Canceled
else
BRANCH=$(echo $BRANCH | xargs)
echo "Swiching to ${BRANCH}"
git ck "${BRANCH}"
fi
else
echo "[ERROR]: Not in a git repository"
fi;
}
function huh() {
CURRENT_DIR=$(echo "${PWD##*/}")
TITLE="${1:=$CURRENT_DIR}"
echo "=-=-=-=-=-=-=-=-=-=-=-=-=-="; \
echo "${TITLE}"; \
echo "=-=-=-=-=-=-=-=-=-=-=-=-=-="; \
echo;echo Status:; \
echo -=-=-=-; \
git status; \
echo; \
echo Remotes:; \
echo -=-=-=-=; \
git remote -v | cat; \
echo;echo Files:; \
echo -=-=-=; \
ls -l; \
echo;echo;
}
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
clear;
huh "${IDE_NAME}";
tmux split-window -h -p 80
tmux split-window -h -p 30
tmux send-keys "gitui" 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 layout with one command
# Usage: $ layout "<num_rows> [<num_rows>]+ [layout_name]"
# Examples: $ layout "2" Bob # Single column with 2 rows, named Bob
# $ layout "2 1" # Two columns, left column with 2 rows, right column with ine row
function layout() {
pre_check_tmux_for_layout "layout/grid" || return 1
CURRENT_DIR=$(echo "${PWD##*/}")
LAYOUT_NAME="[#⃣ Layout: ${2:=$CURRENT_DIR}]"
INPUT=${1-}
if [ -z $INPUT ]; then
echo "Error: Invalid layout. Usage: $ layout <layout>"
return 1
fi
COLS=($(echo $INPUT))
# echo "Number of Columns: ${#COLS[@]}"
# Create columns
H_RATIO=$((100 / ${#COLS[@]}))
cols=$((${#COLS[@]} - 1))
if [ $cols -le 0 ]; then
cols=1
else
for col in {1..$(($cols))}
do
H_SIZE=$((100 - (col * H_RATIO)))
tmux split-window -h -p $((H_SIZE))
done
fi
tmux select-pane -t 0
# Create Rows
for col in {1..$(($cols + 1))}
do
if [ $col != 1 ]; then
tmux select-pane -R #Move pane to the right
fi
ROWS=$((COLS[col]))
if [ $((ROWS)) -gt 1 ]; then
V_RATIO=$((100 / (ROWS)))
for row in {1..$((ROWS - 1))}
do
V_SIZE=$((100 - (row * V_RATIO)))
tmux split-window -v -p $((V_SIZE))
done
fi
done
tmux select-pane -t 0
tmux rename-window ${LAYOUT_NAME}
tmux send-keys "clear" C-m
}
# 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
LAYOUT_SCHEME=""
for i in {1..$((GRID_COLS))}
do
LAYOUT_SCHEME="${LAYOUT_SCHEME} ${GRID_ROWS}"
done
layout $LAYOUT_SCHEME $2 || return 1
return 0
}