Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
snafu4 opened this issue Apr 2, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@snafu4
Copy link

snafu4 commented Apr 2, 2024

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 snafu4 added the enhancement New feature or request label Apr 2, 2024
@xssdoctor
Copy link
Collaborator

Fabric --list already exists

@snafu4
Copy link
Author

snafu4 commented Apr 2, 2024

'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
Copy link
Author

snafu4 commented Apr 2, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants