Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gum helper for choose dialogs #173

Merged
merged 9 commits into from
Dec 21, 2023
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
91 changes: 91 additions & 0 deletions build/ublue-os-just/ugum
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/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
##################################################################

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

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 it
choose "${@:2}"
elif [[ "$1" == "confirm" ]]; then
confirm "${@:2}"
fi
}

function choose () {
# 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 confirm () {
# 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
case "${YESNO}" 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