0

I'm trying to ask for user invention in color without having the answer in a separate line.

I currently have this:

msg() {
    local mesg=$1; shift
    printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n"
}

ALL_OFF="$(tput sgr0)"
BOLD="$(tput bold)"
GREEN="${BOLD}$(tput setaf 2)"

[...]

until [[ $REPLY = [yY] ]]; do
    msg "Done (y/n)?" && read -p ""
done

But as mentioned, the problem is it puts the response to a new line:

==> Done (y/n)?
y

So how can I not do that?

Also is there any way to have the response in color as well?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Det
  • 3,640
  • 5
  • 20
  • 27
  • Usually you would call it a "prompt" not a "question". Did you try the ANSI escapes? http://stackoverflow.com/a/3586005/1180785 – Dave Apr 07 '13 at 23:38
  • @Dave, did you mean [this](http://stackoverflow.com/a/3585889/1821548)? This is not a C program. – Det Apr 08 '13 at 00:04
  • 1
    @Dave `tput` is responsible for looking up escape sequences -- it'll give you back ANSI codes if your terminal is ANSI, non-ANSI ones if those are what are appropriate, and no return value at all if your terminal has no support for the desired color/mode/whatnot. Much better to use it than to hardcode ANSI sequences and thus not support non-ANSI terminals at all. – Charles Duffy Apr 08 '13 at 04:20

1 Answers1

3
printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n"

If you don't want a newline, don't put a \n on the end of your format string.

You could also use read:

read_msg() {
  read -p "${GREEN}==>${ALL_OFF}${BOLD} $1${ALL_OFF}"
}
until [[ $REPLY = [yY] ]]; do
    read_msg "Done (y/n)?"
done
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441