mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 06:37:46 +00:00
Add fuzzy scripts
This commit is contained in:
parent
5a4027defe
commit
3a1abce777
3 changed files with 71 additions and 0 deletions
32
fuzzy/README.md
Normal file
32
fuzzy/README.md
Normal 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
|
38
fuzzy/fuzzy_command_search.nu
Normal file
38
fuzzy/fuzzy_command_search.nu
Normal 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 )
|
||||
}
|
||||
|
1
fuzzy/fuzzy_history_search.nu
Normal file
1
fuzzy/fuzzy_history_search.nu
Normal file
|
@ -0,0 +1 @@
|
|||
def fuzzy-history-search [] { cat $nu.history-path | fzf | clip }
|
Loading…
Add table
Add a link
Reference in a new issue