Skip to content

Commit

Permalink
add color to basic text interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lunarcloud committed Jun 11, 2024
1 parent a6379f5 commit 474d3ad
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
74 changes: 49 additions & 25 deletions script-dialog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,41 @@ if [ -z ${GUI+x} ]; then
fi
fi

if [ "$GUI" == "true" ] ; then
if command -v >/dev/null kdialog; then
hasKDialog=true
fi
if command -v >/dev/null kdialog; then
hasKDialog=true
fi

if command -v >/dev/null zenity; then
hasZenity=true
fi
else
if command -v >/dev/null dialog; then
hasDialog=true
fi
if command -v >/dev/null zenity; then
hasZenity=true
fi

if command -v >/dev/null whiptail; then
hasWhiptail=true
fi
if command -v >/dev/null dialog; then
hasDialog=true
fi

if command -v >/dev/null whiptail; then
hasWhiptail=true
fi

# see if it supports colors...
ncolors=$(tput colors)
if test -n "$ncolors" && test "$ncolors" -ge 8; then
bold="$(tput bold)"
underline="$(tput smul)"
#standout="$(tput smso)"
normal="$(tput sgr0)"
red="$(tput setaf 1)"
#green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
#blue="$(tput setaf 4)"
#magenta="$(tput setaf 5)"
#cyan="$(tput setaf 6)"
else
bold=""
underline=""
normal=""
red=""
yellow=""
fi


Expand Down Expand Up @@ -262,13 +281,17 @@ function message-info() {
function message-warn() {
GUI_ICON=$XDG_ICO_WARN
KDIALOG_ARG=--sorry
echo -n "${yellow}"
messagebox "$@"
echo -n "${normal}"
}

function message-error() {
GUI_ICON=$XDG_ICO_ERROR
KDIALOG_ARG=--error
echo -n "${red}"
messagebox "$@"
echo -n "${normal}"
}

function messagebox() {
Expand Down Expand Up @@ -316,7 +339,7 @@ function yesno() {
kdialog --title "$GUI_TITLE" --icon "$GUI_ICON" --yesno "$1"
answer=$?
else
echo "$1 (y/n)" 3>&1 1>&2 2>&3
echo -n "${bold}$1 (y/n): ${normal}" 3>&1 1>&2 2>&3
read -r answer
if [ "$answer" == "y" ]; then
answer=0
Expand Down Expand Up @@ -345,7 +368,7 @@ function inputbox() {
elif [ "$INTERFACE" == "kdialog" ]; then
INPUT="$(kdialog --title "$GUI_TITLE" --icon "$GUI_ICON" --inputbox "$1" "$2")"
else
read -ei "$2" -rp "$1: " INPUT
read -ei "$2" -rp "${bold}$1: ${normal}" INPUT
fi

echo "$INPUT"
Expand Down Expand Up @@ -381,8 +404,8 @@ function userandpassword() {
CREDS[0]=$(inputbox "$USER_TEXT" "$SUGGESTED_USERNAME")
CREDS[1]=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --password "$PASS_TEXT")
else
read -ei "$SUGGESTED_USERNAME" -rp "$USER_TEXT: " "CREDS[0]"
read -srp "$PASS_TEXT: " "CREDS[1]"
read -ei "$SUGGESTED_USERNAME" -rp "${bold}$USER_TEXT: ${normal}" "CREDS[0]"
read -srp "${bold}$PASS_TEXT: ${normal}" "CREDS[1]"
fi

eval "$__uservar"="'${CREDS[0]}'"
Expand All @@ -406,7 +429,7 @@ function password() {
elif [ "$INTERFACE" == "kdialog" ]; then
PASSWORD=$(kdialog --title="$GUI_TITLE" --icon "$GUI_ICON" --password "$1")
else
read -srp "$ACTIVITY: " PASSWORD
read -srp "${bold}$ACTIVITY: ${normal}" PASSWORD
fi
echo "$PASSWORD"
}
Expand Down Expand Up @@ -530,12 +553,12 @@ function radiolist() {
echo "$ACTIVITY: " 3>&1 1>&2 2>&3
OPTIONS=()
while test ${#} -gt 0; do
OPTIONS+=("\t$1 ($2)\n")
OPTIONS+=("\t- ${underline}$1${normal} ($2)\n")
shift
shift
shift
done
read -rp "$(echo -e "${OPTIONS[*]}$TEXT: ")" CHOSEN
read -rp "$(echo -e "${OPTIONS[*]}${bold}$TEXT: ${normal}")" CHOSEN
fi

echo "$CHOSEN"
Expand Down Expand Up @@ -707,9 +730,10 @@ function datepicker() {
GUI_ICON=$XDG_ICO_CALENDAR
fi
updateGUITitle
DAY="0"
MONTH="0"
YEAR="0"

DAY=$( printf '%(%d)T' )
MONTH=$( printf '%(%m)T' )
YEAR=$( printf '%(%Yd)T' )

if [ "$INTERFACE" == "whiptail" ]; then
STANDARD_DATE=$(inputbox "Input Date (DD/MM/YYYY)" " ")
Expand Down Expand Up @@ -754,7 +778,7 @@ function datepicker() {
YEAR=$(echo "$INPUT_DATE" | cut -d' ' -f4)
STANDARD_DATE="$DAY/$MONTH/$YEAR"
else
read -rp "Date (DD/MM/YYYY): " STANDARD_DATE
read -ei "$( printf '%(%d/%m/%Y)T' )" -rp "${bold}Date (DD/MM/YYYY): ${normal}" STANDARD_DATE
fi

echo "$STANDARD_DATE"
Expand Down
9 changes: 3 additions & 6 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ source "${SCRIPT_DIR}"/script-dialog.sh
relaunchIfNotVisible

APP_NAME="Test Script"
#INTERFACE="unknown" #force an interface, but only do this for testing

ACTIVITY="Salutations"
message-info "Hello $desktop desktop user.\nUsing the ${INTERFACE-basic} interface for dialogs";
Expand Down Expand Up @@ -46,9 +45,6 @@ userandpassword S_USER S_PASS "$SUGGESTED_USERNAME"

message-info $"So, that was:\n user: $S_USER\n password: $S_PASS"

ACTIVITY="Test Script"
displayFile "$0"

ACTIVITY="Enter Birthday"
ANSWER=$(datepicker)

Expand Down Expand Up @@ -76,6 +72,9 @@ ANSWER=$(filepicker "$HOME" "open")

message-info "File selected was ${ANSWER[*]}"

ACTIVITY="Test Script"
displayFile "$0"

ANSWER=$(folderpicker "$HOME")

message-info "Folder selected was ${ANSWER[*]}"
Expand All @@ -91,5 +90,3 @@ else
message-error "Password denied"
fi
fi

exit 0;

0 comments on commit 474d3ad

Please sign in to comment.