# shellcheck shell=sh # lib/cheat.sh — terminal cheatsheet browser over $PE_CHEAT_DIR/*.md. # fzf list + preview when interactive; ripgrep for `pe cheat `. # Depends on config.sh, ui.sh. Degrades gracefully without fzf/glow/bat/rg. # pick a markdown renderer: glow > bat/batcat > pager cheat__bat() { command -v bat >/dev/null 2>&1 && echo bat || { command -v batcat >/dev/null 2>&1 && echo batcat; }; } cheat__render() { _f=$1 if command -v glow >/dev/null 2>&1; then glow -p "$_f" elif _b=$(cheat__bat); [ -n "$_b" ]; then "$_b" --style=plain --paging=always -l md "$_f" else ${PE_PAGER:-less -R} "$_f"; fi } cheat__preview() { # command string fzf uses to preview a sheet if command -v glow >/dev/null 2>&1; then printf 'glow -s dark %s/{}' "$PE_CHEAT_DIR" elif _b=$(cheat__bat); [ -n "$_b" ]; then printf '%s --color=always -l md %s/{}' "$_b" "$PE_CHEAT_DIR" else printf 'cat %s/{}' "$PE_CHEAT_DIR"; fi } cheat_list() { [ -d "$PE_CHEAT_DIR" ] || pe_die "no cheatsheet dir: $PE_CHEAT_DIR" ( cd "$PE_CHEAT_DIR" && ls -1 ./*.md 2>/dev/null | sed 's,^\./,,' ) } cheat_main() { _sheets=$(cheat_list) [ -n "$_sheets" ] || pe_die "no cheatsheets in $PE_CHEAT_DIR" # `pe cheat `: ripgrep across all sheets, jump to a match. if [ $# -gt 0 ]; then if command -v rg >/dev/null 2>&1; then ( cd "$PE_CHEAT_DIR" && rg -n --color=never "$*" ./*.md ) \ | sed 's,^\./,,' || pe_empty "no match for: $*" else ( cd "$PE_CHEAT_DIR" && grep -rni "$*" ./*.md ) || pe_empty "no match for: $*" fi return 0 fi # interactive: fzf list with live preview; selection opens in renderer if command -v fzf >/dev/null 2>&1 && [ -t 0 ]; then _sel=$(printf '%s\n' "$_sheets" \ | fzf --prompt='cheat> ' --height=80% --reverse \ --preview "$(cheat__preview)" --preview-window=right:65%) [ -n "$_sel" ] && cheat__render "$PE_CHEAT_DIR/$_sel" else pe_info "cheatsheets in $PE_CHEAT_DIR:" printf '%s\n' "$_sheets" | sed 's/^/ /' >&2 pe_info "open one with: pe cheat | (install fzf for the picker)" fi }