Skip to content

Commit

Permalink
feat: add gum helper for choose dialogs (#173)
Browse files Browse the repository at this point in the history
* feat: add ugum helper for choose dialogs

* chore: make syntax identical to gum

* feat: add support for confirm and parse only relevant args

* chore: update spec file with ugum info

* chore: add ugum to rpmbuild SOURCES

* feat: attempt to use fzf as fallback before resorting to generic bash

* chore: change to use echo -e thanks to @gerblesh

* feat: add support for menu handlers through $MENU env variable

* chore: change wording for fzf  choose to be better
  • Loading branch information
HikariKnight authored Dec 21, 2023
1 parent 74b4acd commit a20eb9c
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 1 deletion.
1 change: 1 addition & 0 deletions build/ublue-os-just/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mkdir -p /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/*.just /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/ublue-os-just.sh /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/ujust /tmp/ublue-os/rpmbuild/SOURCES
cp ${SCRIPT_DIR}/ugum /tmp/ublue-os/rpmbuild/SOURCES

rpmbuild -ba \
--define '_topdir /tmp/ublue-os/rpmbuild' \
Expand Down
8 changes: 7 additions & 1 deletion build/ublue-os-just/ublue-os-just.spec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Name: ublue-os-just
Packager: ublue-os
Vendor: ublue-os
Version: 0.9
Version: 0.10
Release: 1%{?dist}
Summary: ublue-os just integration
License: MIT
Expand All @@ -20,6 +20,7 @@ Source6: 50-akmods.just
Source7: 60-custom.just
Source8: 70-nix.just
Source9: ujust
Source10: ugum

%global sub_name %{lua:t=string.gsub(rpm.expand("%{NAME}"), "^ublue%-os%-", ""); print(t)}

Expand All @@ -43,15 +44,20 @@ done
# Add global "ujust" script to run just with --unstable
mkdir -p -m0755 %{buildroot}%{_bindir}
install -Dm755 %{SOURCE9} %{buildroot}%{_bindir}/ujust
install -Dm755 %{SOURCE10} %{buildroot}%{_bindir}/ugum

%files
%dir %attr(0755,root,root) %{_datadir}/%{VENDOR}/%{sub_name}
%attr(0755,root,root) %{_sysconfdir}/profile.d/ublue-os-just.sh
%attr(0644,root,root) %{_datadir}/%{VENDOR}/%{sub_name}/*.just
%attr(0644,root,root) %{_datadir}/%{VENDOR}/justfile
%attr(0755,root,root) %{_bindir}/ujust
%attr(0755,root,root) %{_bindir}/ugum

%changelog
* Wed Dec 20 2023 HikariKnight <[email protected]> - 0.10
- Add ugum, a helper for user input for use in just

* Tue Nov 28 2023 RJ Trujillo <eyecantcu> - 0.9
- Copy nix justfile to correct location and restore ujust

Expand Down
178 changes: 178 additions & 0 deletions build/ublue-os-just/ugum
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#!/usr/bin/env bash
##################################################################
# This is a helper script to provide a basic fallback replacement
# for just commands and bash scripts that want to use gum in uBlue
##################################################################

# Supported menu handlers
SUPPORTED_HANDLERS=(
"fzf"
)

# Check if gum is present
GUM=$(which gum 2>/dev/null)

# Check if fzf is installed and set it as the handler
FALLBACK_HANDLER=$(which fzf 2>/dev/null)
HANDLER=""
if [[ ! -z $FALLBACK_HANDLER ]]; then
HANDLER="fzf"
fi

# If $MENU is set
if [[ ! -z $MENU ]]; then
for BIN in "${SUPPORTED_HANDLERS[@]}"
do
if [[ "$BIN" == "$MENU" ]]; then
HANDLER=$BIN
fi
done
fi

function noGum () {
if [[ -z "$1" ]]; then
# If no arguments are provided then error with
echo "ugum supports only choose or confirm as the first argument!"
exit 5
elif [[ "$1" == "choose" ]]; then
# If choose is the verb then run the choose function and pass all remaining args to an appropriate handler
if [[ "$HANDLER" == "fzf" ]]; then
# Use fzf for choice selector
choose_Fzf "${@:2}"
else
# Use generic bash selector
choose_Generic "${@:2}"
fi
elif [[ "$1" == "confirm" ]]; then
# If confirm is the verb then run the confirm function and pass all remaining args to an appropriate handler
if [[ "$HANDLER" == "fzf" ]]; then
# Use fzf as a confirm dialog
confirm_Fzf "${@:2}"
else
# Use a generic bash dialog
confirm_Generic "${@:2}"
fi
fi
}

function choose_Generic () {
# Change PS3 to our select prompt
PS3='Please enter your choice: '

# Make an array to contain all options in
OPTIONS=()

# Parse the arguments for the ones we support and care about
for arg in "$@"
do
# If the argument does not start with -
if [[ ! $arg =~ ^- ]]; then
OPTIONS+=("$arg")
fi
done

# Make a select prompt in bash
select opt in "${OPTIONS[@]}"
do
case $opt in
"")
# Invalid options print to STDERR and then loops back for the user to select again
echo "Invalid option $REPLY" >&2
;;
"$opt")
echo "$opt"
break
;;
esac
done
}

function choose_Fzf () {
# Change our select prompt
PROMPT='Please select your choice: '

# Make an array to contain all options in
local OPTIONS

# Parse the arguments for the ones we support and care about
for arg in "$@"
do
# If the argument does not start with -
if [[ ! $arg =~ ^- ]]; then
if [[ "$OPTIONS" == "" ]]; then
OPTIONS="${arg}"
continue
fi
OPTIONS="${OPTIONS}\n${arg}"
fi
done

# Make a select prompt using fzf
echo -e $OPTIONS | fzf --height="~20%" --prompt="$PROMPT"
}

function confirm_Generic () {
# Set default prompt
PROMPT="Are you sure?"

# Parse the arguments for the ones we support and care about
for arg in "$@"
do
if [[ ! $arg =~ ^- ]]; then
PROMPT="$arg"
fi
done

# Print the prompt and read input
read -r -p "$PROMPT [Y/n]: " YESNO
confirm_Parse "$YESNO"
}

function confirm_Fzf () {
PROMPT=$(confirm_getPrompt "$@")

# Make the confirm prompt using fzf and read response
YESNO=$(echo -e "Yes\nNo" | fzf --height="~20%" --prompt="$PROMPT ")
confirm_Parse "$YESNO"
}

function confirm_getPrompt () {
# Set default prompt
PROMPT="Are you sure?"

# Parse the arguments for the ones we support and care about
for arg in "$@"
do
if [[ ! $arg =~ ^- ]]; then
PROMPT="$arg"
fi
done

# Return the prompt
echo $PROMPT
}

function confirm_Parse () {
case "$@" in
[Yy]*)
# Use exit code 0 for yes, just like gum
exit 0
;;
[Nn]*)
# Use exit code 1 for no, just like gum
exit 1
;;
*)
# Default exit code is 0
exit 0
;;
esac
}

# If gum is not present
if [[ -z "$GUM" ]]; then
noGum "$@"
else
# If gum is present just pass args to gum
$GUM "$@"
fi

0 comments on commit a20eb9c

Please sign in to comment.