zsh-users
 help / color / mirror / code / Atom feed
* Tiny problem with a prompt
@ 2011-07-30 12:57 Tomasz Moskal
  2011-07-30 13:00 ` Mikael Magnusson
  0 siblings, 1 reply; 4+ messages in thread
From: Tomasz Moskal @ 2011-07-30 12:57 UTC (permalink / raw)
  To: zsh-users

I am trying to display current directory in different colour based on if
it's writeable or not. What I thought will be simple  if...then...else
task has become a really headache. My (simplistic) code is as follows:


prompt_k2_setup() {
    
    pdir() {
        if [[ -w $PWD ]]; then
            dirp='%3F%1/%3f '
        else
            dirp='%1F%1/%1f '
        fi  
    }   
    
    precmd() { pdir }

    PS1="${dirp} "
}   

prompt_k2_setup "$@"


It sort off works, but not as I intended it to - for the prompt to show
current directory I need to execute  'prompt k2'  each time I change to
different directory.

I am probably missing something painfully obvious but I can't figure it
out. A nudge in the right direction will be greatly appreciated.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Tiny problem with a prompt
  2011-07-30 12:57 Tiny problem with a prompt Tomasz Moskal
@ 2011-07-30 13:00 ` Mikael Magnusson
  2011-07-30 13:47   ` Tomasz Moskal
  0 siblings, 1 reply; 4+ messages in thread
From: Mikael Magnusson @ 2011-07-30 13:00 UTC (permalink / raw)
  To: ramshackle.industries; +Cc: zsh-users

On 30 July 2011 14:57, Tomasz Moskal <ramshackle.industries@gmail.com> wrote:
> I am trying to display current directory in different colour based on if
> it's writeable or not. What I thought will be simple  if...then...else
> task has become a really headache. My (simplistic) code is as follows:
>
>
> prompt_k2_setup() {
>
>    pdir() {
>        if [[ -w $PWD ]]; then
>            dirp='%3F%1/%3f '
>        else
>            dirp='%1F%1/%1f '
>        fi
>    }
>
>    precmd() { pdir }
>
>    PS1="${dirp} "
> }
>
> prompt_k2_setup "$@"
>
>
> It sort off works, but not as I intended it to - for the prompt to show
> current directory I need to execute  'prompt k2'  each time I change to
> different directory.
>
> I am probably missing something painfully obvious but I can't figure it
> out. A nudge in the right direction will be greatly appreciated.

You need to update PS1 inside precmd(), or use promptsubst and protect
the parameter expansion.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Tiny problem with a prompt
  2011-07-30 13:00 ` Mikael Magnusson
@ 2011-07-30 13:47   ` Tomasz Moskal
  2011-07-30 14:10     ` Mikael Magnusson
  0 siblings, 1 reply; 4+ messages in thread
From: Tomasz Moskal @ 2011-07-30 13:47 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: zsh-users

On Sat, 2011-07-30 at 15:00 +0200, Mikael Magnusson wrote:

> You need to update PS1 inside precmd(), or use promptsubst and protect
> the parameter expansion.

Brilliant! Updating PS1 in precomd() did the trick. Would you mind
explaining what did you mean by "protecting the parameter expansion" as
the other solution?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Tiny problem with a prompt
  2011-07-30 13:47   ` Tomasz Moskal
@ 2011-07-30 14:10     ` Mikael Magnusson
  0 siblings, 0 replies; 4+ messages in thread
From: Mikael Magnusson @ 2011-07-30 14:10 UTC (permalink / raw)
  To: ramshackle.industries; +Cc: zsh-users

On 30 July 2011 15:47, Tomasz Moskal <ramshackle.industries@gmail.com> wrote:
> On Sat, 2011-07-30 at 15:00 +0200, Mikael Magnusson wrote:
>
>> You need to update PS1 inside precmd(), or use promptsubst and protect
>> the parameter expansion.
>
> Brilliant! Updating PS1 in precomd() did the trick. Would you mind
> explaining what did you mean by "protecting the parameter expansion" as
> the other solution?

Either by '$foo' or "\$foo" for example. Ie, put a literal $ sign in
the string. I would prefer updating PS1 in precmd() to setting
promptsubst though.


-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-07-30 14:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-30 12:57 Tiny problem with a prompt Tomasz Moskal
2011-07-30 13:00 ` Mikael Magnusson
2011-07-30 13:47   ` Tomasz Moskal
2011-07-30 14:10     ` Mikael Magnusson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).