Fabric icon indicating copy to clipboard operation
Fabric copied to clipboard

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

Open snafu4 opened this issue 1 year ago • 4 comments

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).

snafu4 avatar Apr 02 '24 14:04 snafu4

Fabric --list already exists

xssdoctor avatar Apr 02 '24 14:04 xssdoctor

'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 avatar Apr 02 '24 15:04 snafu4

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'

  1. 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" '''

  1. Note: CTRL-p can be a quick substitute for 'fabric -l' to quickly see all available patterns.
  2. Note: CTRL-p will only work from the directory where 'patterns' is a directory below (i.e. the 'fabric' directory).
  3. Note: CTRL-p allows 'fuzzy search' so you can type "predict" and it will show all patterns that have predict in the name

snafu4 avatar Apr 02 '24 16:04 snafu4

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.

joshmedeski avatar Sep 20 '24 13:09 joshmedeski