debian-dots

dotfiles (does the obvious)
git clone [email protected]:dracuxan/debian-dots.git
Log | Files | Refs | README | LICENSE

start_tmux (2479B)


      1 #!/bin/bash
      2 
      3 # Parse command line arguments
      4 force_new=false
      5 
      6 while [[ $# -gt 0 ]]; do
      7     case $1 in
      8     -n | --new)
      9         force_new=true
     10         shift
     11         ;;
     12     *)
     13         echo "Unknown option: $1" >&2
     14         echo "Usage: $0 [-n|--new]" >&2
     15         exit 1
     16         ;;
     17     esac
     18 done
     19 
     20 logs() {
     21     local action=$1
     22     local session_name=$2
     23     local log_dir="$HOME/.tmux_logs"
     24     local log_file="$log_dir/tmux_session.log"
     25     local time_stamp=$(date "+%Y-%m-%d %H:%M:%S")
     26 
     27     # Create log directory if it doesn't exist
     28     mkdir -p "$log_dir" || {
     29         echo "Error: Failed to create log directory '$log_dir'." >&2
     30         return 1
     31     }
     32 
     33     # Check if log file is writable
     34     if ! touch "$log_file" 2>/dev/null; then
     35         echo "Error: Log file '$log_file' is not writable." >&2
     36         return 1
     37     fi
     38 
     39     # Write log entry
     40     echo "$action -> $session_name: $time_stamp" >>"$log_file"
     41 }
     42 
     43 tm() {
     44     # Check if tmux is installed
     45     if ! command -v tmux >/dev/null 2>&1; then
     46         echo "Error: tmux is not installed." >&2
     47         exit 1
     48     fi
     49 
     50     # Check for existing sessions
     51     local existing_sessions
     52     existing_sessions=$(tmux ls 2>/dev/null)
     53 
     54     # Decide whether to create new session or attach to existing
     55     if [ "$force_new" = true ] || [ -z "$existing_sessions" ]; then
     56         # Prompt for session name
     57         if [ "$force_new" = true ] && [ -n "$existing_sessions" ]; then
     58             echo -n "Enter a name for a new session: "
     59         else
     60             echo -n "No sessions found. Enter a name for a new session: "
     61         fi
     62         read -r name
     63         # Validate input
     64         if [ -z "$name" ]; then
     65             echo "Error: Session name cannot be empty." >&2
     66             exit 1
     67         fi
     68         # Log before creating session
     69         logs "new" "$name"
     70         # Create new session
     71         tmux new-session -s "$name" || {
     72             echo "Error: Failed to create session '$name'." >&2
     73             exit 1
     74         }
     75     else
     76         # Get the most recent session name
     77         local session_name
     78         session_name=$(tmux list-sessions -F '#S' | head -n 1) || {
     79             echo "Error: Failed to retrieve session name." >&2
     80             exit 1
     81         }
     82         # Log before attaching
     83         logs "enter" "$session_name"
     84         # Attach to the most recent session
     85         tmux attach || {
     86             echo "Error: Failed to attach to session '$session_name'." >&2
     87             exit 1
     88         }
     89     fi
     90 }
     91 
     92 # Call the tm function
     93 tm