help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] `read -e -p`, colors, and invisible chars


From: Mike Frysinger
Subject: [Help-bash] `read -e -p`, colors, and invisible chars
Date: Wed, 26 Aug 2015 21:01:44 -0400

my desire is simple:
  show a custom colorized prompt when reading input and leverage readline
  (for editing & history)

the `read` builtin almost gets me there:
 - the -p flag allows you to specify a custom prompt
 - the -e flag uses readline for input

however, the -p flag does not parse escape sequences.  np, just expand
that myself ahead of time using `printf %b`.  simple example:
  prompt=$(printf '%bfoo>%b ' '\033[1;33m' '\033[0m')
  read -e -p "${prompt}"

now i hit the classic invisible char problem when readline tries to
redraw the prompt (just hold up on the arrow key to draw many lines
from your history).  normally i'd leverage \[ and \], but bash does
not seem to support that w/the read builtin -- they get rendered in
the output directly instead of being consumed.
  $ prompt=$(printf '%bfoo>%b ' '\[\033[1;33m\]' '\[\033[0m\]')
  $ read -e -p "${prompt}"
  \[\]foo>\[\] 
(yes, moving the \[ and \] to the printf string yields same behavior)

am i out of luck ?  if so, can we change the behavior of -p to support
escape sequences naturally ?  maybe have it go through the same prompt
code paths as the bash loop ?  e.g. this would "just work":
  read -e -p "$PS1"

if we're worried about breaking existing -p users (who might have an
escape sequence in there and actually want it), maybe we can add a
new -P flag ?  although i'd say too bad and just put it behind the
existing bash version compat shopt settings ...
-mike

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]