diff --git a/build/ublue-os-just/build.sh b/build/ublue-os-just/build.sh index 4de7ec57..1dab7224 100755 --- a/build/ublue-os-just/build.sh +++ b/build/ublue-os-just/build.sh @@ -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' \ diff --git a/build/ublue-os-just/ublue-os-just.spec b/build/ublue-os-just/ublue-os-just.spec index a195aa46..1078ec8d 100644 --- a/build/ublue-os-just/ublue-os-just.spec +++ b/build/ublue-os-just/ublue-os-just.spec @@ -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 @@ -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)} @@ -43,6 +44,7 @@ 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} @@ -50,8 +52,12 @@ install -Dm755 %{SOURCE9} %{buildroot}%{_bindir}/ujust %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 <2557889+HikariKnight@users.noreply.github.com> - 0.10 +- Add ugum, a helper for user input for use in just + * Tue Nov 28 2023 RJ Trujillo - 0.9 - Copy nix justfile to correct location and restore ujust diff --git a/build/ublue-os-just/ugum b/build/ublue-os-just/ugum new file mode 100755 index 00000000..6cf106a1 --- /dev/null +++ b/build/ublue-os-just/ugum @@ -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