SergeySamoylov

Menu_universal

Jul 16th, 2025
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.67 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # menu.sh — universal fzf menu
  3.  
  4. menu() {
  5.     local -a options=("${@}")
  6.     local formatted=()
  7.     local i=1
  8.  
  9.     for option in "${options[@]}"; do
  10.         formatted+=("$(printf "%02d) %s" "$i" "$option")")
  11.        ((i++))
  12.    done
  13.  
  14.    # Use fzf with --print-query and fallback logic
  15.    local result query selection
  16.  
  17.    result=$(printf "%s\n" "${formatted[@]}" | fzf --prompt="Choose: " --height=40% --reverse --print-query)
  18.    query=$(echo "$result" | sed -n '1p')
  19.    selection=$(echo "$result" | sed -n '2p' | sed 's/^[0-9][0-9]*) //')
  20.  
  21.    # Return selection if not empty, otherwise the query
  22.    printf "%s\n" "${selection:-$query}"
  23. }
  24.  
  25.  
Tags: BASH Menu
Advertisement
Add Comment
Please, Sign In to add comment