# shell
set -g default-command $SHELL
set -g default-shell $SHELL

# start with window 1 (for keybinds)
set -g base-index 1

# renumber windows after closing
set -g renumber-windows on

# start with pane 1
set -g pane-base-index 1

# vim style
setw -g mode-keys vi
set -g status-keys vi
bind : command-prompt

# other random bindings
bind x kill-pane
bind t set status

# panes
set -g pane-border-style "fg=color0"
set -g pane-active-border-style "fg=color0"
set -g window-active-style "bg=terminal"
set -g window-style "bg=color235"

# status line
set -g status-justify left
set -g status-style "bg=terminal,fg=color1"
set -g status-interval 2

# window status
set-option -g status-position bottom
setw -g window-status-format " #[bg=color1,fg=color0,noreverse]▓░ #W "
setw -g window-status-current-format " #[bg=color6,fg=color0,noreverse]▓░ #W "

# clock
set -g clock-mode-color color1

# mouse mode
set -g mouse on

# rebind prefix to C-a 
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

# easy reloads
bind r source-file ~/.config/tmux/tmux.conf

# fix escape-time to prevent weird behaviour in vim
set -s escape-time 0

# use C-b s/h for splitting
unbind s
unbind v
bind s split-window -h
bind v split-window -v

# plugins
set -g @plugin "tmux-plugins/tpm"
setenv -g TMUX_PLUGIN_MANAGER_PATH "~/.config/tmux/plugins/"

# nvim/tmux integration
set -g @plugin "aserowy/tmux.nvim"
set -g @tmux-nvim-navigation true
set -g @tmux-nvim-navigation-cycle true
set -g @tmux-nvim-navigation-keybinding-left "C-h"
set -g @tmux-nvim-navigation-keybinding-down "C-j"
set -g @tmux-nvim-navigation-keybinding-up "C-k"
set -g @tmux-nvim-navigation-keybinding-right "C-l"
set -g @tmux-nvim-resize true
set -g @tmux-nvim-resize-step-x 2
set -g @tmux-nvim-resize-step-y 2
set -g @tmux-nvim-resize-keybinding-left 'C-Left'
set -g @tmux-nvim-resize-keybinding-down 'C-Down'
set -g @tmux-nvim-resize-keybinding-up 'C-Up'
set -g @tmux-nvim-resize-keybinding-right 'C-Right'
is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?\.?(view|n?vim?x?)(-wrapped)?(diff)?$'"

bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h' { if -F '#{pane_at_left}' '' 'select-pane -L' }
bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j' { if -F '#{pane_at_bottom}' '' 'select-pane -D' }
bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k' { if -F '#{pane_at_top}' '' 'select-pane -U' }
bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l' { if -F '#{pane_at_right}' '' 'select-pane -R' }

bind-key -T copy-mode-vi 'C-h' if -F '#{pane_at_left}' '' 'select-pane -L'
bind-key -T copy-mode-vi 'C-j' if -F '#{pane_at_bottom}' '' 'select-pane -D'
bind-key -T copy-mode-vi 'C-k' if -F '#{pane_at_top}' '' 'select-pane -U'
bind-key -T copy-mode-vi 'C-l' if -F '#{pane_at_right}' '' 'select-pane -R'

bind -n 'C-Left' if-shell "$is_vim" 'send-keys C-Left' 'resize-pane -L 1'
bind -n 'C-Down' if-shell "$is_vim" 'send-keys C-Down' 'resize-pane -D 1'
bind -n 'C-Up' if-shell "$is_vim" 'send-keys C-Up' 'resize-pane -U 1'
bind -n 'C-Right' if-shell "$is_vim" 'send-keys C-Right' 'resize-pane -R 1'

bind-key -T copy-mode-vi C-Left resize-pane -L 1
bind-key -T copy-mode-vi C-Down resize-pane -D 1
bind-key -T copy-mode-vi C-Up resize-pane -U 1
bind-key -T copy-mode-vi C-Right resize-pane -R 1

run "~/.config/tmux/plugins/tpm/tpm"
