From aaa843f1c53b5e8fd5e7404456b0cfeb939f9d83 Mon Sep 17 00:00:00 2001 From: Kamil Date: Sun, 1 Aug 2021 00:40:56 +0000 Subject: [PATCH 1/3] Create cdpath-implementation.nu MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If anyone knows how to write it more clearly and the script (instead of the config one) way — feel free to correct it --- cool_oneliners/cdpath-implementation.nu | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 cool_oneliners/cdpath-implementation.nu diff --git a/cool_oneliners/cdpath-implementation.nu b/cool_oneliners/cdpath-implementation.nu new file mode 100644 index 0000000..8a28154 --- /dev/null +++ b/cool_oneliners/cdpath-implementation.nu @@ -0,0 +1,11 @@ +#!/usr/bin/nu + +#I actually use it as a part of my startup, so I'm not really sure how to pack it, yet I'd like to contribute +#NOTE: it does *not* work with 0 arguments. To get $cd functionality, use $cd. To use cdpath, use $c +#Written by skelly37 + +startup = [ + "let cdpath = [. /place/your /cdpath/here]", + + "def c [dir] { let wd = (pwd); for element in $cdpath {if (pwd) == $wd {cd $element; for directory in (ls -d -a */ | get name) {if $dir == $directory {cd $dir} {}}; if (pwd) == $element {cd $wd} {}} {}}; if (pwd) == $wd {cd $wd; echo \"No such path!\"} {}}", +] From 4e4ea71612e957b89118bd6935dbac7fca5e12f7 Mon Sep 17 00:00:00 2001 From: Kamil Date: Sun, 1 Aug 2021 09:21:40 +0000 Subject: [PATCH 2/3] Update to speed up the script I've made use of selecting just directories instead of iterating over everything in a directory, increasing the speed of execution significantly --- cool_oneliners/cdpath-implementation.nu | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cool_oneliners/cdpath-implementation.nu b/cool_oneliners/cdpath-implementation.nu index 8a28154..f060650 100644 --- a/cool_oneliners/cdpath-implementation.nu +++ b/cool_oneliners/cdpath-implementation.nu @@ -5,7 +5,6 @@ #Written by skelly37 startup = [ - "let cdpath = [. /place/your /cdpath/here]", - - "def c [dir] { let wd = (pwd); for element in $cdpath {if (pwd) == $wd {cd $element; for directory in (ls -d -a */ | get name) {if $dir == $directory {cd $dir} {}}; if (pwd) == $element {cd $wd} {}} {}}; if (pwd) == $wd {cd $wd; echo \"No such path!\"} {}}", + "let cdpath = [. /data /data/it /data/games]", + "def c [dir] { let wd = (pwd); for element in $cdpath {if (pwd) == $wd {cd $element; for directory in (ls -a | select name type | each { if $it.type == Dir {echo $it.name} {} } ) {if $dir == $directory {cd $dir} {}}; if (pwd) == $element {cd $wd} {}} {}}; if (pwd) == $wd {cd $wd; echo \"No such path!\"} {}}", ] From 9f648e7f0481dff254c6f894e6bb83df849bc6ac Mon Sep 17 00:00:00 2001 From: Kamil Date: Sun, 1 Aug 2021 09:28:53 +0000 Subject: [PATCH 3/3] Documented I have just noticed I had not documented the script well. Now it is the final version of my idea, I guess. --- cool_oneliners/cdpath-implementation.nu | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/cool_oneliners/cdpath-implementation.nu b/cool_oneliners/cdpath-implementation.nu index f060650..c1b88db 100644 --- a/cool_oneliners/cdpath-implementation.nu +++ b/cool_oneliners/cdpath-implementation.nu @@ -1,10 +1,23 @@ #!/usr/bin/nu -#I actually use it as a part of my startup, so I'm not really sure how to pack it, yet I'd like to contribute -#NOTE: it does *not* work with 0 arguments. To get $cd functionality, use $cd. To use cdpath, use $c +# I actually use it as a part of my startup, so I am not really sure how to pack it, yet I wouldd like to contribute +#------------------------------------------------------------------------------------------------------------------------------- +# +# How to use? +#------------------------------------------------- +#1) Add desired paths to the cdpath variable +#2) Use in your shell: $c [directory] +#2.5) You *have to* use an argument. If you wish to simply $cd, use $cd command. +#3) If the path exists, you will cd into the first match found (the command is iterating over the list in the correct order, +# i.e. first element is being iterated overin the first place) +#3.5) But if path does not exist, you will receive a proper echo. +#----------------------------------------------------------------------------------------------------------------------------------- +# #Written by skelly37 +#------------------------ + startup = [ - "let cdpath = [. /data /data/it /data/games]", + "let cdpath = [. /place/your ~/cdpath/here ]", "def c [dir] { let wd = (pwd); for element in $cdpath {if (pwd) == $wd {cd $element; for directory in (ls -a | select name type | each { if $it.type == Dir {echo $it.name} {} } ) {if $dir == $directory {cd $dir} {}}; if (pwd) == $element {cd $wd} {}} {}}; if (pwd) == $wd {cd $wd; echo \"No such path!\"} {}}", ]