A status line in forty lines
Every few years I install a prompt framework, admire it for a week and remove it when the shell takes half a second to start. This time I wrote down what I actually read in a prompt. It was three things.
What a prompt has to say
- Where I am, shortened to the last two directories.
- Whether the last command failed, as its exit code.
- The branch, when there is one, and nothing when there is none.
That is one call to pwd, one read of $? and one small file under .git. No process has to start.
The whole of it
lantern() {
code=$?
dir=${PWD#"${PWD%/*/*}/"}
[ $code -ne 0 ] && err=" [$code]" || err=""
printf '%s%s $ ' "$dir" "$err"
}
The remaining thirty lines read the branch and choose colours. They are in the repository.