How To: Remove grub theme during boot?

Here is the more or less finished script, unless errors are found, or somebody has a good suggestion:

#/bin/bash

etcdefaultgrubfile="/etc/default/grub"
grubconffile="/boot/grub/grub.cfg"

function cleanup() {
  rm -f -- "$tmpfile"
  printf '\e[?25h' # unhide cursor
  stty echo
}
trap cleanup EXIT

readonly tmpfile="$(mktemp default-grub-temp-XXXXXX)"

set -e # fail on first error

stty -echo
printf '\e[?25l' # hide cursor

CSI_fg=$'\e[38;2;'
CSI_bg=$'\e[48;2;'
esc_reset=$'\e[0m'

esc=(
  "0;0;0m"         # black
  "0;0;168m"       # blue
  "0;168;0m"       # green
  "0;168;168m"     # cyan
  "168;0;0m"       # red
  "168;0;168m"     # magenta
  "168;84;0m"      # brown
  "168;168;168m"   # light-gray
  "84;84;84m"      # dark-gray
  "84;84;254m"     # light-blue
  "84;254;84m"     # light-green
  "84;254;254m"    # light-cyan
  "254;84;84m"     # light-red
  "254;84;254m"    # light-magenta
  "254;254;84m"    # yellow
  "254;254;254m"   # white
)

# make black opaque for truecolor terminal emulators:
[[ $COLORTERM = "truecolor" ]] && esc[0]="1;1;1m"

cname=(
  "black"
  "blue"
  "green"
  "cyan"
  "red"
  "magenta"
  "brown"
  "light-gray"
  "dark-gray"
  "light-blue"
  "light-green"
  "light-cyan"
  "light-red"
  "light-magenta"
  "yellow"
  "white"
)

val=(0 9 1 11) #normal bg, normal fg, highlighted bg, highlighted fg
selected=0;

print_menu() {
  gb=$CSI_bg${esc[0]}$CSI_fg${esc[7]}
  no=$CSI_bg${esc[${val[0]}]}$CSI_fg${esc[${val[1]}]}
  hl=$CSI_bg${esc[${val[2]}]}$CSI_fg${esc[${val[3]}]}

  cat \
<< EOF
$gb          Colour Chooser for GRUB - PREVIEW           $esc_reset
$gb    $noβ”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”$gb    $esc_reset
$gb    $noβ”‚ Normal option                              β”‚$gb    $esc_reset
$gb    $noβ”‚ Normal option                              β”‚$gb    $esc_reset
$gb    $noβ”‚$hl Highlighted option                         $noβ”‚$gb    $esc_reset
$gb    $noβ”‚ Normal option                              β”‚$gb    $esc_reset
$gb    $noβ”‚                                            β”‚$gb    $esc_reset
$gb    $noβ”‚                                            β”‚$gb    $esc_reset
$gb    $noβ”‚                                            β”‚$gb    $esc_reset
$gb    $noβ””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜$gb    $esc_reset
$gb    Use the arrow keys to change the values below.    $esc_reset
$gb    Press ENTER when done.                            $esc_reset
$gb                                                      $esc_reset
EOF

  local SO
  local SC
  SO[$selected]='>'
  SC[$selected]='<'

  printf "%s      Normal background:      %-2s%-14s%s%2s    %s\n" \
    "$gb" "${SO[0]}" "${cname[${val[0]}]}" "$CSI_fg${esc[${val[0]}]}β–ˆβ–ˆ$gb" \
    "${SC[0]}" "$esc_reset"
  printf "%s      Normal foreground:      %-2s%-14s%s%2s    %s\n" \
    "$gb" "${SO[1]}" "${cname[${val[1]}]}" "$CSI_fg${esc[${val[1]}]}β–ˆβ–ˆ$gb" \
    "${SC[1]}" "$esc_reset"
  printf "%s      Highlighted background: %-2s%-14s%s%2s    %s\n" \
    "$gb" "${SO[2]}" "${cname[${val[2]}]}" "$CSI_fg${esc[${val[2]}]}β–ˆβ–ˆ$gb" \
    "${SC[2]}" "$esc_reset"
  printf "%s      Highlighted foreground: %-2s%-14s%s%2s    %s\n" \
    "$gb" "${SO[3]}" "${cname[${val[3]}]}" "$CSI_fg${esc[${val[3]}]}β–ˆβ–ˆ$gb" \
    "${SC[3]}" "$esc_reset"
}

move_cursor_top() {
  printf "\e[17A\r"
}


while true; do
  print_menu
  # wait for input
  read -rsn1 key
  [[ $key = $'' ]] && break
  [[ $key = $'\u1b' ]] && {
    read -rsn2 key
  }
  [[ -n $key ]] && {
    case "$key" in
      '[D'|h ) val[$selected]=$(((val[selected]+15)%16)) ;;
      '[B'|j ) selected=$((selected >= 3 ? 3 : selected+1)) ;;
      '[A'|k ) selected=$((selected <= 0 ? 0 : selected-1)) ;;
      '[C'|l ) val[$selected]=$(((val[selected]+1) % 16)) ;;
    esac
  }
  move_cursor_top
done

move_cursor_top
selected=5
print_menu

timestamp="$(date +'%Y-%m-%d %H:%M')"

sedscript="/^GRUB_COLOR_NORMAL=/i \# commented out by grub-colour-chooser, $timestamp:
s|^GRUB_COLOR_NORMAL=|\#GRUB_COLOR_NORMAL=|
/^GRUB_COLOR_HIGHLIGHT=/i \# commented out by grub-colour-chooser, $timestamp:
s|^GRUB_COLOR_HIGHLIGHT=|\#GRUB_COLOR_HIGHLIGHT=|
/^GRUB_THEME=/i \# commented out by grub-colour-chooser, $timestamp:
s|^GRUB_THEME=|\#GRUB_THEME=|"

sed "$sedscript" "$etcdefaultgrubfile" > "$tmpfile"
printf "\n#Added by grub-colour-chooser, %s:\nGRUB_COLOR_NORMAL=\"%s\"\nGRUB_COLOR_HIGHLIGHT=\"%s\"\n" \
  "$timestamp" \
  "${cname[${val[1]}]}/${cname[${val[0]}]}" \
  "${cname[${val[3]}]}/${cname[${val[2]}]}"  >> "$tmpfile"

printf "\nThe following changes to %s are proposed:\n\n" "$etcdefaultgrubfile"
diff --color=auto "$etcdefaultgrubfile" "$tmpfile" && true

printf '\e[?25h\n' # unhide cursor and print new line
stty echo
read -p "Apply these changes and run grub-mkconfig [y/N]? " response
if [[ $response =~ ^(yes|y|Y|Yes)$ ]]; then
  sudo cp -- "$tmpfile" "$etcdefaultgrubfile" || exit
  sudo grub-mkconfig -o "$grubconffile"
fi

Please test it thoroughly before adding it to the repo, and add a disclaimer. I take no responsibility for any damage it causes (it shouldn’t but I might have missed something).

6 Likes

Nice work! I can’t foresee any problems with it, and it should behave as spec’ed. Only one sad thing… even THIS won’t have me using grub again any time soon! :grin:

3 Likes

Well the way i have mine set up with rEFInd i still use grub so it would work for me i think. rEFInd loads and i select my desktop or distro and then grub loads normally.

So how does this work? Separate from the default grub on the system? How do you test it/use it? I’m bash illiterate here. Can you explain?

It’s super simple. The script just displays a fancy menu that lets you pick the colours for the grub menu. Then it comments out the relevant options in /etc/default/grub and adds the new options for colours. It’s nothing you couldn’t just as easily do by editing /etc/default/grub yourself. This is just a bit more interactive as it gives you a nice preview.

So i just save the script and run it? I’m not well versed with bash and scripts. :pleading_face:

Well, @manuel will add it to the endeavouros repo, I think (after it is sufficiently tested).

Until then, you can make a file named grub-colour-chooser and copy the script into it. Then make it executable with chmod u+x grub-colour-chooser. Then you can run it in a terminal emulator (with ./grub-colour-chooser) and the rest should be fairly obvious:

Screenshot_20210625_165823

It will not make any changes to your config unless you answer y when asked to apply the changes and then enter your sudo password (so you have two chances to change your mind). Also, unless I screwed up somewhere, all the edits to /etc/default/grub should be non-destructive. It never erases anything, just comments it out and adds a comment before it saying it was the script that did it, with a timestamp. So, it should be fairly easy to revert the changes. The script uses diff to tell you the exact changes it will make to your /etc/default/grub.

5 Likes

Do you save the file as grub-color-chooser.sh? Also when using chmod do you have to use the .sh in the file name?

No need for .sh. In an ideal world, extensions do not matter and are just part of the filename – they should not determine the file type.

Sure - if you choose. What you call it is irrelevant, except as a memory aid for its purpose. It is the chmod command that makes it executable (runnable) - no matter its name.

Comes up like this. Is this correct?

Screenshot_20210625_111536

Yes, it is working. :slight_smile:

1 Like

Thanks @Kresimir ! :+1:

I’ll test it and eventually add it as one of the scripts we are providing.
I may need to make some small modifications, is it OK for you?
(Note that we still blame you if it fails… :rofl:)

Just to make sure, I was joking.
(Or was I… :wink:)

3 Likes

Of course. You can do whatever you want with it. I’d appreciate you sharing these modifications with me so I can go over them before they are published, but if you don’t feel like doing that, that’s fine, too.

1 Like

OK, I’ll show you my (possible) modifications before releasing the corresponding package.
They will be visible at github, like all our software anyway.

And if you have a github account, you can even make pull requests when you notice something needs changing.

Nope, only gitlab.

If I have anything to add, I’ll ping you here :slight_smile:

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.

Here it is:
https://raw.githubusercontent.com/endeavouros-team/PKGBUILDS/master/eos-bash-shared/grub-colour-chooser

I’m releasing package eos-bash-shared soon.

3 Likes

So is this going to be part of the welcome app? What does the eos-bash-shared do when it’s installed?

1 Like