06ced8274de7c2edd19cd5be4642bb0ec0772437

Author
Kayhan Gültekin <kayhan@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(dircycle): add bindings to go up or down in hierarchy (#12291)

Co-authored-by: Marc Cornellà <marc@mcornella.com>

Diff

  1diff --git a/plugins/dircycle/README.md b/plugins/dircycle/README.md
  2index 3c9b3a96f6533e821f688ade77c9715168103380..c4105558d67efb51d3f66e54a35a3f13f532db49 100644
  3--- a/plugins/dircycle/README.md
  4+++ b/plugins/dircycle/README.md
  5@@ -37,13 +37,13 @@ Say you opened these directories on the terminal:
  6 3       ~
  7 ```
  8 
  9-By pressing <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Left</kbd>, the current working directory or `$CWD` will be from `oh-my-zsh` to `Hacktoberfest`. Press it again and it will be at `Projects`.
 10+By pressing <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Left</kbd>, the current working directory or `$PWD` will be from `oh-my-zsh` to `Hacktoberfest`. Press it again and it will be at `Projects`.
 11 
 12-And by pressing <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Right</kbd>, the `$CWD` will be from `Projects` to `Hacktoberfest`. Press it again and it will be at `oh-my-zsh`.
 13+And by pressing <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Right</kbd>, the `$PWD` will be from `Projects` to `Hacktoberfest`. Press it again and it will be at `oh-my-zsh`.
 14 
 15 Here's a example history table with the same accessed directories like above:
 16 
 17-| Current `$CWD`  | Key press                                             | New `$CWD`      |
 18+| Current `$PWD`  | Key press                                             | New `$PWD`      |
 19 | --------------- | ----------------------------------------------------- | --------------- |
 20 | `oh-my-zsh`     | <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Left</kbd>  | `Hacktoberfest` |
 21 | `Hacktoberfest` | <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Left</kbd>  | `Projects`      |
 22@@ -53,7 +53,7 @@ Here's a example history table with the same accessed directories like above:
 23 | `Hacktoberfest` | <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Right</kbd> | `oh-my-zsh`     |
 24 | `oh-my-zsh`     | <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Right</kbd> | `~`             |
 25 
 26-Note the last traversal, when pressing <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Right</kbd> on a last known `$CWD`, it will change back to the first known `$CWD`, which in the example is `~`.
 27+Note the last traversal, when pressing <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Right</kbd> on a last known `$PWD`, it will change back to the first known `$PWD`, which in the example is `~`.
 28 
 29 Here's an asciinema cast demonstrating the example above:
 30 
 31@@ -61,18 +61,22 @@ Here's an asciinema cast demonstrating the example above:
 32 
 33 ## Functions
 34 
 35-| Function             | Description                                                                                               |
 36-| -------------------- | --------------------------------------------------------------------------------------------------------- |
 37-| `insert-cycledleft`  | Change `$CWD` to the previous known stack, binded on <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Left</kbd> |
 38-| `insert-cycledright` | Change `$CWD` to the next known stack, binded on <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Right</kbd>    |
 39+| Function             | Description                                                                                                         |
 40+| -------------------- | ------------------------------------------------------------------------------------------------------------------- |
 41+| `insert-cycledleft`  | Change `$PWD` to the previous known stack, bound to <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Left</kbd>            |
 42+| `insert-cycledright` | Change `$PWD` to the next known stack, bound to <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Right</kbd>               |
 43+| `insert-cycledup`    | Change `$PWD` to the parent folder, bound to <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Up</kbd>                     |
 44+| `insert-cycleddown`  | Change `$PWD` to the first alphabetical child folder, bound to <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Down</kbd> |
 45 
 46 ## Rebinding keys
 47 
 48-You can bind these functions to other key sequences, as long as you know the bindkey sequence. For example, these commands bind to <kbd>Alt</kbd> + <kbd>Shift</kbd> + <kbd>Left</kbd> / <kbd>Right</kbd> in `xterm-256color`:
 49+You can bind these functions to other key sequences, as long as you know the bindkey sequence. For example, these commands bind to <kbd>Alt</kbd> + <kbd>Shift</kbd> + <kbd>key</kbd> in `xterm-256color`:
 50 
 51 ```zsh
 52 bindkey '^[[1;4D' insert-cycledleft
 53 bindkey '^[[1;4C' insert-cycledright
 54+bindkey "\e[1;4A" insert-cycledup
 55+bindkey "\e[1;4B" insert-cycleddown
 56 ```
 57 
 58 You can get the bindkey sequence by pressing <kbd>Ctrl</kbd> + <kbd>V</kbd>, then pressing the keyboard shortcut you want to use.
 59diff --git a/plugins/dircycle/dircycle.plugin.zsh b/plugins/dircycle/dircycle.plugin.zsh
 60index bb69f6b3fd7bb3a647aeb10310e98b39d6021c9a..8c03594ba69684b4d430366a6b6d9b2b264fa564 100644
 61--- a/plugins/dircycle/dircycle.plugin.zsh
 62+++ b/plugins/dircycle/dircycle.plugin.zsh
 63@@ -8,7 +8,16 @@
 64 #  pushd +N: start counting from left of `dirs' output
 65 #  pushd -N: start counting from right of `dirs' output
 66 
 67+# Either switch to a directory from dirstack, using +N or -N syntax
 68+# or switch to a directory by path, using `switch-to-dir -- <path>`
 69 switch-to-dir () {
 70+	# If $1 is --, then treat $2 as a directory path
 71+	if [[ $1 == -- ]]; then
 72+		# We use `-q` because we don't want chpwd to run, we'll do it manually
 73+		[[ -d "$2" ]] && builtin pushd -q "$2" &>/dev/null
 74+		return $?
 75+	fi
 76+
 77 	setopt localoptions nopushdminus
 78 	[[ ${#dirstack} -eq 0 ]] && return 1
 79 
 80@@ -22,10 +31,10 @@ switch-to-dir () {
 81 }
 82 
 83 insert-cycledleft () {
 84-	switch-to-dir +1 || return
 85+	switch-to-dir +1 || return $?
 86 
 87 	local fn
 88-	for fn (chpwd $chpwd_functions precmd $precmd_functions); do
 89+	for fn in chpwd $chpwd_functions precmd $precmd_functions; do
 90 		(( $+functions[$fn] )) && $fn
 91 	done
 92 	zle reset-prompt
 93@@ -33,22 +42,46 @@ insert-cycledleft () {
 94 zle -N insert-cycledleft
 95 
 96 insert-cycledright () {
 97-	switch-to-dir -0 || return
 98+	switch-to-dir -0 || return $?
 99 
100 	local fn
101-	for fn (chpwd $chpwd_functions precmd $precmd_functions); do
102+	for fn in chpwd $chpwd_functions precmd $precmd_functions; do
103 		(( $+functions[$fn] )) && $fn
104 	done
105 	zle reset-prompt
106 }
107 zle -N insert-cycledright
108 
109+insert-cycledup () {
110+	switch-to-dir -- .. || return $?
111+
112+	local fn
113+	for fn in chpwd $chpwd_functions precmd $precmd_functions; do
114+		(( $+functions[$fn] )) && $fn
115+	done
116+	zle reset-prompt
117+}
118+zle -N insert-cycledup
119+
120+insert-cycleddown () {
121+	switch-to-dir -- "$(find . -mindepth 1 -maxdepth 1 -type d | sort -n | head -n 1)" || return $?
122+
123+	local fn
124+	for fn in chpwd $chpwd_functions precmd $precmd_functions; do
125+		(( $+functions[$fn] )) && $fn
126+	done
127+	zle reset-prompt
128+}
129+zle -N insert-cycleddown
130 
131 # These sequences work for xterm, Apple Terminal.app, and probably others.
132 # Not for rxvt-unicode, but it doesn't seem differentiate Ctrl-Shift-Arrow
133 # from plain Shift-Arrow, at least by default.
134+#
135 # iTerm2 does not have these key combinations defined by default; you will need
136 # to add them under "Keys" in your profile if you want to use this. You can do
137 # this conveniently by loading the "xterm with Numeric Keypad" preset.
138-bindkey "\e[1;6D" insert-cycledleft
139-bindkey "\e[1;6C" insert-cycledright
140+bindkey "\e[1;6D" insert-cycledleft     # Ctrl+Shift+Left
141+bindkey "\e[1;6C" insert-cycledright    # Ctrl+Shift+Right
142+bindkey "\e[1;6A" insert-cycledup       # Ctrl+Shift+Up
143+bindkey "\e[1;6B" insert-cycleddown     # Ctrl+Shift+Down