Skip to content

Commit

Permalink
Color PS1s and improve the rows and columns detection in serial shells
Browse files Browse the repository at this point in the history
This overwrites the default debian bashrc with a custom bashrc that does
two things:

- Makes the prompt green or red depending on whether the previous
  command succeeded or failed, I think it's quite useful in general
- Before every command on the serial tty, uses a crazy escape sequence
  to query the host terminal for its dimensions and resize the guest
  terminal accordingly:
  https://blog.n-z.jp/blog/2022-01-30-serial-console-resize.html this
  makes ncurses apps like vim work fairly nicely even on the serial pane.
  Ideally, this should automatically run on host terminal resizes
  (SIGWINCH) but the series proposing this paravirtualization to QEMU
  never got reviewed and merged :(
  https://lists.endsoftwarepatents.org/archive/html/qemu-devel/2020-06/msg09591.html
  this should anyway be much better than the current situation where the
  guest always assumes an 80x24 screen
  • Loading branch information
FlorentRevest committed Feb 21, 2024
1 parent bdf36c8 commit c1a6e1a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tasks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ mkdir /run/sshd/
setsid /sbin/getty -l /bin/bash -n 115200 ${SERIAL_TTY}
EOF
chmod +x ${img_bind_mnt}/sbin/init-minimal
cat << EOF > ${img_bind_mnt}/etc/bash.bashrc
# Use a green or red prompt depending on the previous command's exit value
__prompt () {
if [[ $? = "0" ]]; then
PS1='\[\033[01;32m\]'
else
PS1='\[\033[01;31m\]'
fi
PS1="$PS1\u@\h:\w\$ \[\033[00m\]"
}
PROMPT_COMMAND=__prompt
# On the serial tty, ask the host terminal for dimensions before each command
__resize () {
local escape r c
IFS='[;' read -t 1 -sd R -p "$(printf '\e7\e[r\e[999;999H\e[6n\e8')" escape r c
if [[ "$r" -gt 0 && "$c" -gt 0 ]]; then
stty cols $c rows $r
fi
}
if [[ $TERM = "vt102" ]]; then
trap __resize DEBUG
fi
EOF

# Atomically make the rootfs file available to unblock wait-for-vm tasks
sync
Expand Down

0 comments on commit c1a6e1a

Please sign in to comment.