# ~/.zshrc — Primary interactive Zsh shell configuration
#
# This is the main shell config for all interactive zsh sessions on this system.
# It wraps Oh My Zsh (for plugin management and completions), then loads a wide
# set of aliases, utility functions, and environment variables.
#
# Structure:
#   1. GNU grep detection guard (activates OMZ block only on GNU/Linux)
#   2. Oh My Zsh setup and plugin list
#   3. Editor and terminal environment variables
#   4. Aliases for navigation, editors, git, distrobox, python, etc.
#   5. Shell functions: gitf, dbc, dbt, y, n, calc
#   6. Starship prompt init
#   7. PATH additions (spicetify, nvm, pipx)
#

# ─── GNU grep guard ────────────────────────────────────────────────────────────
# Detects whether the system grep is GNU grep (Linux) by checking its --help output.
# If it is GNU grep, the entire Oh My Zsh block runs. On non-GNU systems (macOS BSD,
# for example) this block would be skipped, preventing GNU-only flag errors.
GREPOUTPUT=$( grep --help 2>&1 )
RESULT=$( echo $GREPOUTPUT | grep gnu )
if [ "${RESULT}" != "" ]; then
#source ~/.bash_profile
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH

# ─── Oh My Zsh ─────────────────────────────────────────────────────────────────
# OMZ is a community framework for managing zsh configuration and plugins.
# It provides plugin loading, theme support, and helper functions.
# Path to your Oh My Zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time Oh My Zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
# robbyrussell is the classic OMZ default; starship overrides it at the bottom.
ZSH_THEME="robbyrussell"

# WALK_MAIN_COLOR: accent color for the 'walk' interactive directory browser.
# Matches the CyberQueer electric-violet secondary accent color.
export WALK_MAIN_COLOR="#5018DD"

# lk(): navigate to a directory selected interactively via 'walk' (a fuzzy dir browser).
# walk prints the selected path to stdout; this function cd's into it.
function lk {
  cd "$(walk "$@")"
}

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
# zstyle ':omz:update' mode auto      # update automatically without asking
# zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# ─── OMZ Plugin list ───────────────────────────────────────────────────────────
# Plugins add completions, keybindings, and helper functions.
# They are loaded from $ZSH/plugins/ (built-in) or $ZSH_CUSTOM/plugins/ (installed).
# Too many plugins slow shell startup — only keep what's actively used.
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(	git                      # git aliases (gst, gco, ga, gp, etc.)
			zsh-syntax-highlighting  # colorizes valid commands green, errors red (installed via zshplugins.sh)
			zsh-autosuggestions      # ghost-text completions based on command history (installed via zshplugins.sh)
			wd                       # warp directory: bookmark dirs with 'wd add <name>', jump with 'wd <name>'
			archlinux                # pacman/yay shortcuts (pacin, pacrem, etc.)
			git                      # duplicate entry — harmless but redundant
			git-auto-fetch           # automatically runs 'git fetch' in the background in git repos
			nmap                     # completions for nmap
			perms                    # helper functions for setting file permissions
			zsh-interactive-cd       # interactive fuzzy cd using fzf
			zsh-navigation-tools     # panel-style history/dir navigation widgets
			z                        # jump to frecent directories: 'z projectname' jumps to most visited match
			ufw                      # completions for ufw (Uncomplicated Firewall)
			web-search               # open web searches from the terminal: 'google foo'
			timer                    # shows command execution time in the prompt for long-running commands
			sudo                     # press Esc twice to prepend 'sudo' to the previous command
			taskwarrior              # completions for the taskwarrior task manager
)

# Load Oh My Zsh — sources all plugins, the theme, and OMZ helper functions.
source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='nvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch $(uname -m)"

# Set personal aliases, overriding those provided by Oh My Zsh libs,
# plugins, and themes. Aliases can be placed here, though Oh My Zsh
# users are encouraged to define aliases within a top-level file in
# the $ZSH_CUSTOM folder, with .zsh extension. Examples:
# - $ZSH_CUSTOM/aliases.zsh
# - $ZSH_CUSTOM/macos.zsh
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
fi   # end of the GNU grep guard block

# ─── Default editor ────────────────────────────────────────────────────────────
# nvim is the default editor for git commits, sudoedit, cron, etc.
# This is set outside the guard block so it always applies regardless of grep detection.
export EDITOR='nvim'

# ─── Startup system info ───────────────────────────────────────────────────────
# Show fastfetch system info on every new interactive shell, except inside
# distrobox containers. CONTAINER_ID is set by distrobox on entry; checking
# for it prevents a duplicate/confusing fetch banner inside containers.
if [ -z "${CONTAINER_ID}" ]; then
	    fastfetch --logo-color-1 red --logo-color-2 red --color red -l ~/Dotfiles/resources/m-archy-SPC.txt
	fi


# Force 256-color + true-color mode. xterm-256color is widely recognized by
# remote servers and terminal multiplexers; it ensures color-capable apps
# (vim, tmux, etc.) enable their full color support even if COLORTERM isn't set.
export TERM=xterm-256color


# ─── Core aliases ──────────────────────────────────────────────────────────────

# Colorize ls and grep output for readability.
alias ls='ls --color=auto'
alias grep='grep --color=auto'

# Detailed listing with ISO timestamps — unambiguous YYYY-MM-DD HH:MM format.
alias ll="ls -la --time-style=long-iso"
alias l="ll"   # Short form

# lsblk with extra columns: filesystem type, drive model, and UUID.
# Useful for identifying disks, partitions, and their UUIDs for /etc/fstab.
alias ldsk="lsblk -o+FSTYPE,MODEL,UUID"

# Quick parent-directory jump.
alias ..="cd .."

# ─── Editor aliases ────────────────────────────────────────────────────────────

alias m="micro"     # micro: simple terminal editor, good for quick config edits
alias sm="sudo micro"  # sudo micro: edit system-owned files

# edit-in-kitty: open a file in micro inside a Kitty terminal panel/tab.
# These variants control where the new editor window appears.
alias e="edit-in-kitty"            # default placement (new OS window)
alias et="edit-in-kitty --type=tab"     # open in a new Kitty tab
alias ek="edit-in-kitty --type=window"  # open in a new Kitty split window
alias ew="kitty --detach micro"         # detach: open in a completely new Kitty window, non-blocking

# ex: open the current directory in Thunar (GTK file manager).
# Useful for quick GUI access without leaving the terminal.
alias ex="thunar ."

# fast-ssh: a wrapper around ssh with fuzzy host selection from ~/.ssh/config.
alias fs="fast-ssh"

# ─── Git aliases ───────────────────────────────────────────────────────────────

alias gita="git add ."              # Stage all changes
alias gitc="git commit -m"          # Commit with message
alias gitp="git push"               # Push to remote
alias gitg="git pull"               # Pull from remote (g for 'get')
alias gitfuck="git commit --amend -m"  # Fix the last commit message without creating a new commit

# gitf(): one-shot add + commit + push. Guards against empty commit messages.
function gitf() {

	if [ -z $1 ]; then
	        echo "no commit message, doing nothing"
	else
			echo "directly commiting with message \"$1\""
	        git add .
	        git commit -m $1
	        git push
	fi
}

# icat: display images inline in Kitty using the Kitty graphics protocol.
# Note: in zsh the kitten is invoked via 'kitty +kitten' rather than the
# standalone 'kitten' binary used in .bashrc.
alias icat="kitty +kitten icat"

alias cls="clear"  # Windows-style clear alias
alias c="cls"      # Ultra-short clear

# kitten ssh: wraps ssh with Kitty's ssh kitten for proper terminfo propagation.
alias ks="kitten ssh"

#function s() {
#	alacritty -e sh -c "ssh $1" &
#}
# (Legacy: used to open ssh in an alacritty window; replaced by Kitty-native approach)

# tio: serial port terminal. '-a latest' picks the most-recently plugged device.
alias serial="sudo tio -a latest"

# wd: warp directory bookmarks. 't' as an alias makes it one keystroke.
alias t="wd"

# Terminal weather report via wttr.in (ANSI art, no browser needed).
alias weather="curl https://wttr.in/"

# Pipe detailed listing through grep for quick in-directory file search.
alias lgrep="l | grep"
alias lg="lgrep"

# treegrep / tg: list the entire directory tree recursively and grep in the output.
# Useful for finding files by partial name across deep hierarchies.
# tree -fi: full-path (-f), no indentation graph (-i) — clean output for grepping.
alias treegrep="tree -fi | grep"
alias tg="treegrep"

# cpwd: copy the current working directory path to the Wayland clipboard.
# wl-copy is the Wayland equivalent of xclip/xsel.
alias cpwd="pwd | wl-copy"

# v/vi: use neovim as the primary $VISUAL editor instead of legacy vim.
alias v="nvim"
alias vi="nvim"

# sv: sudoedit — opens a file as root in $SUDO_EDITOR (nvim) using a safe
# temp-file mechanism, avoiding the security risk of running nvim as root directly.
alias sv="sudoedit"

# ─── Distrobox aliases ─────────────────────────────────────────────────────────
# Distrobox runs OCI containers that share the host's home directory, so tools
# from other distros can be used transparently alongside Arch packages.

alias dbe="distrobox enter"   # Enter (attach to shell of) a running container
alias dbrm="distrobox rm"     # Remove a stopped container
alias dbs="distrobox stop"    # Stop a running container

# fig: find files anywhere in the tree and filter by name using grep.
# Equivalent to `find | grep`, but faster to type.
alias fig="find | grep"

# mfetch: fastfetch with a profile photo displayed via the Kitty image protocol.
# --logo-width/height control the inline image dimensions in terminal cells.
alias mfetch="fastfetch --kitty-icat ~/Pictures/profile.jpg --logo-width 30 --logo-height 30 --color red"

# Python shortcuts
alias pyr="python"                      # Shorter form for Python 3 interpreter
alias pynowr='python -W "ignore"'       # Run Python suppressing all warnings (useful for noisy scripts)

# ─── dbc(): create a distrobox container ──────────────────────────────────────
# Usage: dbc <image> <name>
# e.g.: dbc ubuntu mybox  → pulls ubuntu:latest and names it 'mybox'
function dbc()
{
    distrobox create --image "${1}:latest" --name $2
}

# ─── dbt(): create and immediately enter a distrobox container ────────────────
# Usage: dbt <image>
# The container name matches the image name for simplicity.
function dbt()
{
    distrobox create --image "${1}:latest" --name $1
    distrobox enter $1
}

# ─── y(): yazi file manager with persistent directory change ─────────────────
# Without this wrapper, directory navigation in yazi is lost when it exits
# because yazi runs in a subshell. The --cwd-file mechanism lets yazi report
# its final directory; this function reads it and cd's the parent shell there.
function y() {
	local tmp="$(mktemp -t "yazi-cwd.XXXXXX")" cwd   # temp file for yazi's final cwd
	yazi "$@" --cwd-file="$tmp"                        # run yazi with cwd reporting enabled
	if cwd="$(command cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
		builtin cd -- "$cwd"                           # apply the directory change to the parent shell
	fi
	rm -f -- "$tmp"                                    # clean up temp file
}

# ─── n(): cd + show directory listing ─────────────────────────────────────────
# A quick navigation helper: cd into $1, print the absolute path, and list contents.
# Useful when you want a visual confirmation of where you landed.
function n() {
    cd $1
    echo  $(pwd)
    ls -la
}


# ─── calc(): quick arithmetic evaluator ───────────────────────────────────────
# Passes its arguments to bc with the -l flag (math library for floating point).
# Usage: calc "2.5 * 3 + 1"   → prints 8.5
# printf '%s\n' "$@" handles spaces and multiple expressions correctly.
calc() { printf "%s\n" "$@" | bc -l; }

# ─── Prompt ────────────────────────────────────────────────────────────────────
# Initialize starship for zsh. Starship reads ~/Dotfiles/starship.toml and
# generates a rich prompt segment showing user, path, git state, tool versions,
# and the current time — styled with the CyberQueer red/pink/violet palette.
eval "$(starship init zsh)"

# ─── PATH additions ────────────────────────────────────────────────────────────

# Spicetify: Spotify theme/extension manager. Its CLI lives in ~/.spicetify.
export PATH=$PATH:$HOME/.spicetify

# NVM (Node Version Manager): manages multiple Node.js versions.
# The conditional [ -s ... ] checks that the file exists and is non-empty before sourcing,
# so missing nvm installations don't error out silently.
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# SUDO_EDITOR: the editor invoked by sudoedit / sudo -e.
# Using nvim here instead of vi/nano keeps the editing experience consistent.
export SUDO_EDITOR=/usr/bin/nvim

# Created by `pipx` on 2025-05-12 08:58:28
# pipx installs Python CLI tools in isolated environments under ~/.local/bin.
# The two lines handle both the old tilde-literal form and the expanded $HOME form;
# the second one takes precedence in PATH (higher priority).
export PATH="$PATH:~/.local/bin"
export PATH="$HOME/.local/bin:$PATH"
