From 3a1abce77780c155c2362d8fe7524b6c3185ad21 Mon Sep 17 00:00:00 2001 From: naefl Date: Sun, 25 Jul 2021 22:38:36 -0400 Subject: [PATCH] Add fuzzy scripts --- fuzzy/README.md | 32 +++++++++++++++++++++++++++++ fuzzy/fuzzy_command_search.nu | 38 +++++++++++++++++++++++++++++++++++ fuzzy/fuzzy_history_search.nu | 1 + 3 files changed, 71 insertions(+) create mode 100644 fuzzy/README.md create mode 100644 fuzzy/fuzzy_command_search.nu create mode 100644 fuzzy/fuzzy_history_search.nu diff --git a/fuzzy/README.md b/fuzzy/README.md new file mode 100644 index 0000000..9a97b94 --- /dev/null +++ b/fuzzy/README.md @@ -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 /fuzzy/fuzzy_history_search.nu", + "source /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 diff --git a/fuzzy/fuzzy_command_search.nu b/fuzzy/fuzzy_command_search.nu new file mode 100644 index 0000000..57f1a22 --- /dev/null +++ b/fuzzy/fuzzy_command_search.nu @@ -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 ) +} + diff --git a/fuzzy/fuzzy_history_search.nu b/fuzzy/fuzzy_history_search.nu new file mode 100644 index 0000000..ad2a1ca --- /dev/null +++ b/fuzzy/fuzzy_history_search.nu @@ -0,0 +1 @@ +def fuzzy-history-search [] { cat $nu.history-path | fzf | clip }