zsh4humans icon indicating copy to clipboard operation
zsh4humans copied to clipboard

[Question] What is the Correct Function to Force a Prompt Redraw

Open Syphdias opened this issue 3 years ago • 9 comments

Hey, I'm trying to add some additional functionality (that is probably only useful for me) to z4h-cd-back and I found -z4h-redraw-prompt in -z4h-cd-rotate.

Can I use -z4h-redraw-prompt or is there a public function for redrawing the prompt. I feel like we had this discussion once before in the context of p10k but I cant find the corresponding issue.

The "probably only useful to me" background: I used to split my terminal emulator tilix which made it possible to use vde magic to get a new prompt in the same directory as the old terminal.
Nowadays I usually open new terminals via i3wm. I lose the vde features by doing that and to be honest most of the time I don't need to be in the same directory. I'd like to have the option to switch to it quickly though. So my two step idea:

  1. mark the directory (e.g. through a function maybe bound to a shortcut/zle-widget)
  2. make it possible to jump to that mark (either automatically on new zsh session or manually by calling "something")
    This "something" could be my-z4h-cd-back which is z4h-cd-back unless there is a recent "mark", then it jumps to that AND needs to redraw the prompt (thus the question)

Syphdias avatar Oct 30 '20 13:10 Syphdias

I feel like we had this discussion once before in the context of p10k but I cant find the corresponding issue.

Found the issue where we talked about it: https://github.com/romkatv/powerlevel10k/issues/72

Syphdias avatar Oct 30 '20 13:10 Syphdias

I'm going to add a new widget similar to z4h-cd-down that will display previous directories. Sort of like persistent cd history, similar to z. The last visited directory will always be the first in the list, so you'll be able to hit the widget's shortcut and then Enter.

Would that work for you?

P.S.

Functions and parameters that start with -z4h or _z4h are internal. Don't use them. If necessary, I can add z4h redraw-prompt or something like that.

romkatv avatar Oct 30 '20 13:10 romkatv

I'm going to add a new widget similar to z4h-cd-down that will display previous directories. Sort of like persistent cd history, similar to z. The last visited directory will always be the first in the list, so you'll be able to hit the widget's shortcut and then Enter.

Would that work for you?

I think z is similar to autojump and jumps to recently visited places but is more trimmed towards most often visited. What you describe sounds better that what I want and what I was planing. :joy:

[...] and then Enter

Or tab (zstyle ':z4h:new-cd-widget' fzf-bindings 'tab:down')

Functions and parameters that start with -z4h or _z4h are internal. Don't use them. If necessary, I can add z4h redraw-prompt or something like that.

I expected as much. I'll wait for the new widget which will probably satisfy my needs. :wink:

Syphdias avatar Oct 30 '20 13:10 Syphdias

The widget is currently blocked on finding a new keybidning for it. I'm open to changing some of the existing bindings if that'll help.

For context, right now on PC cd widgets are bound to Alt+Arrow and on Mac to Shift+Arrow. They use all four arrows. How can a new cd widget fit in there? Or maybe one of the existing widgets should be dropped or modified?

romkatv avatar Oct 30 '20 14:10 romkatv

Left and right are so close together you can probably not scrap one without the other. I guess z4h-cd-up is the least useful? Can you combine two arrows? like Alt+Left+Right?

Syphdias avatar Oct 31 '20 22:10 Syphdias

At some point we discussed to support Alt+Up while you are in Alt+Down, I think it can help here. Here's my proposal:

  • If you are jumping back and forth between two or three directories, you use Alt+Left/Right (as today).
  • If you want to navigate to somewhere that is close relatively to the current folder, you use Alt+Down, and in there you can either go up with Alt+Up, or go down just using filter in fzf. This way, Alt+Up itself (not inside Alt+Down) becomes available for a new widget.
  • If you want to navigate to a completely different, frequently visited place, you use Alt+Up (bound to the new widget) and you get fzf with most frequently used folders

maximbaz avatar Nov 01 '20 19:11 maximbaz

IMO Alt+Up is the most useful of the bunch. I don't want to lose it.

I think I'll add a new key to get started. Perhaps Alt+C as a mnemonic for cd. In there future there also might be Alt+D and Alt+F for inserting directories and files in the command line (fetched from history).

romkatv avatar Nov 01 '20 19:11 romkatv

  • If you want to navigate to a completely different, frequently visited place, you use Alt+Up (bound to the new widget) and you get fzf with most frequently used folders

I like the idea of combining it with another arrow key. But I would argue for combining it with Alt+Right and Alt+Left.

In the case of just having opened a new terminal there is nowhere to go previous or next except previously visited places.

  • Alt+Left could be jumping instantly to the most recently visited directory.
  • Alt+Rightcould open a fzf with the most recent directories.

For the cases where you already have something in the "cd history stack". I'd argue that it would be worth sacrificing or blurring the lines a little bit. If you are in the most recent directory (no Alt+Left used yet) and you use Alt+Right, open a fzf with the most recently visited directories. This would include the entire history stack, of course, but also other directories visited in other terminal sessions (maybe even open ones). To loop around you would press Alt+Right again to leave fzf again an be back at ~. Also Alt+Left would quit the fzf in case you overshot from navigating to the most "right" directory.

Example: Single cds into "Projects", "the-project", ~/Downloads, unzipped/folder. The imaginary stack would look like this and could be traversed by Alt+Left and Alt+Right:

$most-recent-directory # from old session or ~/Downloads/unzipped/folder
~ # 10
~/Projects
~/Projects/the-project
~/Downloads
~/Downloads/unzipped/folder
$fzf
# GOTO 10

Going down with Alt+Left, going up with Alt+Right.

This idea is quite complicated which could lower UX due to being complicated. Maybe it can be improved.

Syphdias avatar Nov 01 '20 22:11 Syphdias

I've attempted to provide something along the lines of what's been discussed here. Please see https://github.com/romkatv/zsh4humans/issues/35#issuecomment-848940481.

romkatv avatar May 26 '21 16:05 romkatv

This has been done a long time ago. Alt-R is the default binding for a new widget called :z4h:fzf-dir-history. There is a way to configure it so that it can be rebound to Alt-Down without losing the original functionality of that key combo. See https://github.com/romkatv/zsh4humans/blob/master/tips.md#current-directory.

romkatv avatar Jan 12 '24 05:01 romkatv