Skip to content

Commit

Permalink
theme/essential: SC2154
Browse files Browse the repository at this point in the history
Handle all unbound parameters, even colors!
  • Loading branch information
gaelicWizard committed Sep 19, 2021
1 parent 7e04c8f commit af0f1ff
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions themes/essential/essential.theme.bash
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
#!/usr/bin/env bash

# https://github.com/koalaman/shellcheck/wiki/Sc2154
# shellcheck disable=SC2154

function _user-prompt() {
local -r user='\\u'

if [[ "${EUID}" -eq 0 ]]; then
# Privileged users:
local -r user_color="${bold_red}"
local -r user_color="${bold_red?}"
else
# Standard users:
local -r user_color="${bold_green}"
local -r user_color="${bold_green?}"
fi

# Print the current user's name (colored according to their current EUID):
echo -e "${user_color}${user}${normal}"
echo -e "${user_color}${user}${normal?}"
}

function _host-prompt() {
local -r host='\\h'

# Check whether or not $SSH_TTY is set:
if [[ -z "${SSH_TTY}" ]]; then
if [[ -z "${SSH_TTY:-}" ]]; then
# For local hosts, set the host's prompt color to blue:
local -r host_color="${bold_blue}"
local -r host_color="${bold_blue?}"
else
# For remote hosts, set the host's prompt color to red:
local -r host_color="${bold_red}"
local -r host_color="${bold_red?}"
fi

# Print the current hostname (colored according to $SSH_TTY's status):
echo -e "${host_color}${host}${normal}"
echo -e "${host_color}${host}${normal?}"
}

function _user-at-host-prompt() {
# Concatenate the user and host prompts into: user@host:
echo -e "$(_user-prompt)${bold_white}@$(_host-prompt)"
echo -e "$(_user-prompt)${bold_white?}@$(_host-prompt)"
}

function _exit-status-prompt() {
Expand All @@ -47,32 +46,32 @@ function _exit-status-prompt() {
if [[ "${exit_status}" -eq 0 ]]; then
# For commands that return an exit status of zero, set the exit status's
# notifier to green:
local -r exit_status_color="${bold_green}"
local -r exit_status_color="${bold_green?}"
else
# For commands that return a non-zero exit status, set the exit status's
# notifier to red:
local -r exit_status_color="${bold_red}"
local -r exit_status_color="${bold_red?}"
fi

echo -ne "${exit_status_color}"
if [[ "${prompt_string}" -eq 1 ]]; then
# $PS1:
echo -e " +${normal} "
echo -e " +${normal?} "
elif [[ "${prompt_string}" -eq 2 ]]; then
# $PS2:
echo -e " |${normal} "
echo -e " |${normal?} "
else
# Default:
echo -e " ?${normal} "
echo -e " ?${normal?} "
fi
}

function _ps1() {
local -r time='\\t'

echo -ne "${bold_white}${time} "
echo -ne "${bold_white?}${time} "
echo -ne "$(_user-at-host-prompt)"
echo -e "${bold_white}:${normal}${PWD}"
echo -e "${bold_white?}:${normal?}${PWD}"
echo -e "$(_exit-status-prompt 1 "${exit_status}")"
}

Expand Down

0 comments on commit af0f1ff

Please sign in to comment.