💾 Archived View for adamthiede.com › log › 2023-01-28.gmi captured on 2023-11-14 at 07:56:52. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-07-10)

➡️ Next capture (2023-12-28)

-=-=-=-=-=-=-

Fun with my bash prompt

Over Christmas I had to use Windows, and customizing the powershell prompt was necessary - it's basically just '\w >', so you get your prompt at varying screen widths and I can't deal with that. I typically have used something like `PS1='\u@\h \w \n\$ '`, with some additions here and there, but that's the basic structure. I like a newline before my $. But anyway, because of Windows, I tried adding way more features to my Linux prompt, just because.

Here's what it looks like now.

1 ⬢ 07:34 [~/repos/gemlog] master
$

That's with all the features showing up. The '1' is the non-zero exit code of the last command. If it's 0 it's not there. The ⬢ is for if I'm in a distrobox. The next bit is just the path, and the last word is the git branch. Then of course a new line and $.

Without the extra bits it looks like this.

07:34 [~/repos/gemlog]
$

I tried to do background colors but that got super distracting really quick. Here's the code.

function exitcode {
	ec=$?
	# 130=128+2; bash ctrl+c exit code
	if [ $ec -eq 0 -o $ec -eq 130 ];then
		printf ""
	else
		printf "\e[1;31m$ec\e[0m "
	fi
}
function gitbranchprompt {
	gb=$(git branch --show-current 2>/dev/null)
	if [ "$gb" = '' ];then
		printf ''
	else
		printf "\e[0m $gb"
	fi
}
function inadistrobox {
	if [ -n "$DISTROBOX_ENTER_PATH" ];then
		printf "\e[33;35m⬢\e[0m "
	fi
}
PS1='$(exitcode)$(inadistrobox)\e[1m\A \e[0m[\w]$(gitbranchprompt)\e[0m\n\$ '

(I'm aware git includes its own prompt thing and I shouldn't have to do gitbranchprompt, but alpine doesn't have it.)

back to gemlog