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

added Brace exspansion module (#535)

* updated readme

* added the expand module

* change from --help to -h due to a bug

* changed expand to main, to self reference

* updated wrong output
This commit is contained in:
Tilen Gimpelj 2023-06-23 22:06:25 +02:00 committed by GitHub
parent 9d12487d11
commit 9e62390ec1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 0 deletions

View file

@ -62,6 +62,29 @@ An extensive example of a wrapper for docker operations, with nushell completion
## filesystem
- [bm](./filesystem/bm.nu) - A Simple bookmarking module. It uses `XGD_DATA_HOME` to save bookmarks.
- [expand](./filesystem/expand.nu) - expansion module that implements bashes brace expansion.
The expansion uses a list inside of braces seperated by `,` to expand into a list of multiple string variations like:
```
expand a/{b,c}/d{e,f,g}.nu{,on}
```
parses into:
```
╭────┬─────────────╮
│ 0 │ a/b/de.nu │
│ 1 │ a/c/de.nu │
│ 2 │ a/b/df.nu │
│ 3 │ a/c/df.nu │
│ 4 │ a/b/dg.nu │
│ 5 │ a/c/dg.nu │
│ 6 │ a/b/de.nuon │
│ 7 │ a/c/de.nuon │
│ 8 │ a/b/df.nuon │
│ 9 │ a/c/df.nuon │
│ 10 │ a/b/dg.nuon │
│ 11 │ a/c/dg.nuon │
╰────┴─────────────╯
```
## formats
Examples of input/output formatters:

View file

@ -0,0 +1,64 @@
# A bash like quick string manipulation script
# the script works by creating a list from brace contents, seperated by a brace.
# Expand the given string into a list on braces like bashes brace expansion
export def main [
input: string # the string to expand.
] {
listify $input
}
def listify [lst] {
try {
($lst
| parse -r '(?P<left>.*)(?<!\\){(?P<list>.*)(?<!\\)}(?P<right>.*)'
| get 0
| upsert list { |l|
$l.list
| split row ","
| str trim
}
| each { |it|
$it.list
| each { |l|
listify $"($it.left)($l)($it.right)"
}})
| flatten
} catch {
$lst
}
}
export def help [] {
print -n ( main -h )
print (
[
$"(ansi green)Examples(ansi reset):"
$" > (ansi light_green)expand (ansi green)a/{b,c}/d(ansi reset)"
$"╭───┬───────╮\n│ 0 │ a/b/d │\n│ 1 │ a/c/d │\n╰───┴───────╯"
$" > (ansi light_green)expand (ansi green)\"my {beautifull,ugly} duckling\"(ansi reset)"
$"╭───┬────────────────────────╮\n│ 0 │ my beautifull duckling │\n│ 1 │ my ugly duckling │\n╰───┴────────────────────────╯"
$" > (ansi light_green)expand (ansi green).config/nushell/config.nu{,on}(ansi reset)"
$"╭───┬─────────────────────────────╮\n│ 0 │ .config/nushell/config.nu │\n│ 1 │ .config/nushell/config.nuon │\n╰───┴─────────────────────────────╯"
$" > (ansi light_green)expand (ansi green)a/{b,c}/{d,e,f,g} (ansi reset)(ansi purple)|(ansi reset)(ansi light_green) each(ansi reset) (ansi light_green){ |d| (ansi reset)(ansi light_green) mkdir(ansi reset) (ansi purple)$d (ansi reset)(ansi light_green)}(ansi reset);(ansi light_green)tree(ansi reset)"
$".
`-- a
|-- b
| |-- d
| |-- e
| |-- f
| `-- g
`-- c
|-- d
|-- e
|-- f
`-- g
12 directories, 0 files"
] |
str join "\n" |
nu-highlight
)
}