Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [RFC][PATCH] `newuser` prompt theme



[only vcs_info]
On 4/14/21, Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
> Marlon wrote on Wed, Apr 14, 2021 at 16:17:02 +0300:
>> On 14 Apr 2021, at 15:05, Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
>> >> +
>> >> +  if ! [[ -v vcs_info_msg_0_ ]]; then
>> >> +    zstyle ':vcs_info:*' check-for-staged-changes yes
>> >
>> > First, no other prompt theme sets styles, so I'm not sure prompt themes
>> > should be doing that.
>>
>> Please show me another way to theme the VCS part of the prompt and I will
>> use that.
>
> You seem to have checked your manners in at the door.
>
> In any case, what you've implemented is that you re-set a style on the
> first precmd after every chdir.  I don't think these semantics should be
> implemented in the first place.
>
>> > Second, even if a prompt were to set styles, doing so in a precmd and
>> > behind what _looks_ like a "first run" condition but is actually a "we
>> > just changed directory" condition isn't exactly best practice.
>>
>> What would be a better practice?
>
> Don't surprise/mislead code readers.
>
> Don't surprise/mislead users.
>
> Document your code.
>
> Make defaults overridable.
>
> Don't trample user settings (e.g., someone explicitly setting that style
> to a false value for that same context, or for the context «:vcs_info*»,
> which is less specific than the context you use and thus would be
> shadowed).
>
> These are all such ground rules that I'm surprised I have to spell them
> out.
>
>> >> +prompt_newuser_setup() {
>> >> +  prompt_opts=( cr percent sp )
>> >> +
>> >> +  zstyle -e ':vcs_info:*' formats '
>> >> +    reply=( "%u%c$( prompt_newuser_format start branch repo )" )
>> >> +  '
>> >> +  zstyle -e ':vcs_info:*' actionformats '
>> >> +    reply=( "%u%c$( prompt_newuser_format start action repo )" )
>> >> +  '
>> >> +  zstyle -e ':vcs_info:*' stagedstr '
>> >> +    reply=( "$( prompt_newuser_format start staged )" )
>> >> +  '
>> >> +  zstyle -e ':vcs_info:*' unstagedstr '
>> >> +    reply=( "$( prompt_newuser_format start unstaged )" )
>> >> +  '
>> >
>> > Your theme does _nothing_ with the 'unstaged' style other than
>> > pass it through verbatim.
>>
>> After setting it to '%B%F{red}*%b%f’, yes.
>>
>
> Hmm.  That's a good question: how _should_ a theme set an overrideable
> default for stagedstr?

It's actually very easy to do so (somewhat pseudocodified actual code):
setup() {
  zstyle ':vcs_info:*:prompt-mikachu:*' actionformats %b %a
  zstyle ':vcs_info:*:prompt-mikachu:*' formats %b
  zstyle ':vcs_info:*:prompt-mikachu:*' max-exports 2
  zstyle ':vcs_info:(hg|svn):prompt-mikachu:*' formats '%b[%s]'

  autoload -Uz vcs_info
}

precmd() {

  local vcs_info_msg_{0..1}_
  vcs_info prompt-mikachu
  psvar[3]=( "${vcs_info_msg_0_:-}" )
  psvar[4]=( "${vcs_info_msg_1_:-}" )
}

eg, the first argument to vcs_info is used as a context to look up
values of styles, so you can very easily namespace all your settings
to your theme. localing vcs_info_msg_*_ is also important so that you
don't interfere with any commandline usage of vcs_info (I didn't check
if you do this).

-- 
Mikael Magnusson




Messages sorted by: Reverse Date, Date, Thread, Author