Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prompt segment for current history mode: global vs. per-directory #2384

Closed
cohml opened this issue Jul 21, 2023 · 7 comments
Closed

Add prompt segment for current history mode: global vs. per-directory #2384

cohml opened this issue Jul 21, 2023 · 7 comments

Comments

@cohml
Copy link

cohml commented Jul 21, 2023

The per-directory-history zsh plugin allows users to toggle between directory-specific command histories and overall command histories. This allows for much more rapid and effective history searching. It's awesome.

However, it is kind of annoying to have to manually check my history to deduce which mode I'm in. E.g. history | wc -l --> huge number indicates "global history mode", while a smaller number indicates per-directory history mode. Note that simply scrolling through the last few commands is not always clear, as these may be generic, e.g., ls -l.

In the P10K spirit of an info-packed prompt, it would be so useful to add a prompt element which concisely shows which mode your history is in. For example:

~/some_dir [G]    # global history mode
~/some_dir [D]    # per-directory history mode

From a quick skim of your source code, it seems like the POWERLEVEL9K_*_PROMPT_ELEMENTS variables may be the most sensible place to set this. But maybe there's a better way.

@cohml
Copy link
Author

cohml commented Jul 21, 2023

It appears that the per-directory-history plugin sets a _per_directory_history_is_global variable which is true if mode == global else false.

That could be a nice, simple way to detect the mode within P10K and update the prompt accordingly.

@rwmitchell
Copy link

Concur!

I use per-directory-history but never toggle to global because there is no clear indication which mode is being used.

@romkatv
Copy link
Owner

romkatv commented Jul 21, 2023

You can use a custom prompt segment for this.

function prompt_my_history() {
  if [[ $_per_directory_history_is_global == true ]]; then
    p10k segment -b yellow -f red -t G
  else
    p10k segment -b yellow -f red -t L
  fi
}

Put this in ~/.p10k.zsh and add my_history to POWERLEVEL9K_LEFT_PROMPT_ELEMENTS or POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS in the same file. You can customize colors and the icon the same way as for all other segments.

See p10k help segment for docs.

@cohml
Copy link
Author

cohml commented Jul 22, 2023

Wow, this is perfect and works like an absolute charm! Thank you for making it so easy!

For @rwmitchell, and others in the future interested to know, here is the entirety of what I added to my p10k.zsh file to get this working, added around line 1650 where the discussion of custom prompt segments is located:

function prompt_my_history_mode() {
  if [[ $_per_directory_history_is_global = true ]]; then
    p10k segment -f '#e57374' -t "%BG"
  else
    p10k segment -f '#e57374' -t "%BL"
  fi
}
echo "${plugins}" | grep -q "per-directory-history" && POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(
  my_history_mode
  ${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS}
)

This will add the proposed segment only when the per-directory-history plugin is enabled.

The result (for me) looks as follows:

image image

@romkatv If you think this segment or a simplified version might be more widely useful, I'd be happy to submit a PR for it. Otherwise, my feelings are not hurt :)

Thanks again!

@rwmitchell
Copy link

I'm going to assume Roman is not going to add it because he's trying minimize complexity while maximizing only the most commonly used features. As it isn't hard to add custom segments, I've added (and dropped some) several of my own, it isn't necessary to add everyone's pet feature.

While these do not need to be added to p10k, I love sharing ideas people come up with, either to use outright or to modify. The question becomes where to share them, that encourages community activity and easily discoverable by other p10k users.

@romkatv
Copy link
Owner

romkatv commented Jul 25, 2023

I added per_directory_history to Powerlevel10k. It's enabled by default in configs generated by p10k configure (except if you choose Pure style).

@cohml
Copy link
Author

cohml commented Jul 25, 2023

That's awesome! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants