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

Re: print color escapes



On Jan 1, 11:24am, Ray Andrews wrote:
}
} Ok, we hafta stop the terminal from doing it's thing when we want to 
} 'see' the escape code without the terminal 'doing' the escape code. But 
} why/where is the change made?

The pair "\e" is interpreted by the "print" command and changed into an
ascii 033 character, UNLESS you use the -r or -R options.  So in your
script:

}     cyan='\e[36;1m'
}     norm='\e[0m'
}     print    "1: $fg[cyan] howdy $norm"
}     print    "2: $cyan howdy $norm"

above, "print" has changed \e to ESC, but:

}     print -r "7: the value of fg[cyan] is: ${(V)fg[cyan]}"
}     print -r "8: the value of cyan       is: ${(V)cyan}"

there, print has *not* done so.

The value of $fg[cyan] already has a literal ESC in it, so "print" does
nothing with that either way.  In these lines:

}     7: the value of fg[cyan] is: ^[[36;1m      << modified to 'bold' by me.
}     8: the value of cyan       is: \e[36;1m

There (V) flag has changed the literal ESC into ^[ for display, but there
is no literal ESC in $cyan so the (V) flag did nothing.

} ... either the " \e[ " or the " ^[[ " form produces the color change, so 

No, NEITHER of those forms produce the color change.  What produces the
color change is the literal ESC, which occurs either because it is already
there ($fg[cyan]) or because the "print" command translated \e ($cyan).

} why does the 'color' function 'bother' to change from one form to the 
} other, and how/where?

The color function does not change the form.  The (V) flag does, to make
it "visible".

} I see the escape created like this:
} 
}      local lc=$'\e[' rc=m

In that expression, the $'' form of quoting converts \e to ESC, which is
why the $'' form of quoting exists in the first place (and is different
from ordinary single quoting).  $'' is to avoid having to do something
more expensive (fork + read output) such as lc="$(echo '\e[')".

The (V) flag converts ESC to ^[ because that's what most people are used
to seeing ("visible").  The (q) flag converts to $'\e' because that's
what people are used to writing (and because "eval ${(q)...}" needs it
in that format).  The print and echo commands convert \e to ESC because
of historical practice.  The bindkey command converts both ^[ and \e to
ESC for maximum flexibility in writing key bindings.  All of this is
entirely independent of what the terminal does when it sees an ESC.

-- 
Barton E. Schaefer



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