powerlevel9k icon indicating copy to clipboard operation
powerlevel9k copied to clipboard

[Feature request] An option to turn `time` segment into realtime clock

Open romkatv opened this issue 6 years ago • 12 comments
trafficstars

It would be nice to have an option that would turn time segment into realtime clock. That is, the segment would look the same as before but it would constantly update and always display current time. Users are asking for it. This feature exists in several other themes.

I implemented it in Powerlevel10k and it looks cool. However, I'd very much like to avoid having visual features that don't exist in Powerlevel9k because it'll make it difficult for the users to switch back to Powerlevel9k if/when it becomes faster. Hence I named the option POWERLEVEL9K_EXPERIMENTAL_TIME_REALTIME with EXPERIMENTAL in the name. If you could add POWERLEVEL9K_TIME_REALTIME (or use some other name that isn't as obscure), I'll rename my option to match.

romkatv avatar Mar 13 '19 18:03 romkatv

@romkatv - That's really cool! I would love to upstream this.

I'd like to get this in as a new clock segment. I actually prefer not having the clock update in my personal prompt, as it's easy for me to look at that segment to see when the previous command completed rather than scrolling up to when I executed it and back-calculating based on the time expired segment.

@romkatv - Can you file a PR against next that adds this feature as a new clock segment, perhaps?

bhilburn avatar Mar 13 '19 18:03 bhilburn

I'd like to get this in as a new clock segment.

The advantage of a separate segment is that it allows both time segments to be visible simultaneously -- the time of the last command completion and the time of the next command start (which is ticking at the current prompt). Both are useful, but in practice it's better to use one kind of time segment plus command_execution_time. The other time can be computed from these.

When I scroll up in my terminal, I personally prefer to see when each command started (plus its duration) rather than when each has ended. It feels more intuitive. Plus, it makes prompt look cooler, even more eye-candy. But I can see how one might prefer to see end times, mostly due to habbit.

The disadvantage of having two time segments is UX complexity. Note that these segments will have identical set of configuration options (time has only one option currently).

@romkatv - Can you file a PR against next that adds this feature as a new clock segment, perhaps?

I'll defer to your dev team. Feel free to look at my implementation or just roll your own. One word of caution: don't use TMOUT to implement this because there can be only one TMOUT handler in a shell. It's not nice to claim a singleton resource and deny its use to everyone else. Powerlevel9k already does this with zle-keymap-select, and it's also not nice.

romkatv avatar Mar 13 '19 18:03 romkatv

I'd like to take a look at this. A few things.

I currently rebound my enter to redraw my prompt if I hit it, so I get the execution time.

I agree with @romkatv. It should be implemented in the same segment but configurable. @dritter introduced the concept of tags for segments which could make it possible to have the same segment with different configuration if you so choose. This would be something for the future though.

If I can, I'll try to get it ready for 0.7.0 but don't hold the release for this.

I'd like to copy and modify your code if that's OK with you, @romkatv. I'll give credit to you of course.
Also thanks for the pointer, I have no idea what you are talking about 😅

Syphdias avatar Mar 13 '19 19:03 Syphdias

Good point re: multiple segments, @romkatv.

Thanks for taking this on, @Syphdias!

bhilburn avatar Mar 13 '19 19:03 bhilburn

I'd like to copy and modify your code if that's OK with you, @romkatv. I'll give credit to you of course.

Go ahead. It's a tiny piece of code, so don't worry about giving credit.

Note that I haven't looked at next at all, so I don't know how easy it is to implement realtime clock there. I think in powerlevel9k/master it would be rather difficult.

Here's an important test for this feature: cd into a large git repo and verify that your prompt doesn't freeze every second. In other words, clock refresh shouldn't trigger vcs segment refresh.

Good luck!

romkatv avatar Mar 13 '19 19:03 romkatv

POWERLEVEL9K_MODE="nerdfont-complete"
POWERLEVEL9K_LEFT_SEGMENT_SEPARATOR=$''
POWERLEVEL9K_RIGHT_SEGMENT_SEPARATOR=$''
POWERLEVEL9K_CONTEXT_TEMPLATE='\uFB82%n'
POWERLEVEL9K_CONTEXT_DEFAULT_FOREGROUND='white'
POWERLEVEL9K_CONTEXT_DEFAULT_BACKGROUND='none'
POWERLEVEL9K_TIME_BACKGROUND='none'
POWERLEVEL9K_TIME_FOREGROUND='006'
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(os_icon context time dir vcs)
# POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(ram load background_jobs)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=()
POWERLEVEL9K_CUSTOM_TIME_FORMAT="%D{%H:%M:%S}"
setopt PROMPT_SUBST
PROMPT='%B%F{red}%n@%m%f%F{yellow}[%D{%L:%M:%S}]%f:%F{blue}${${(%):-%~}}%f$ %b'
TMOUT=1
TRAPALRM() {
    zle reset-prompt
}

This works perfect for me. image

0xdhrv avatar Apr 27 '19 12:04 0xdhrv

@dhrvsthr There are several known issues with this approach.

  1. cd into a git repository and then press and hold a or any other key. You'll see that once a second your prompt freezes. In a large git repository these freezes can be quite long.
  2. Enable completion menu via zstyle ':completion:*' menu yes select and then type ls<TAB><TAB>. After a second all menu options except for the selected one will disappear.

There are more of these.

romkatv avatar Apr 27 '19 12:04 romkatv

I still like the idea and I don't think there is any harm in keeping the PR open for reference/documentation.
@romkatv If you find a fix/workaround for the big bugs (with vanilla libraries, no compiling stuff, no binaries 😉) I'll happily port it.

Syphdias avatar Apr 27 '19 14:04 Syphdias

@Syphdias Sure thing. For the reference, here's the current state w.r.t. the issues that I know about.

To be addressed by changes in Powerlevel9k:

  1. Prompt freezes every second. Only the time segment should be refreshed, not the whole prompt.

Already fixed in ZSH but will still take a while before most users pick up the fixes:

  1. History breaks.
  2. Handlers registered with zle -F stop firing after every signal until the next signal.

Still broken and no one is working on fixing it:

  1. Completion menu breaks on reset-prompt.
  2. Some terminals experience visible flickering and cursor jumps on reset-prompt.

romkatv avatar Apr 27 '19 15:04 romkatv

@Syphdias Sure thing. For the reference, here's the current state w.r.t. the issues that I know about.

To be addressed by changes in Powerlevel9k:

  1. Prompt freezes every second. Only the time segment should be refreshed, not the whole prompt.

Already fixed in ZSH but will still take a while before most users pick up the fixes:

  1. History breaks.
  2. Handlers registered with zle -F stop firing after every signal until the next signal.

Still broken and no one is working on fixing it:

  1. Completion menu breaks on reset-prompt.
  2. Some terminals experience visible flickering and cursor jumps on reset-prompt.

I really love the REALTIME option and thank very much for developing it. But I have run into the problem that the realtime clock ticks every 2 seconds. I use zsh on OS X. I do not know whether this is designed to be like so.

KeeeeiZ avatar Nov 16 '19 18:11 KeeeeiZ

realtime clock ticks every 2 seconds. I do not know whether this is designed to be like so.

It is not.

romkatv avatar Nov 17 '19 07:11 romkatv

I implemented it in Powerlevel10k and it looks cool.

I think this feature never made it into p9k which is why this issue is still open.

I really love the REALTIME option and thank very much for developing it.

So you are either using the proof of concept commit(s) I made or you are not using p9k.

This feature is currently not supported by p9k. If you are not using p9k, please open an issue against the project you are using.

Syphdias avatar Nov 17 '19 15:11 Syphdias