Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
feat: improve prompt integration with quadlets (ublue-os#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
m2Giles authored Feb 11, 2024
1 parent c3251a9 commit 267e5be
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 26 deletions.
28 changes: 26 additions & 2 deletions usr/libexec/enable-bluefin-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# shellcheck disable=2154
source /usr/lib/ujust/ujust.sh

bluefin_cli=(${red}Disabled${n} ${red}Inactive${n})
bluefin_cli=(${red}Disabled${n} ${red}Inactive${n} ${red}Not Default${n})

function get_status(){
if systemctl --quiet --user is-enabled bluefin-cli.target; then
Expand All @@ -20,8 +20,29 @@ function get_status(){
else
bluefin_cli[1]="${red}Inactive${n}"
fi
echo "Bluefin-cli is currently ${b}${bluefin_cli[0]}${n} and ${b}${bluefin_cli[1]}${n}"
get_default=$(dconf read /org/gnome/Prompt/Profiles/default-profile-uuid)
if test "$get_default" = "'a21a910811504857bea4c96b3d937b93'"; then
bluefin_cli[2]="${green}Default${n}"
else
bluefin_cli[2]="${red}Not-Default${n}"
fi
echo "Bluefin-cli is currently ${b}${bluefin_cli[0]}${n} (run status), ${b}${bluefin_cli[1]}${n} (on boot status), and ${b}${bluefin_cli[2]}${n} (terminal profile)."
}

function default_login(){
toggle=$(Choose Default Not-Default Cancel)
if test "$toggle" = "Default"; then
echo "Setting Bluefin-CLI to default Prompt Profile"
/usr/libexec/prompt-create-profile bluefin-cli default
elif test "$toggle" = "Not-Default"; then
echo "Setting Host back to default Prompt Profile"
/usr/libexec/prompt-create-profile Host default
else
dconf write /or
echo "Not Changing"
fi
}

function logic(){
if test "$toggle" = "Enable"; then
echo "${b}${green}Enabling${n} Bluefin-CLI"
Expand All @@ -31,6 +52,7 @@ function logic(){
echo "${b}${green}Starting${n} Bluefin-CLI"
systemctl --user start bluefin-cli.service
fi
default_login
elif test "$toggle" = "Disable"; then
echo "${b}${red}Disabling${n} Bluefin-CLI"
systemctl --user disable --now bluefin-cli.target > /dev/null 2>&1
Expand All @@ -42,6 +64,8 @@ function logic(){
systemctl --user reset-failed bluefin-cli.service > /dev/null 2>&1 || true
fi
fi
echo "Setting Host back to default Prompt Profile"
/usr/libexec/prompt-create-profile Host default
else
echo "Not Changing"
fi
Expand Down
7 changes: 5 additions & 2 deletions usr/libexec/prompt-add-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Read the current value of the array
CURRENT_VALUE=$(dconf read /org/gnome/Prompt/profile-uuids)
guid=$1
guid="$1"

# remove the leading and trailing brackets
CURRENT_VALUE=${CURRENT_VALUE:1:-1}
Expand All @@ -13,6 +13,9 @@ CURRENT_VALUE=${CURRENT_VALUE// /}
# split the string into an array
IFS=',' read -r -a array <<<"$CURRENT_VALUE"

# Exit if the guid already is in the array
[[ $CURRENT_VALUE =~ $guid ]] && exit 0

# add the new value
array+=("'$guid'")

Expand All @@ -26,4 +29,4 @@ UPDATED_VALUE=${UPDATED_VALUE%?}
UPDATED_VALUE="[$UPDATED_VALUE]"

# Write the updated array back to dconf
dconf write /org/gnome/Prompt/profile-uuids "$UPDATED_VALUE"
dconf write /org/gnome/Prompt/profile-uuids "$UPDATED_VALUE"
61 changes: 54 additions & 7 deletions usr/libexec/prompt-create-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,75 @@

# if dconf doesn't exist, just return
if ! command -v dconf >/dev/null; then
return
exit 0
fi

# shellcheck disable=SC1091
. /usr/share/ublue-os/bluefin-cli/known-containers

# shellcheck disable=SC2001
gen_uuid() {
uuid="$(cat /proc/sys/kernel/random/uuid)"
echo "$uuid" | sed 's/-//g'
}

guid=$(gen_uuid)
name="$1"
palette="$2"
default="$2"
palette="$3"

for check in "${!known_container[@]}"; do
if test "$check" = "$name"; then
guid=${known_container[$check]}
fi
done

if test -z "$guid"; then
guid=$(gen_uuid)
fi

default_guid=$(dconf read /org/gnome/Prompt/default-profile-uuid)
default_guid=${default_guid:1:-1}

# If default profile is trying to be made, just exit
if test "$guid" = "$default_guid"; then
exit 0
fi

if test -z "$default"; then
make_default=0
elif test "$default" = "default" || test "$default" -eq 1; then
make_default=1
fi

# Write the default value if specified
if test "$make_default" -eq 1; then
dconf write /org/gnome/Prompt/default-profile-uuid "'${guid}'"
fi

profile="/org/gnome/Prompt/Profiles/${guid}/"
opacity=$(dconf read /org/gnome/Prompt/Profiles/"${default_guid}"/opacity)

if test "$name" = "Host"; then
dconf write "${profile}label" "'${name}'"
else
dconf write "${profile}custom-command" "'sh -c \"[ ! -e /run/.containerenv ] && exec distrobox enter ${name} || ${SHELL}\"'"
dconf write "${profile}label" "'${name}'"
dconf write "${profile}use-custom-command" "true"
dconf write "${profile}ublue-os" "true"
fi

if test -n "$opacity"; then
dconf write "${profile}opacity" "'${opacity}'"
fi

dconf write "${profile}custom-command" "'sh -c \"[ ! -e /run/.containerenv ] && [ ! -e /run/.dockerenv ] && distrobox enter ${name} || ${SHELL}\"'"
dconf write "${profile}label" "'${name}'"
dconf write "${profile}use-custom-command" "true"
if test -n "$palette"; then
dconf write "${profile}palette" "'${palette}'"
elif test "$name" = "bluefin-cli"; then
elif test "$name" = "bluefin-cli" || test "$name" = "bluefin-dx-cli"; then
dconf write "${profile}palette" "'catppuccin-dynamic'"
elif test "$name" = "fedora-toolbox"; then
dconf write "${profile}palette" "'Elio'"
elif test "$name" = "ubuntu-toolbox"; then
dconf write "${profile}palette" "'Clone Of Ubuntu'"
fi

/usr/libexec/prompt-add-profile.sh "$guid"
43 changes: 28 additions & 15 deletions usr/libexec/prompt-remove-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ if ! command -v dconf >/dev/null; then
return
fi

# Cleanup any stale profiles
for i in $(dconf list /org/gnome/Prompt/Profiles/); do
i=${i:0:-1}
[[ $(dconf read /org/gnome/Prompt/profile-uuids) =~ $i ]] || dconf reset -f "/org/gnome/Prompt/Profiles/${i}/"
done

name="$1"

# Read the current value of the array
Expand All @@ -20,33 +26,40 @@ CURRENT_VALUE=${CURRENT_VALUE// /}
# split the string into an array
IFS=',' read -r -a array <<<"$CURRENT_VALUE"

# Get Default
DEFAULT_VALUE=$(dconf read /org/gnome/Prompt/default-profile-uuid)

# loop through the array and remove any that don't exist
for i in "${!array[@]}"; do
guid=${array[i]}

# remove single quotes from guid

guid=${guid//\'/}

#echo "Checking profile for $(red $guid)"
profile="/org/gnome/Prompt/Profiles/${guid}/"

custom_shell=$(dconf read "${profile}custom-command")
ublue_os=$(dconf read "${profile}ublue-os")
label=$(dconf read "${profile}label")
label=${label:1:-1}

if [[ $custom_shell == *"[ ! -e /run/.containerenv ] && [ ! -e /run/.dockerenv ] && distrobox enter ${name}"* ]]; then
dconf reset -f "${profile}"
# remove the guid from the array
unset 'array[i]'
# join the array back into a string
UPDATED_VALUE=$(printf "%s," "${array[@]}")
if test "$ublue_os" = "true"; then
# Don't delete the profile if it's the default or if it's enabled
if ! test "$DEFAULT_VALUE" = "$guid" && test "$name" = "$label" && ! systemctl --user --quiet is-enabled "${name}".target; then
dconf reset -f "${profile}"
# remove the guid from the array
unset 'array[i]'
# join the array back into a string
UPDATED_VALUE=$(printf "%s," "${array[@]}")

# remove the trailing comma
UPDATED_VALUE=${UPDATED_VALUE%?}
# remove the trailing comma
UPDATED_VALUE=${UPDATED_VALUE%?}

# add the leading and trailing brackets
UPDATED_VALUE="[$UPDATED_VALUE]"
# add the leading and trailing brackets
UPDATED_VALUE="[$UPDATED_VALUE]"

# Write the updated array back to dconf
dconf write /org/gnome/Prompt/profile-uuids "$UPDATED_VALUE"
# Write the updated array back to dconf
dconf write /org/gnome/Prompt/profile-uuids "$UPDATED_VALUE"
fi
fi
done
done
19 changes: 19 additions & 0 deletions usr/share/ublue-os/bluefin-cli/known-containers
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# shellcheck shell=bash
declare -Ar known_container=(
["bluefin-cli"]="a21a910811504857bea4c96b3d937b93"
["bluefin-dx-cli"]="276d25e6065b497b96dd57e4f95ff50a"
["fedora-toolbox"]="d6fe45489ed74fada5d95d715449ce7e"
["ubuntu-toolbox"]="4741cb2eb3614750b79edc5c4b8c08b3"
["wolfi-toolbox"]="33bd8e9953224e6985d8524e9b7294a1"
["wolfi-dx-toolbox"]="03d42eab38f740089cbe2fa5e2d6a374"
["Host"]="2871e8027773ae74d6c87a5f659bbc74"
)
declare -Ar known_guid=(
["a21a910811504857bea4c96b3d937b93"]="bluefin-cli"
["276d25e6065b497b96dd57e4f95ff50a"]="bluefin-dx-cli"
["d6fe45489ed74fada5d95d715449ce7e"]="fedora-toolbox"
["4741cb2eb3614750b79edc5c4b8c08b3"]="ubuntu-toolbox"
["33bd8e9953224e6985d8524e9b7294a1"]="wolfi-toolbox"
["03d42eab38f740089cbe2fa5e2d6a374"]="wolfi-dx-toolbox"
["2871e8027773ae74d6c87a5f659bbc74"]="Host"
)

0 comments on commit 267e5be

Please sign in to comment.