xPMo / zsh-prompt-dir-glob

ISC License
2 stars 0 forks source link

Support p10k instant prompt #2

Open xPMo opened 4 years ago

xPMo commented 4 years ago

See here. This is not public API (I think), but this segment is more time-consuming to run than the standard dir segment, so if this can make the prompt more responsive, I want to support it. Additionally, we could cache individual directories, so when descending, only the new directories in PWD need to be checked against the user's globs.

@romkatv if you have time, I'd like some info on these.

romkatv commented 4 years ago

This is not public API (I think)

Indeed, it's not public. Anything that starts with an underscore is private. If you rely on any of these symbols, your code can break when you update p10k.

xPMo commented 4 years ago

Okay, cache is private. I just need a key-val store; I'll make my own.

Is instant prompt public? Each segment defines an instant_prompt_$segment function, which isn't prefixed with _. Is calling p10k segment ... in instant_prompt_$segment sufficient to supporting it?

romkatv commented 4 years ago

Is instant prompt public?

The API for defining instant prompt segments is public. See https://github.com/romkatv/powerlevel10k/blob/8ef2b737d1f6099966a1eb16bdfc90d67b367f22/config/p10k-lean.zsh#L935-L952.

Is calling p10k segment ... in instant_prompt_$segment sufficient to supporting it?

Yes.

Note that the key in the instant prompt database is undocumented. The public documentation linked above implicitly says that the key is empty, meaning that p10k stores a single instant prompt. This isn't actually true but may change in the future. If you assume that there is only one instant prompt, your code won't break.

Here are a few examples. Prompt segment foo shows username if $FOO is not empty. Robust implementation that won't break:

function prompt_foo() { p10k segment -c '$FOO' -t '%n' }
function instant_prompt_foo() { prompt_foo }

Implementation that currently works (because username is a part of the key for instant prompt) but may break:

function prompt_foo() { p10k segment -c '$FOO' -t $USER }
function instant_prompt_foo() { prompt_foo }

Implementation that doesn't work:

function prompt_foo() { [[ -n $FOO ]] && p10k segment -t '%n' }
function instant_prompt_foo() { prompt_foo }

Here by "doesn't work" or "will break" I mean that instant prompt will show incorrect information (different from regular prompt).