diff --git a/tmux/session-manager.sh b/tmux/session-manager.sh index 43424bc..92f71a1 100755 --- a/tmux/session-manager.sh +++ b/tmux/session-manager.sh @@ -20,8 +20,8 @@ show_usage() { Usage: $0 [OPTIONS] [SESSION_TYPE] Session Types: - main - General purpose session with system monitoring - dev - Development-focused session with project tools + main - btop, home (~ + neo), projects (~/Projects + lls) + dev - btop, home (~ + neo), projects (~/Projects + lls), git (~/Projects) list - List all active sessions kill - Kill specified session killall - Kill all sessions @@ -41,113 +41,37 @@ Examples: EOF } -# Create main session +# Create main session (aligned with setup-main.sh) create_main_session() { local session_name="${MAIN_SESSION}" log "${session_name}" "Creating main session: ${session_name}" - create_tmux_session "${session_name}" + if ! tmux new-session -d -s "${session_name}" -c "${HOME}" -n 'btop'; then + error_exit "Failed to create tmux session: ${session_name}" + fi + configure_session_options "${session_name}" "green" + create_main_session_windows "${session_name}" - # Window 0: Home/Overview - create_window "${session_name}" "home" 0 "${HOME}" \ - "clear" \ - 'echo "Welcome to Main Session!"' \ - 'echo "Available commands: htop, nvtop, neofetch"' \ - "pwd" - - # Window 1: Documentation - create_window "${session_name}" "docs" 1 "${HOME}/Projects/documentation" \ - "clear" \ - 'echo "Documentation workspace"' \ - "ls -la" - - # Window 2: Projects - create_window "${session_name}" "projects" 2 "${HOME}/Projects" \ - "clear" \ - 'echo "Projects workspace"' \ - "ls -la" - - # Window 3: System monitoring - create_window "${session_name}" "monitor" 3 "" \ - "clear" \ - 'echo "System monitoring - press Ctrl+C to exit htop"' \ - "htop" - - # Window 4: Development tools - create_window "${session_name}" "tools" 4 "" \ - "clear" \ - 'echo "Development tools workspace"' \ - 'echo "Available: git, docker, kubectl, etc."' \ - "pwd" - - log "${session_name}" "Main session created successfully with 5 windows" + log "${session_name}" "Main session created (windows: btop, home, projects)" } -# Create development session +# Create development session (aligned with setup-dev.sh) create_dev_session() { local session_name="${DEV_SESSION}" log "${session_name}" "Creating development session: ${session_name}" - create_tmux_session "${session_name}" + if ! tmux new-session -d -s "${session_name}" -c "${HOME}" -n 'btop'; then + error_exit "Failed to create tmux session: ${session_name}" + fi + configure_session_options "${session_name}" "cyan" + create_main_session_windows "${session_name}" + + tmux new-window -t "${session_name}" -n 'git' -c "${HOME}/Projects" + prepare_tmux_pane "${session_name}:git" "${HOME}/Projects" - # Window 0: Development Home - create_window "${session_name}" "dev-home" 0 "" \ - "clear" \ - 'echo "Development Session Ready!"' \ - 'echo "Quick commands: git status, docker ps, kubectl get pods"' \ - "pwd" - - # Window 1: Active Project (with split panes) - create_window "${session_name}" "project" 1 "${HOME}/Projects" \ - "clear" \ - 'echo "Active project workspace"' \ - "ls -la" - - # Split the project window horizontally - split_window_horizontal "${session_name}" 1 \ - 'echo "Right pane - logs, monitoring, etc."' \ - "pwd" - - # Window 2: Git/Version Control - create_window "${session_name}" "git" 2 "" \ - "clear" \ - 'echo "Git workspace - check status, branches, etc."' \ - "git status 2>/dev/null || echo 'Not in a git repository'" - - # Window 3: Docker/Containers - create_window "${session_name}" "docker" 3 "" \ - "clear" \ - 'echo "Docker workspace"' \ - "docker ps 2>/dev/null || echo 'Docker not running or not installed'" - - # Window 4: Testing/CI - create_window "${session_name}" "test" 4 "" \ - "clear" \ - 'echo "Testing workspace"' \ - 'echo "Run tests, linting, etc. here"' \ - "pwd" - - # Window 5: Database/Backend - create_window "${session_name}" "db" 5 "" \ - "clear" \ - 'echo "Database workspace"' \ - 'echo "Connect to databases, run queries, etc."' \ - "pwd" - - # Window 6: Logs/Monitoring - create_window "${session_name}" "logs" 6 "" \ - "clear" \ - 'echo "Logs and monitoring"' \ - 'echo "Tail logs, monitor system, etc."' \ - "pwd" - - # Set up window layout for project window (1) - tmux select-window -t "${session_name}:1" - tmux select-pane -t "${session_name}:1.0" - - log "${session_name}" "Development session created successfully with 7 windows" + log "${session_name}" "Development session created (windows: btop, home, projects, git)" } # Handle session creation or attachment @@ -170,7 +94,7 @@ handle_session() { error_exit "Unknown session type: ${session_type}" ;; esac - tmux select-window -t "${session_name}:0" + tmux select-window -t "${session_name}:home" attach_to_session "${session_name}" fi } diff --git a/tmux/setup-dev.sh b/tmux/setup-dev.sh index dac6a30..209964a 100755 --- a/tmux/setup-dev.sh +++ b/tmux/setup-dev.sh @@ -1,136 +1,48 @@ #!/bin/bash -# Development tmux session setup script -# Creates or reattaches to a development session with development-focused windows +# Development tmux session — main layout plus git (index 3, ~/Projects) -set -euo pipefail # Exit on error, undefined vars, pipe failures +set -euo pipefail -# Configuration -readonly SESSION_NAME='dev' -readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -readonly LOG_FILE="${HOME}/.tmux-session-${SESSION_NAME}.log" - -# Colors for output -readonly RED='\033[0;31m' -readonly GREEN='\033[0;32m' -readonly YELLOW='\033[1;33m' -readonly BLUE='\033[0;34m' -readonly NC='\033[0m' # No Color - -# Logging function -log() { - echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $*" | tee -a "${LOG_FILE}" -} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=tmux-utils.sh +source "${SCRIPT_DIR}/tmux-utils.sh" -# Error handling -error_exit() { - echo -e "${RED}Error: $1${NC}" >&2 - exit 1 -} - -# Check if tmux is installed -if ! command -v tmux >/dev/null 2>&1; then - error_exit "tmux is not installed. Please install tmux first." -fi +readonly SESSION_NAME='dev' -# Function to create new session with windows create_session() { - log "Creating new development tmux session: ${SESSION_NAME}" + log "${SESSION_NAME}" "Creating new tmux session: ${SESSION_NAME}" - # Create detached session - if ! tmux new-session -d -s "${SESSION_NAME}" 2>/dev/null; then + if ! tmux new-session -d -s "${SESSION_NAME}" -c "${HOME}" -n 'btop'; then error_exit "Failed to create tmux session: ${SESSION_NAME}" fi - # Configure session options for development - tmux set-option -t "${SESSION_NAME}" -g status on - tmux set-option -t "${SESSION_NAME}" -g status-interval 1 - tmux set-option -t "${SESSION_NAME}" -g status-left-length 20 - tmux set-option -t "${SESSION_NAME}" -g status-right-length 50 - tmux set-option -t "${SESSION_NAME}" -g status-left '#[fg=cyan]#S #[fg=white]| ' - tmux set-option -t "${SESSION_NAME}" -g status-right '#[fg=yellow]%Y-%m-%d %H:%M:%S' - tmux set-option -t "${SESSION_NAME}" -g default-terminal "screen-256color" - - # Window 0: Development Home - tmux rename-window -t "${SESSION_NAME}:0" 'dev-home' - tmux send-keys -t "${SESSION_NAME}:0" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:0" 'echo "Development Session Ready!"' C-m - tmux send-keys -t "${SESSION_NAME}:0" 'echo "Quick commands: git status, docker ps, kubectl get pods"' C-m - tmux send-keys -t "${SESSION_NAME}:0" 'pwd' C-m - - # Window 1: Active Project (with split panes) - tmux new-window -t "${SESSION_NAME}" -n 'project' - tmux send-keys -t "${SESSION_NAME}:1" 'cd "${HOME}/Projects" 2>/dev/null || cd "${HOME}"' C-m - tmux send-keys -t "${SESSION_NAME}:1" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:1" 'echo "Active project workspace"' C-m - tmux send-keys -t "${SESSION_NAME}:1" 'ls -la' C-m - - # Split the project window horizontally - tmux split-window -t "${SESSION_NAME}:1" -h - tmux send-keys -t "${SESSION_NAME}:1.1" 'echo "Right pane - logs, monitoring, etc."' C-m - tmux send-keys -t "${SESSION_NAME}:1.1" 'pwd' C-m - - # Window 2: Git/Version Control - tmux new-window -t "${SESSION_NAME}" -n 'git' - tmux send-keys -t "${SESSION_NAME}:2" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:2" 'echo "Git workspace - check status, branches, etc."' C-m - tmux send-keys -t "${SESSION_NAME}:2" 'git status 2>/dev/null || echo "Not in a git repository"' C-m - - # Window 3: Docker/Containers - tmux new-window -t "${SESSION_NAME}" -n 'docker' - tmux send-keys -t "${SESSION_NAME}:3" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:3" 'echo "Docker workspace"' C-m - tmux send-keys -t "${SESSION_NAME}:3" 'docker ps 2>/dev/null || echo "Docker not running or not installed"' C-m - - # Window 4: Testing/CI - tmux new-window -t "${SESSION_NAME}" -n 'test' - tmux send-keys -t "${SESSION_NAME}:4" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:4" 'echo "Testing workspace"' C-m - tmux send-keys -t "${SESSION_NAME}:4" 'echo "Run tests, linting, etc. here"' C-m - tmux send-keys -t "${SESSION_NAME}:4" 'pwd' C-m - - # Window 5: Database/Backend - tmux new-window -t "${SESSION_NAME}" -n 'db' - tmux send-keys -t "${SESSION_NAME}:5" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:5" 'echo "Database workspace"' C-m - tmux send-keys -t "${SESSION_NAME}:5" 'echo "Connect to databases, run queries, etc."' C-m - tmux send-keys -t "${SESSION_NAME}:5" 'pwd' C-m - - # Window 6: Logs/Monitoring - tmux new-window -t "${SESSION_NAME}" -n 'logs' - tmux send-keys -t "${SESSION_NAME}:6" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:6" 'echo "Logs and monitoring"' C-m - tmux send-keys -t "${SESSION_NAME}:6" 'echo "Tail logs, monitor system, etc."' C-m - tmux send-keys -t "${SESSION_NAME}:6" 'pwd' C-m + configure_session_options "${SESSION_NAME}" "cyan" + create_main_session_windows "${SESSION_NAME}" - # Set up window layout for project window (1) - tmux select-window -t "${SESSION_NAME}:1" - tmux select-pane -t "${SESSION_NAME}:1.0" + tmux new-window -t "${SESSION_NAME}" -n 'git' -c "${HOME}/Projects" + prepare_tmux_pane "${SESSION_NAME}:git" "${HOME}/Projects" - log "Development session ${SESSION_NAME} created successfully with 7 windows" + log "${SESSION_NAME}" "Session ${SESSION_NAME} created (windows: btop, home, projects, git)" } -# Function to attach to existing session attach_session() { - log "Attaching to existing development session: ${SESSION_NAME}" + log "${SESSION_NAME}" "Attaching to existing session: ${SESSION_NAME}" tmux attach-session -t "${SESSION_NAME}" } -# Main execution main() { - log "Starting development tmux session manager for: ${SESSION_NAME}" + log "${SESSION_NAME}" "Starting tmux setup for: ${SESSION_NAME}" + check_tmux_installed - # Check if session exists - if tmux has-session -t "${SESSION_NAME}" 2>/dev/null; then - log "Development session ${SESSION_NAME} already exists" + if session_exists "${SESSION_NAME}"; then + log "${SESSION_NAME}" "Session ${SESSION_NAME} already exists" attach_session else create_session - # Select first window and attach - tmux select-window -t "${SESSION_NAME}:0" + tmux select-window -t "${SESSION_NAME}:home" attach_session fi } -# Run main function main "$@" diff --git a/tmux/setup-main.sh b/tmux/setup-main.sh index c845b40..f1fb6a0 100755 --- a/tmux/setup-main.sh +++ b/tmux/setup-main.sh @@ -1,113 +1,45 @@ #!/bin/bash -# Main tmux session setup script -# Creates or reattaches to a main development session with organized windows +# Main tmux session setup script — three windows: btop, home (~ + neo), projects (~/Projects + lls) -set -euo pipefail # Exit on error, undefined vars, pipe failures +set -euo pipefail -# Configuration -readonly SESSION_NAME='main' -readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -readonly LOG_FILE="${HOME}/.tmux-session-${SESSION_NAME}.log" - -# Colors for output -readonly RED='\033[0;31m' -readonly GREEN='\033[0;32m' -readonly YELLOW='\033[1;33m' -readonly BLUE='\033[0;34m' -readonly NC='\033[0m' # No Color - -# Logging function -log() { - echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $*" | tee -a "${LOG_FILE}" -} - -# Error handling -error_exit() { - echo -e "${RED}Error: $1${NC}" >&2 - exit 1 -} +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=tmux-utils.sh +source "${SCRIPT_DIR}/tmux-utils.sh" -# Check if tmux is installed -if ! command -v tmux >/dev/null 2>&1; then - error_exit "tmux is not installed. Please install tmux first." -fi +readonly SESSION_NAME='main' -# Function to create new session with windows create_session() { - log "Creating new tmux session: ${SESSION_NAME}" + log "${SESSION_NAME}" "Creating new tmux session: ${SESSION_NAME}" - # Create detached session - if ! tmux new-session -d -s "${SESSION_NAME}" 2>/dev/null; then + if ! tmux new-session -d -s "${SESSION_NAME}" -c "${HOME}" -n 'btop'; then error_exit "Failed to create tmux session: ${SESSION_NAME}" fi - # Configure session options - tmux set-option -t "${SESSION_NAME}" -g status on - tmux set-option -t "${SESSION_NAME}" -g status-interval 1 - tmux set-option -t "${SESSION_NAME}" -g status-left-length 20 - tmux set-option -t "${SESSION_NAME}" -g status-right-length 50 - tmux set-option -t "${SESSION_NAME}" -g status-left '#[fg=green]#S #[fg=white]| ' - tmux set-option -t "${SESSION_NAME}" -g status-right '#[fg=yellow]%Y-%m-%d %H:%M:%S' - - # Window 0: Home/Overview - tmux rename-window -t "${SESSION_NAME}:0" 'home' - tmux send-keys -t "${SESSION_NAME}:0" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:0" 'echo "Welcome to Main Session!"' C-m - tmux send-keys -t "${SESSION_NAME}:0" 'echo "Available commands: htop, nvtop, neofetch"' C-m - tmux send-keys -t "${SESSION_NAME}:0" 'pwd' C-m - - # Window 1: Documentation - tmux new-window -t "${SESSION_NAME}" -n 'docs' - tmux send-keys -t "${SESSION_NAME}:1" 'cd "${HOME}/Projects/documentation" 2>/dev/null || cd "${HOME}/Documents" 2>/dev/null || cd "${HOME}"' C-m - tmux send-keys -t "${SESSION_NAME}:1" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:1" 'echo "Documentation workspace"' C-m - tmux send-keys -t "${SESSION_NAME}:1" 'ls -la' C-m - - # Window 2: Projects - tmux new-window -t "${SESSION_NAME}" -n 'projects' - tmux send-keys -t "${SESSION_NAME}:2" 'cd "${HOME}/Projects" 2>/dev/null || cd "${HOME}"' C-m - tmux send-keys -t "${SESSION_NAME}:2" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:2" 'echo "Projects workspace"' C-m - tmux send-keys -t "${SESSION_NAME}:2" 'ls -la' C-m - - # Window 3: System monitoring - tmux new-window -t "${SESSION_NAME}" -n 'monitor' - tmux send-keys -t "${SESSION_NAME}:3" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:3" 'echo "System monitoring - press Ctrl+C to exit htop"' C-m - tmux send-keys -t "${SESSION_NAME}:3" 'htop' C-m - - # Window 4: Development tools - tmux new-window -t "${SESSION_NAME}" -n 'tools' - tmux send-keys -t "${SESSION_NAME}:4" 'clear' C-m - tmux send-keys -t "${SESSION_NAME}:4" 'echo "Development tools workspace"' C-m - tmux send-keys -t "${SESSION_NAME}:4" 'echo "Available: git, docker, kubectl, etc."' C-m - tmux send-keys -t "${SESSION_NAME}:4" 'pwd' C-m + configure_session_options "${SESSION_NAME}" "green" + create_main_session_windows "${SESSION_NAME}" - log "Session ${SESSION_NAME} created successfully with 5 windows" + log "${SESSION_NAME}" "Session ${SESSION_NAME} created (windows: btop, home, projects)" } -# Function to attach to existing session attach_session() { - log "Attaching to existing session: ${SESSION_NAME}" + log "${SESSION_NAME}" "Attaching to existing session: ${SESSION_NAME}" tmux attach-session -t "${SESSION_NAME}" } -# Main execution main() { - log "Starting tmux session manager for: ${SESSION_NAME}" + log "${SESSION_NAME}" "Starting tmux setup for: ${SESSION_NAME}" + check_tmux_installed - # Check if session exists - if tmux has-session -t "${SESSION_NAME}" 2>/dev/null; then - log "Session ${SESSION_NAME} already exists" + if session_exists "${SESSION_NAME}"; then + log "${SESSION_NAME}" "Session ${SESSION_NAME} already exists" attach_session else create_session - # Select first window and attach - tmux select-window -t "${SESSION_NAME}:0" + tmux select-window -t "${SESSION_NAME}:home" attach_session fi } -# Run main function main "$@" diff --git a/tmux/tmux-utils.sh b/tmux/tmux-utils.sh index 6c3042e..683a38b 100755 --- a/tmux/tmux-utils.sh +++ b/tmux/tmux-utils.sh @@ -11,9 +11,11 @@ readonly BLUE='\033[0;34m' readonly CYAN='\033[0;36m' readonly NC='\033[0m' # No Color -# Logging function +# Logging function — first argument is session key for ~/.tmux-session-.log log() { - local log_file="${HOME}/.tmux-session-${1:-general}.log" + local session_key="${1:-general}" + shift + local log_file="${HOME}/.tmux-session-${session_key}.log" echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $*" | tee -a "${log_file}" } @@ -50,13 +52,61 @@ configure_session_options() { local session_name="$1" local status_color="${2:-green}" - tmux set-option -t "${session_name}" -g status on - tmux set-option -t "${session_name}" -g status-interval 1 - tmux set-option -t "${session_name}" -g status-left-length 20 - tmux set-option -t "${session_name}" -g status-right-length 50 - tmux set-option -t "${session_name}" -g status-left "#[fg=${status_color}]#S #[fg=white]| " - tmux set-option -t "${session_name}" -g status-right '#[fg=yellow]%Y-%m-%d %H:%M:%S' - tmux set-option -t "${session_name}" -g default-terminal "screen-256color" + # Per-session options only (-g here would set global defaults for all sessions) + tmux set-option -t "${session_name}" status on + tmux set-option -t "${session_name}" status-interval 1 + tmux set-option -t "${session_name}" status-left-length 20 + tmux set-option -t "${session_name}" status-right-length 50 + tmux set-option -t "${session_name}" status-left "#[fg=${status_color}]#S #[fg=white]| " + tmux set-option -t "${session_name}" status-right '#[fg=yellow]%Y-%m-%d %H:%M:%S' + tmux set-option -t "${session_name}" default-terminal "screen-256color" +} + +# Source interactive shell config (themes, aliases) in a tmux pane. +source_user_runtime_config() { + local target="$1" + local shell_base="${SHELL##*/}" + local rc_file="" + + case "${shell_base}" in + zsh) rc_file="${HOME}/.zshrc" ;; + bash) rc_file="${HOME}/.bashrc" ;; + *) return 0 ;; + esac + + [[ -f "${rc_file}" ]] && + tmux send-keys -t "${target}" "source \"${rc_file}\"" C-m +} + +# Shared main layout: btop (0), home (1), projects (2). +create_main_session_windows() { + local session_name="$1" + + prepare_tmux_pane "${session_name}:btop" "${HOME}" 'btop' + + tmux new-window -t "${session_name}" -n 'home' -c "${HOME}" + prepare_tmux_pane "${session_name}:home" "${HOME}" 'clear' 'neo' + + tmux new-window -t "${session_name}" -n 'projects' -c "${HOME}/Projects" + prepare_tmux_pane "${session_name}:projects" "${HOME}/Projects" 'clear' 'lls' +} + +# Prime a pane: runtime config, explicit cwd, then startup commands (e.g. clear, neo). +prepare_tmux_pane() { + local target="$1" + local working_dir="$2" + shift 2 + + sleep 0.15 + source_user_runtime_config "${target}" + + if [[ -n "${working_dir}" ]]; then + tmux send-keys -t "${target}" "cd \"${working_dir}\" || cd \"${HOME}\"" C-m + fi + + for cmd in "$@"; do + tmux send-keys -t "${target}" "${cmd}" C-m + done } # Send keys to a tmux window