mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 06:37:46 +00:00
Port before_v0.60/fuzzy
, before_v0.60/ls-mods
and before_v0.60/nu_101
(#845)
This PR is part of porting all old scripts #221 and includes a set of modules: - fuzzy -> `modules/fuzzy/fuzzy_command_search.nu` - ls-mods -> `modules/ls-mods`: `ls-less.nu`, `ls-wide.nu` and `ls-wide-with-color.nu` - nu_101 -> `modules/nu_101`: `nothing.nu` and `inner_outer_loop.nu` Edit: `fuzzy` and `nu_101` have been moved to `sourced`
This commit is contained in:
parent
7a5424d7d0
commit
e8df70a406
16 changed files with 177 additions and 230 deletions
28
modules/fuzzy/README.md
Normal file
28
modules/fuzzy/README.md
Normal file
|
@ -0,0 +1,28 @@
|
|||
# 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_scripts>/fuzzy/fuzzy_history_search.nu
|
||||
source <absolute-path-to-nu_scripts>/fuzzy/fuzzy_command_search.nu
|
||||
```
|
||||
|
||||
to your `config.nu`
|
||||
|
||||
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
|
||||
```
|
25
modules/fuzzy/fuzzy_command_search.nu
Normal file
25
modules/fuzzy/fuzzy_command_search.nu
Normal file
|
@ -0,0 +1,25 @@
|
|||
const tablen = 8
|
||||
|
||||
# calculate required tabs/spaces to get a nicely aligned table
|
||||
def pad-tabs [input_name max_indent] {
|
||||
let input_length = ($input_name | str length)
|
||||
let required_tabs = $max_indent - ($input_length / $tablen | into int)
|
||||
seq 0 $required_tabs | reduce -f "" {|it, acc| $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
|
||||
|
||||
|
||||
export def fuzzy-command-search [] {
|
||||
let max_len = (help commands | each { $in.name | str length } | math max)
|
||||
let max_indent = ($max_len / $tablen | into int)
|
||||
let command = ((help commands | each {|it|
|
||||
let name = ($it.name | str trim | ansi strip)
|
||||
$"($name)(pad-tabs $name $max_indent)($it.usage)"
|
||||
}) | str join (char nl) | fzf | split column (char tab) | get column1.0)
|
||||
if ($command | is-not-empty) {
|
||||
help $command
|
||||
}
|
||||
}
|
1
modules/fuzzy/fuzzy_history_search.nu
Normal file
1
modules/fuzzy/fuzzy_history_search.nu
Normal file
|
@ -0,0 +1 @@
|
|||
export def fuzzy-history-search [] { cat $nu.history-path | fzf | clip }
|
7
modules/ls_mods/ls-hidden.nu
Normal file
7
modules/ls_mods/ls-hidden.nu
Normal file
|
@ -0,0 +1,7 @@
|
|||
|
||||
export def ls-hidden [
|
||||
--dir(-d):any # The directory you want to list
|
||||
] {
|
||||
let dir = if ($dir | is-empty) { "." } else { $dir }
|
||||
ls -a $dir | filter { ($in.name | into string | str starts-with '.') }
|
||||
}
|
12
modules/ls_mods/ls-less.nu
Normal file
12
modules/ls_mods/ls-less.nu
Normal file
|
@ -0,0 +1,12 @@
|
|||
# An attempt at trying to put ls into a paging mode
|
||||
export def ls-less [
|
||||
--dir(-d):any # The directory you want to list
|
||||
] {
|
||||
let is_empty = ($dir | is-empty)
|
||||
if $is_empty {
|
||||
nu -c 'ls' | less -r
|
||||
} else {
|
||||
let command = $"ls ($dir)"
|
||||
nu -c $command | less -r
|
||||
}
|
||||
}
|
75
modules/ls_mods/ls-wide-with-color.nu
Normal file
75
modules/ls_mods/ls-wide-with-color.nu
Normal file
|
@ -0,0 +1,75 @@
|
|||
alias relet = ansi -e '0m' # really reset but there are external commands for reset already
|
||||
|
||||
alias fg_black = ansi -e '30m'
|
||||
alias fg_red = ansi -e '31m'
|
||||
alias fg_green = ansi -e '32m'
|
||||
alias fg_yellow = ansi -e '33m'
|
||||
alias fg_blue = ansi -e '34m'
|
||||
alias fg_magenta = ansi -e '35m'
|
||||
alias fg_purple = ansi -e '35m'
|
||||
alias fg_cyan = ansi -e '36m'
|
||||
alias fg_white = ansi -e '37m'
|
||||
|
||||
alias fg_dark_gray = ansi -e '90m'
|
||||
alias fg_light_black = ansi -e '90m'
|
||||
alias fg_light_red = ansi -e '91m'
|
||||
alias fg_light_green = ansi -e '92m'
|
||||
alias fg_light_yellow = ansi -e '93m'
|
||||
alias fg_light_blue = ansi -e '94m'
|
||||
alias fg_light_magenta = ansi -e '95m'
|
||||
alias fg_light_purple = ansi -e '95m'
|
||||
alias fg_light_cyan = ansi -e '96m'
|
||||
alias fg_light_gray = ansi -e '97m'
|
||||
alias fg_light_white = ansi -e '97m'
|
||||
|
||||
# A ls command that approximates the ls -sh command in bash
|
||||
export def ls-wide2 [
|
||||
--dir(-d):any # The directory you want to list
|
||||
--columns(-c):int # The number of columns in your output
|
||||
] {
|
||||
let is_dir_empty = ($dir | is-empty)
|
||||
let is_columns_empty = ($columns | is-empty)
|
||||
let ls_data = (if $is_dir_empty { ls } else { ls $dir })
|
||||
let max_fname_size = ($ls_data | get name | into string | str length | math max)
|
||||
let max_fsize_size = ($ls_data | get size | into string | str length | math max)
|
||||
($ls_data) | enumerate | each { |file|
|
||||
let clr_file = (colorize $file.item.name)
|
||||
let clr_size = (echo $file.item.size | into string)
|
||||
let new_line = (if $is_columns_empty {
|
||||
if (($file.index + 1) mod 3) == 0 {
|
||||
char newline
|
||||
}
|
||||
} else {
|
||||
if (($file.index + 1) mod $columns) == 0 {
|
||||
char newline
|
||||
}
|
||||
})
|
||||
$"($clr_file | fill -a l -c ' ' -w $max_fname_size) ($clr_size | fill -a r -c ' ' -w $max_fsize_size) ($new_line)"
|
||||
} | str join
|
||||
}
|
||||
|
||||
def colorize [thing:any] {
|
||||
let thing_as_string = (echo $thing | into string)
|
||||
let ext = (echo $thing_as_string | path parse | get extension)
|
||||
let is_empty = ($ext | is-empty)
|
||||
|
||||
if $is_empty {
|
||||
$"(fg_cyan)($thing)(relet)"
|
||||
} else {
|
||||
if $ext == "nu" {
|
||||
$"(fg_light_magenta)($thing)(relet)"
|
||||
} else {
|
||||
$"(fg_light_green)($thing)(relet)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def colorit [] {
|
||||
print (colorize 123)
|
||||
print (colorize " 123.456")
|
||||
print (colorize " file.nu")
|
||||
|
||||
# These all work
|
||||
print $"(fg_light_green)abc(relet)"
|
||||
print $"(fg_cyan)def(relet)"
|
||||
}
|
47
modules/ls_mods/ls-wide.nu
Normal file
47
modules/ls_mods/ls-wide.nu
Normal file
|
@ -0,0 +1,47 @@
|
|||
# A ls command that approximates the ls -sh command in bash
|
||||
export def ls-wide [
|
||||
--path(-p):string # The path you want to list
|
||||
--columns(-c):int # The number of columns in your output
|
||||
] {
|
||||
let is_columns_empty = ($columns | is-empty)
|
||||
let is_path_empty = ($path | is-empty)
|
||||
let columns_default = 3
|
||||
|
||||
if $is_path_empty {
|
||||
if $is_columns_empty {
|
||||
run_ls "." $columns_default
|
||||
} else {
|
||||
run_ls "." $columns
|
||||
}
|
||||
} else {
|
||||
if $is_columns_empty {
|
||||
run_ls $path $columns_default
|
||||
} else {
|
||||
run_ls $path $columns
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def run_ls [
|
||||
path:string
|
||||
columns:int
|
||||
] {
|
||||
let max_fname_size = (ls $path | get name | into string | str length | math max)
|
||||
let max_fsize_size = (ls $path | get size | into string | str length | math max)
|
||||
let is_columns_empty = ($columns | is-empty)
|
||||
|
||||
ls $path | enumerate | each { |file|
|
||||
let the_file = ($file.item.name | into string | fill -a l -c ' ' -w $max_fname_size)
|
||||
let the_size = ($file.item.size | into string | fill -a r -c ' ' -w $max_fsize_size)
|
||||
let new_line = (if $is_columns_empty {
|
||||
if ($file.index + 1) mod 3 == 0 {
|
||||
char newline
|
||||
}
|
||||
} else {
|
||||
if ($file.index + 1) mod $columns == 0 {
|
||||
char newline
|
||||
}
|
||||
})
|
||||
$"($the_file) ($the_size) ($new_line)"
|
||||
} | str join
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue