#!/usr/bin/env bash # Interactive demo launcher for pikl-menu. # Uses pikl to pick a scenario, then runs that scenario in pikl. # # Usage: ./examples/demo.sh set -euo pipefail # Resolve the pikl binary once up front. if [[ -n "${PIKL:-}" ]]; then PIKL_BIN="$PIKL" elif command -v pikl >/dev/null 2>&1; then PIKL_BIN="pikl" else # Build quietly, use the debug binary directly. cargo build --quiet 2>&1 PIKL_BIN="cargo run --quiet --" fi # Wrapper so scenarios can just call `pikl` with args. pikl() { $PIKL_BIN "$@" } # ── Scenario runners ────────────────────────────────────── plain_list() { printf "apple\nbanana\ncherry\ndate\nelderberry\nfig\ngrape\nhoneydew\n" \ | pikl } big_list() { # seq output is wrapped as JSON strings so they get # proper labels (bare numbers parse as JSON numbers # with empty display text). seq 1 500 | sed 's/.*/"&"/' | pikl } json_objects() { cat <<'ITEMS' | pikl {"label": "Arch Linux", "category": "rolling", "init": "systemd"} {"label": "NixOS", "category": "rolling", "init": "systemd"} {"label": "Void Linux", "category": "rolling", "init": "runit"} {"label": "Debian", "category": "stable", "init": "systemd"} {"label": "Alpine", "category": "stable", "init": "openrc"} {"label": "Fedora", "category": "semi-rolling", "init": "systemd"} {"label": "Gentoo", "category": "rolling", "init": "openrc"} ITEMS } custom_label_key() { cat <<'ITEMS' | pikl --label-key name {"name": "Neovim", "type": "editor", "lang": "C/Lua"} {"name": "Helix", "type": "editor", "lang": "Rust"} {"name": "Kakoune", "type": "editor", "lang": "C++"} {"name": "Emacs", "type": "editor", "lang": "Lisp"} {"name": "Vim", "type": "editor", "lang": "C"} ITEMS } git_branches() { if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then echo "not in a git repo" >&2 return 1 fi git branch --format='%(refname:short)' | pikl } git_log_picker() { if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then echo "not in a git repo" >&2 return 1 fi git log --oneline -30 | pikl } file_picker() { find . -maxdepth 3 -type f \ -not -path './.git/*' \ -not -path './target/*' \ -not -name '*.lock' \ | sort \ | pikl } on_select_hook() { printf "one\ntwo\nthree\nfour\nfive\n" \ | pikl --on-select 'echo "you picked: $(cat)"' } mixed_input() { cat <<'ITEMS' | pikl just a plain string {"label": "a json object", "extra": 42} another plain string {"label": "second object", "extra": 99} ITEMS } # ── Scenario menu ───────────────────────────────────────── scenarios=( "Plain text list" "Big list (500 items)" "JSON objects (distros)" "Custom --label-key (editors)" "Git branches" "Git log (last 30)" "File picker" "on-select hook" "Mixed input (plain + JSON)" ) # Map display names to functions run_scenario() { case "$1" in *"Plain text"*) plain_list ;; *"Big list"*) big_list ;; *"JSON objects"*) json_objects ;; *"label-key"*) custom_label_key ;; *"Git branches"*) git_branches ;; *"Git log"*) git_log_picker ;; *"File picker"*) file_picker ;; *"on-select"*) on_select_hook ;; *"Mixed input"*) mixed_input ;; *) echo "unknown scenario" >&2 return 1 ;; esac } # ── Main ────────────────────────────────────────────────── main() { echo "pikl demo launcher" >&2 echo "pick a scenario, then interact with it" >&2 echo "" >&2 choice=$(printf '%s\n' "${scenarios[@]}" | pikl) || { echo "cancelled" >&2 exit 1 } # pikl outputs JSON. Strip the quotes for matching. choice=$(echo "$choice" | tr -d '"') echo "" >&2 echo "── running: $choice ──" >&2 echo "" >&2 result=$(run_scenario "$choice") || exit $? echo "" >&2 echo "── result ──" >&2 echo "$result" } main "$@"