I've recently been reading some books about Ruby on Rails.
One little thing that I thought was cool was the example commands:
I decided to try to make my shell look the same.
Putting the directory name in the prompt is easy. That can be achieved by setting PS1='$(basename "$PWD")> '
(if you use zsh, you may need to run setopt promptsubst first)
Making everything you type after the prompt bold is the tricky bit. You have to enable bold mode at the end of the prompt, but disable it as soon as the user has pressed Enter, so the command's output isn't bold too.
This can be achieved in zsh by adding %{$(tput bold)%} to the end of your PS1 line, e.g.
and adding a function called preexec that resets the font to normal.
And to finish up, I like to handle things properly if tput and basename aren't available, so I test if tput is present using command -V tput and use POSIX-style ${parameter##word} to delete everything except the last part of the path.
So the stuff you need to add to ~/.zshrc now looks like this
And the end result looks like this

Unfortunately, you can't do this in bash because it doesn't have an equivalent to the preexec function, so everything including the command's output will be bold. (There is a patch to add something similar, but it causes bash to crash in some situations, so I think it's safer not to use it.)
One little thing that I thought was cool was the example commands:
dave> cd work
work> rails demo
create
create app/controllers
create app/helpers
create app/models
: : :
I decided to try to make my shell look the same.
Putting the directory name in the prompt is easy. That can be achieved by setting PS1='$(basename "$PWD")> '
(if you use zsh, you may need to run setopt promptsubst first)
Making everything you type after the prompt bold is the tricky bit. You have to enable bold mode at the end of the prompt, but disable it as soon as the user has pressed Enter, so the command's output isn't bold too.
This can be achieved in zsh by adding %{$(tput bold)%} to the end of your PS1 line, e.g.
PS1='$(basename "$PWD")> %{$(tput bold)%}'
and adding a function called preexec that resets the font to normal.
preexec()
{
tput rmso
}
And to finish up, I like to handle things properly if tput and basename aren't available, so I test if tput is present using command -V tput and use POSIX-style ${parameter##word} to delete everything except the last part of the path.
So the stuff you need to add to ~/.zshrc now looks like this
setopt promptsubst
init_terminal()
{
if command -V tput >/dev/null 2>&1; then
bold="$(tput bold)"
underline="$(tput smul)"
normal="$(tput sgr0)"
fi
}
last_part_of_path()
{
local full_path="$1"
local last_part="${full_path##*/}"
if test -n "$last_part"; then
echo "$last_part"
else
echo "/"
fi
}
preexec()
{
print -n "$normal"
}
init_terminal
PS1='$(last_part_of_path "$PWD")> %{$bold%}'
And the end result looks like this

Unfortunately, you can't do this in bash because it doesn't have an equivalent to the preexec function, so everything including the command's output will be bold. (There is a patch to add something similar, but it causes bash to crash in some situations, so I think it's safer not to use it.)
Post a Comment