1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00

Merge pull request #79 from naefl/main

Add fuzzy commands
This commit is contained in:
Darren Schroeder 2021-07-26 07:54:33 -05:00 committed by GitHub
commit c0a3fe95af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 0 deletions

32
fuzzy/README.md Normal file
View file

@ -0,0 +1,32 @@
# Fuzzy all the things
### Purpose
This contains a few scripts that add fuzzy search interfaces to built-in nu functionalities. Often you
want to search commands/your history interactively, which is where [fzf](https://github.com/junegunn/fzf) excels at.
### How to use
`./fuzzy_history_search.nu` searches your command history and, after pressing `enter`, copies the selected command into the clipboard
`./fuzzy_command_search.nu` searches both commands and subcommands for both a) names and b) their description, and, after pressing `enter`, copies the selected command into the clipboard
To use them in your day-to-day workflow, add
```
[
"source <absolute-path-to-nu_script>/fuzzy/fuzzy_history_search.nu",
"source <absolute-path-to-nu_script>/fuzzy/fuzzy_command_search.nu"
]
```
to your `startup` array in you config `.toml`.
It's likely a good idea to also add some short and sweet aliases, e.g.
```
alias hi = fuzzy-history-search
alias hf = fuzzy-command-search
```
To your config

View file

@ -0,0 +1,38 @@
# calculate required tabs/spaces to get a nicely aligned table
let tablen = 8
let max-len = (help commands | get subcommands | each { $it.name | str length } | math max)
let max-indent = ($max-len / $tablen | into int)
def pad-tabs [input-name] {
let input-length = ($input-name| str length)
let required-tabs = $max-indent - ($"($input-length / $tablen | into int)" | str to-int)
echo $"( seq $required-tabs | reduce -f "" {$acc + (char tab)})"
}
# fuzzy search a) commands b) subcommands
# on selection, will display `help` for the commands
# and paste command into clipboard for you to paste right away
def fuzzy-command-search [] {
help (echo (help commands | get subcommands | each {
let name = ($it.name | str trim | ansi strip)
$"(
$name
)( pad-tabs $name
)(
$it.description
)"
}) (
help commands | reject subcommands | each {
let name = ($it.name | str trim | ansi strip)
$"(
$name
)(
pad-tabs $name
)(
$it.description
)"
}) | str collect (char nl) | ^fzf | cut -f1 -d$'\t' | str trim | clip; paste )
}

View file

@ -0,0 +1 @@
def fuzzy-history-search [] { cat $nu.history-path | fzf | clip }