repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
fzf.zshShell170 lines9.4 KiBraw download
#!/usr/bin/env zsh
# ── fzf.zsh : the interactive fuzzy layer — pick anything, live ──────────────
# find/fd = where's the file · grep/rg = what's INSIDE · fzf = pick it by eye.
# This module gives fzf sane defaults (fd as the file source, bat previews,
# Rosé Pine colours) and a set of `f*` verbs. Two jobs, both asked for:
#
#   FILES BY NAME                     KEYWORDS INSIDE FILES
#   ff        pick a file, open it    fif <word>   live grep-as-you-type, open @line
#   fe        pick + edit ($EDITOR)   fifa <word>  same, but search hidden/ignored too
#   fcd       pick a dir, cd into it  frg <word>   one-shot rg→fzf (defined in ripgrep.zsh)
#   fpre      pick a file, preview it
#
#   EXTRAS   fh history · fkill process · fenv var · fman man page
#   HELP     fzhelp / fzf-cheat       the cheat card
#
# Muscle-memory keys fzf's own scripts bind (from core.zsh): Ctrl-T paste a
# file path · Alt-C cd into a dir · Ctrl-R history (atuin reclaims this).

command -v fzf >/dev/null 2>&1 || return

# ── defaults ─────────────────────────────────────────────────────────────────
# Source list = fd (fast, respects .gitignore, shows hidden but skips .git).
# Fall back to plain find if fd somehow isn't present.
if command -v fd >/dev/null 2>&1; then
  export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git'
  export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
  export FZF_ALT_C_COMMAND='fd --type d --hidden --follow --exclude .git'
fi

# Layout + Rosé Pine colours. bg:-1 keeps the terminal's own background.
export FZF_DEFAULT_OPTS='
  --height=80% --layout=reverse --border=rounded --info=inline --cycle
  --prompt="  " --pointer="▶" --marker="✓"
  --color=fg:#e0def4,bg:-1,hl:#eb6f92,fg+:#e0def4,bg+:#26233a,hl+:#eb6f92
  --color=border:#403d52,header:#31748f,gutter:-1,spinner:#f6c177
  --color=info:#9ccfd8,separator:#403d52,pointer:#c4a7e7,marker:#eb6f92,prompt:#908caa'

# Ctrl-T / Alt-C previews (bat for files, eza/ls tree for dirs).
export FZF_CTRL_T_OPTS='--preview "bat --style=numbers --color=always {} 2>/dev/null || cat {}" --preview-window=right,60%,border-left'
export FZF_ALT_C_OPTS='--preview "eza --tree --level=2 --color=always {} 2>/dev/null || ls -la {}" --preview-window=right,50%,border-left'

# ── FILES BY NAME ────────────────────────────────────────────────────────────

# ff -- pick a file, print its path (great in a pipe or $(ff)). Optional dir arg.
ff() {
  local dir="${1:-.}"
  fd --type f --hidden --follow --exclude .git . "$dir" 2>/dev/null |
    fzf --multi \
        --preview 'bat --style=numbers --color=always {} 2>/dev/null || cat {}' \
        --preview-window 'right,60%,border-left'
}

# fe -- pick file(s) and open in $EDITOR. Multi-select with Tab.
fe() {
  local files
  files=$(ff "$@") || return
  [[ -n $files ]] || return
  ${EDITOR:-nvim} ${(f)files}
}

# fcd -- pick a directory and cd into it (respects zoxide's cwd sense).
fcd() {
  local dir
  dir=$(fd --type d --hidden --follow --exclude .git . "${1:-.}" 2>/dev/null |
    fzf --preview 'eza --tree --level=2 --color=always {} 2>/dev/null || ls -la {}' \
        --preview-window 'right,50%,border-left') || return
  [[ -n $dir ]] && cd -- "$dir"
}

# fpre -- pick a file and just show it (bat pager), no editor.
fpre() {
  local file
  file=$(ff "$@") || return
  [[ -n $file ]] && bat --paging=always -- "$file" 2>/dev/null || less -- "$file"
}

# ── KEYWORDS INSIDE FILES  (the "find anything in any file" ask) ─────────────
# fif = "find in files". Every keystroke re-runs ripgrep; the list is live.
# fzf is just the viewer — rg does the searching, so it's full regex + fast.
# Enter opens the highlighted hit at its exact line in $EDITOR.
#   fif todo            start already searching for "todo"
#   fif                 start empty, type to search
# Inside: type to filter · ↑↓ move · Enter open@line · Esc quit.
_fif_core() {
  (( $+commands[rg] && $+commands[fzf] )) || { print -u2 "fif: needs rg and fzf"; return 1 }
  local rg_extra="$1"; shift
  local rg_prefix="rg --column --line-number --no-heading --color=always --smart-case $rg_extra"
  local result file line
  result=$(
    FZF_DEFAULT_COMMAND="$rg_prefix -- ${(q)1:-}" \
      fzf --ansi --disabled --query "${1:-}" \
          --bind "change:reload:$rg_prefix -- {q} || true" \
          --delimiter : \
          --preview 'bat --style=numbers --color=always --highlight-line {2} {1} 2>/dev/null || sed -n {2}p {1}' \
          --preview-window 'up,60%,border-bottom,+{2}+3/3' \
          --prompt 'rg> '
  ) || return
  [[ -n $result ]] || return
  file=${result%%:*}; line=${${result#*:}%%:*}
  ${EDITOR:-nvim} +"$line" -- "$file"
}
fif()  { _fif_core '' "$@"; }                       # respects .gitignore + hidden rules
fifa() { _fif_core '--no-ignore --hidden' "$@"; }   # search EVERYTHING (ignored + hidden)

# ── EXTRAS ───────────────────────────────────────────────────────────────────

# fh -- fuzzy-search shell history, put the chosen line on the command line.
fh() {
  local cmd
  cmd=$(fc -rl 1 | fzf --no-sort --query "$*" --prompt 'hist> ' | sed -E 's/ *[0-9]+\*? +//') || return
  [[ -n $cmd ]] && print -z -- "$cmd"
}

# fkill -- pick a process (or several) and kill it. Signal as $1: fkill -9
fkill() {
  local sig="${1:-TERM}"; [[ $1 == -* || $1 == [0-9]* ]] && shift
  local pids
  pids=$(ps -eo pid,ppid,user,%cpu,%mem,command --sort=-%cpu |
    fzf --multi --header-lines=1 --prompt 'kill> ' | awk '{print $1}') || return
  [[ -n $pids ]] && echo "$pids" | xargs -r kill "-${sig#-}"
}

# fenv -- fuzzy-browse environment variables.
fenv() { env | sort | fzf --prompt 'env> ' --preview 'echo {}' --preview-window down,3,wrap; }

# fman -- pick a man page from all of them and open it.
fman() {
  local page
  page=$(man -k . 2>/dev/null | fzf --prompt 'man> ' | awk '{print $1}' | tr -d '()') || return
  [[ -n $page ]] && man "$page"
}

# ── HELP : fzf cheat card ────────────────────────────────────────────────────
fzhelp() {
  print -P ""
  print -P "%F{#9ccfd8}▰▰▶ FZF CHEAT SHEET%f   %F{#908caa}fuzzy-pick anything, interactively%f"
  print -P "%F{#908caa}──────────────────────────────────────────────────────────────%f"
  print -P "%F{#f6c177} the idea%f  fd=file NAMES · rg=text INSIDE · %F{#e0def4}fzf%f=pick it by eye"
  print -P "%F{#f6c177} files by name%f"
  print -P "   %F{#e0def4}ff%f              pick file(s), print the path  (pipe it / capture it)"
  print -P "   %F{#e0def4}ff ~/dl%f          ...only under ~/dl"
  print -P "   %F{#e0def4}fe%f              pick file(s) and open in \$EDITOR  (Tab = multi)"
  print -P "   %F{#e0def4}fcd%f             pick a directory and cd into it"
  print -P "   %F{#e0def4}fpre%f            pick a file and page it with bat"
  print -P "%F{#f6c177} keywords inside files  %F{#908caa}(live — type, results update)%f"
  print -P "   %F{#e0def4}fif%f             grep-as-you-type; Enter opens the hit at its line"
  print -P "   %F{#e0def4}fif todo%f        ...start already searching 'todo'"
  print -P "   %F{#e0def4}fifa secret%f     same but include hidden + .gitignore'd files"
  print -P "   %F{#e0def4}frg pattern%f     one-shot (fixed pattern) — from the rg module"
  print -P "%F{#f6c177} extras%f"
  print -P "   %F{#e0def4}fh%f    history      %F{#e0def4}fkill%f  process   %F{#e0def4}fkill -9%f  force"
  print -P "   %F{#e0def4}fenv%f  env vars     %F{#e0def4}fman%f   man page picker"
  print -P "%F{#f6c177} keys fzf binds everywhere%f"
  print -P "   %F{#e0def4}Ctrl-T%f  paste a file path into the current command"
  print -P "   %F{#e0def4}Alt-C%f   cd into a fuzzy-picked directory"
  print -P "   %F{#e0def4}Ctrl-R%f  history search  %F{#908caa}(atuin owns this here)%f"
  print -P "%F{#f6c177} inside the picker%f"
  print -P "   %F{#e0def4}type%f filter · %F{#e0def4}↑↓%f move · %F{#e0def4}Tab%f mark (multi) · %F{#e0def4}Enter%f pick · %F{#e0def4}Esc%f quit"
  print -P "   %F{#e0def4}?%f toggle preview · %F{#e0def4}Ctrl-/%f cycle preview position"
  print -P "%F{#f6c177} query syntax  %F{#908caa}(extended-search, the default)%f"
  print -P "   %F{#e0def4}'exact%f  literal · %F{#e0def4}^head%f start · %F{#e0def4}tail\$%f end · %F{#e0def4}!no%f negate · %F{#e0def4}a | b%f OR"
  print -P "%F{#f6c177} browse the cheat sheets themselves%f"
  print -P "   %F{#e0def4}fcheats%f         fuzzy-pick any card (fzf/grep/rg/…), live preview, Enter prints it"
  print -P "%F{#908caa}──────────────────────────────────────────────────────────────%f"
  print -P "%F{#908caa} defaults: fd source, bat/eza previews, Rosé Pine — set in %f%F{#e0def4}~/.dotfiles/config/fzf.zsh%f"
}
alias fzf-cheat='fzhelp'

# retire the old stub that pointed at a guide file which never existed
unalias fzf-setup 2>/dev/null