danielmiessler / fabric

fabric is an open-source framework for augmenting humans using AI. It provides a modular framework for solving specific problems using a crowdsourced set of AI prompts that can be used anywhere.
https://danielmiessler.com/p/fabric-origin-story
MIT License
24.46k stars 2.62k forks source link

[Feature request]: Binding to a popup that shows patterns available as CLI is used #318

Open snafu4 opened 7 months ago

snafu4 commented 7 months ago

What do you need?

After entering '-p', can you create some sort of binding (maybe to fzf) so that the list of available patterns pops up. A pattern is selected and lands on the CLI. I don't have the skillset to do this myself (I tried).

xssdoctor commented 7 months ago

Fabric --list already exists

snafu4 commented 7 months ago

'fabric -l' just shows a list of patterns, which you then have to remember to type when you want to use that pattern. I'm suggesting a popup (with all of the available patterns shown) after you type '-p' so you just have to select one and it is inserted into the command line.

Screenshot 2024-04-02 112428

snafu4 commented 7 months ago

Maybe figured it out... This has only been tested on WSL2.

  1. sudo apt install fzf
  2. Add the following to ~/.bashrc

    
    selectpattern() {
    # Define the base directory
    local basedir="./patterns"
    
    # Use fzf to select a sub-directory from the sorted list in descending order
    # Use sed to remove the base directory path from the output before presenting it to fzf
    local selected=$(find "$basedir" -mindepth 1 -maxdepth 1 -type d | sed "s|^$basedir/||" | sort -r | fzf)
    
    # Check if a directory was selected
    if [ -n "$selected" ]; then
        # No need to extract the basename since we've already manipulated the output
        # Use printf to escape special characters in the directory name
        printf -v escaped_dirname "%q" "$selected"
    
        # Insert the directory name into the command line at the cursor position
        READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$escaped_dirname ${READLINE_LINE:$READLINE_POINT}"
        READLINE_POINT=$(( READLINE_POINT + ${#escaped_dirname} + 1 ))
    else
        # If no directory was selected, ensure proper handling
        echo "No directory selected."
    fi
    }

Bind the function to a key combination, for example Ctrl+p

bind -x '"\C-p":selectpattern'



3. to use: CTRL-p whenever you want to list all of the patterns available (from the 'fabric' directory) use the arrow keys to go up/down and hit enter when you find the pattern you want
'''
> pbpaste | CTRL-p (and select 'extract_wisdom', press ENTER) and the results will be "pbpaste | extract_wisdom"
'''
4. Note: CTRL-p can be a quick substitute for 'fabric -l' to quickly see all available patterns.
5. Note: CTRL-p will only work from the directory where 'patterns' is a directory below (i.e. the 'fabric' directory).
6. Note: CTRL-p allows 'fuzzy search' so you can type "predict" and it will show all patterns that have predict in the name
joshmedeski commented 1 month ago

The fabric -l pattern isn't friendly for tools like fzf. It' shows a "Patterns" header and indents all of the results. Can we create some sort of raw flag that just lists the patterns and nothing else without a heading, line breaks, or indentations?

I'm willing to create a PR, but want to be on the same page with @danielmiessler and the mods.