You are not logged in.

#1 2010-06-29 07:59:52

synthead
Member
Registered: 2006-05-09
Posts: 1,337

read: color prompt?

How do I get a read prompt in color?  Escape sequences seem to fail here.

read -p "Type something: "

Offline

#2 2010-06-29 08:03:18

synthead
Member
Registered: 2006-05-09
Posts: 1,337

Re: read: color prompt?

Oh, I can't use echo beforehand here because I'm trying to get a command history going.  When I send up/down keys, it erases the prompt.

#!/bin/bash

HISTORYFILE=".buildpkgs_history"
BUILDPKGEXEC="./buildpkg"

ext () {
 history -w $HISTORYFILE
 exit
}

trap ext SIGINT
history -r $HISTORYFILE

for (( ; ; ))
do
 #echo -ne '\E[36;40m'"==> "; tput sgr0; echo -n "Which package to build? "
 read -ep "Which package to build? " pkgtobuild; history -s $pkgtobuild
 $BUILDPKGEXEC $pkgtobuild
done

Offline

#3 2010-06-29 16:33:56

bcat
Member
From: New York, NY, USA
Registered: 2009-01-02
Posts: 30
Website

Re: read: color prompt?

I'm away from a shell right now, but I think this might work.

#!/bin/bash

HISTORYFILE=".buildpkgs_history"
BUILDPKGEXEC="./buildpkg"

ext () {
 history -w $HISTORYFILE
 exit
}

trap ext SIGINT
history -r $HISTORYFILE

prompt=$(tput setaf 6; echo -n '==> '; tput sgr0; echo -n 'Which package to build? ')

for (( ; ; ))
do
 read -ep "$prompt" pkgtobuild; history -s $pkgtobuild
 $BUILDPKGEXEC $pkgtobuild
done

Basically, just cache your prompt and the necessary escape sequences in a variable, then pass that to the read builtin.

Last edited by bcat (2010-06-29 16:37:38)


Running Arch on a Dell Studio 1735. xmonad FTW! Dotfiles here.
Want free cloud-based file sharing? Sign up for Dropbox and we both get some bonus storage!

Offline

#4 2010-06-30 04:07:26

evil
Member
From: Indianapolis, IN
Registered: 2010-03-06
Posts: 41
Website

Re: read: color prompt?

bcat wrote:

I'm away from a shell right now, but I think this might work.

#!/bin/bash

HISTORYFILE=".buildpkgs_history"
BUILDPKGEXEC="./buildpkg"

ext () {
 history -w $HISTORYFILE
 exit
}

trap ext SIGINT
history -r $HISTORYFILE

prompt=$(tput setaf 6; echo -n '==> '; tput sgr0; echo -n 'Which package to build? ')

for (( ; ; ))
do
 read -ep "$prompt" pkgtobuild; history -s $pkgtobuild
 $BUILDPKGEXEC $pkgtobuild
done

Basically, just cache your prompt and the necessary escape sequences in a variable, then pass that to the read builtin.

I tested it and it works. smile


Site | Blog | Freenode Nick: i686

Offline

#5 2010-06-30 17:45:01

bcat
Member
From: New York, NY, USA
Registered: 2009-01-02
Posts: 30
Website

Re: read: color prompt?

Cool!


Running Arch on a Dell Studio 1735. xmonad FTW! Dotfiles here.
Want free cloud-based file sharing? Sign up for Dropbox and we both get some bonus storage!

Offline

#6 2010-06-30 23:53:06

synthead
Member
Registered: 2006-05-09
Posts: 1,337

Re: read: color prompt?

Yeeeah!  Thanks!

Offline

Board footer

Powered by FluxBB