# ------------------
#  General Settings
# ------------------

set-option -g default-shell "/bin/zsh"

# See: https://github.com/gpakosz/.tmux/blob/master/.tmux.conf

# Set Custom Prefix
set -g prefix C-a
unbind C-b


set -sg escape-time 1

# Increase scrollback buffer size from 2000 to 50000 lines
set -g history-limit 50000

set -g base-index 1           # start windows numbering at 1
setw -g pane-base-index 1     # make pane numbering consistent with windows

set -q -g status-utf8 on      # expect UTF-8 (tmux < 2.2)
setw -q -g utf8 on

# Focus events enabled for terminals that support them
set -g focus-events on

# Super useful when using "grouped sessions" and multi-monitor setup
setw -g aggressive-resize on

# -------------------
#   Visual Settings
# -------------------

set -g default-terminal "screen-256color"
source-file ~/.config/tmux/themes/tomorrow-night.tmux

# Theme Variables

TMUX_ACTIVE_COLOR="#373b41"

TMUX_STATUS_TEXT_COLOR="#c5c8c6"
TMUX_STATUS_BG="#26292c"

TMUX_NAMESPACE_FG="#000000"
TMUX_STATUS_RED="#cc6666"

TMUX_USERNAME_FG="#000000"
TMUX_STATUS_AQUA="#8abeb7"

# Styles
# STYLE_DEFAULT="fg=white,bg=$TMUX_STATUS_BG"
STYLE_DEFAULT="fg=white"
STYLE_ACTIVE_ITEM="#[fg=$TMUX_STATUS_TEXT_COLOR,bg=$TMUX_ACTIVE_COLOR,noreverse,bold]"
STYLE_RED="#[fg=$TMUX_NAMESPACE_FG,bg=$TMUX_STATUS_RED,bold]"
STYLE_AQUA="#[fg=$TMUX_USERNAME_FG,bg=$TMUX_STATUS_AQUA,bold]"

# Status bar
set-option -g status-position bottom

# Refresh 'status-left' and 'status-right' more often, from every 15s to 5s
set -g status-interval 5

# Status mid style
set -g status-style $STYLE_DEFAULT

set -g status-left "#{prefix_highlight}$STYLE_RED #S #[fg=$TMUX_STATUS_RED,bg=$TMUX_STATUS_AQUA,bold]$STYLE_AQUA #(whoami) #[fg=$TMUX_STATUS_AQUA,bg=$TMUX_STATUS_BG,bold] "
set -g status-left-length 30

PC_STATS="$STYLE_ACTIVE_ITEM cpu #{cpu_icon} #{cpu_percentage} #[$STYLE_DEFAULT] $STYLE_ACTIVE_ITEM mem #{ram_icon} #{ram_percentage} #[$STYLE_DEFAULT]"

set -g status-right "$PC_STATS #[fg=$TMUX_STATUS_AQUA,bg=$TMUX_STATUS_BG,bold]$STYLE_AQUA #h #[fg=$TMUX_STATUS_RED,bg=$TMUX_STATUS_AQUA,bold]$STYLE_RED %a, %h %d %H:%M "
set -g status-right-length 90

# window name options
# set -g window-status-format "#[$STATUS_DEFAILT] #I #W "
# set -g window-status-current-format "$TYLE_ACTIVE_ITEM] #I #W "

# Prefix style
# https://github.com/tmux-plugins/tmux-prefix-highlight
set -g @prefix_highlight_fg $TMUX_STATUS_TEXT_COLOR
set -g @prefix_highlight_bg $TMUX_ACTIVE_COLOR

# --------------------
#  Custom keybindings
# --------------------

set -g mouse on
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'copy-mode -e'"

setw -g mode-keys vi
bind C-a send-prefix

# Unbind unused defaults
unbind %
unbind [
unbind p

unbind Up
unbind Down
unbind Left
unbind Right

unbind M-Up
unbind M-Down
unbind M-Left
unbind M-Right

unbind C-Up
unbind C-Down
unbind C-Left
unbind C-Right

# Source this file
bind r source-file $HOME/.tmux.conf \; display "Config file reloaded!"

# split current window horizontally
bind v split-window -h
bind | split-window -h

# split current window vertically
bind s split-window -v
bind - split-window -v

# Swap panes
bind-key -r "<" swap-window -d -t -1
bind-key -r ">" swap-window -d -t +1
bind-key -r n swap-pane -U

# Move between panes
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Move between windows
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+

# Resize panes
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

# Copy & Paste
bind Escape copy-mode
bind b list-buffers   # list paste buffers
bind p paste-buffer   # paste last buffer
bind P choose-buffer  # see all buffers
bind C-c run-shell "bash -c 'tmux show-buffer | pbcopy'" \; display 'Copied to clipboard'


# Tmux Plugin manager
# https://github.com/tmux-plugins/tpm

# Enabled plugins
# install         prefix I
# update          prefix U
# uninstall all   prefix alt u
set-environment -g TMUX_PLUGIN_MANAGER_PATH '~/.config/tmux/plugins'
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-cpu'
set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
set -g @plugin 'sainnhe/tmux-fzf'

run -b '~/.config/tmux/plugins/tpm/tpm'
