Skip to content

Commit

Permalink
feat: add ublue-bling for bling-cli support (#99)
Browse files Browse the repository at this point in the history
* feat: add ublue-bling for bling-cli support

This should add support for `bluefin-cli` and `bazzite-cli` or whatever else someone else would like to do with just a single configuration line change

* fix: formatting on ublue bling

* chore(ublue, bling): use shared directory for bling scripts by default
  • Loading branch information
tulilirockz authored Jan 1, 2025
1 parent 4abc02d commit 43aaf05
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ublue/bling/src/bling.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env fish

# FIXME: add no-source-twice fix

# ls aliases
if [ "$(command -v eza)" ]
alias ll='eza -l --icons=auto --group-directories-first'
alias l.='eza -d .*'
alias ls='eza'
alias l1='eza -1'
end

# ugrep for grep
if [ "$(command -v ug)" ]
alias grep='ug'
alias egrep='ug -E'
alias fgrep='ug -F'
alias xzgrep='ug -z'
alias xzegrep='ug -zE'
alias xzfgrep='ug -zF'
end

if status is-interactive
[ "$(command -v atuin)" ] && eval "$(atuin init fish)"
[ "$(command -v zoxide)" ] && eval "$(zoxide init fish)"
end
35 changes: 35 additions & 0 deletions ublue/bling/src/bling.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env sh

# Check if bling has already been sourced so that we dont break atuin. https://github.com/atuinsh/atuin/issues/380#issuecomment-1594014644
[ "${BLING_SOURCED:-0}" -eq 1 ] && return
BLING_SOURCED=1

# ls aliases
if [ "$(command -v eza)" ]; then
alias ll='eza -l --icons=auto --group-directories-first'
alias l.='eza -d .*'
alias ls='eza'
alias l1='eza -1'
fi

# ugrep for grep
if [ "$(command -v ug)" ]; then
alias grep='ug'
alias egrep='ug -E'
alias fgrep='ug -F'
alias xzgrep='ug -z'
alias xzegrep='ug -zE'
alias xzfgrep='ug -zF'
fi

HOMEBREW_PREFIX="${HOMEBREW_PREFIX:-/home/linuxbrew/.linuxbrew}"

if [ "$(basename "$SHELL")" = "bash" ]; then
#shellcheck disable=SC1091
[ -f "${HOMEBREW_PREFIX}"/etc/profile.d/bash-preexec.sh ] && . "${HOMEBREW_PREFIX}"/etc/profile.d/bash-preexec.sh
[ "$(command -v atuin)" ] && eval "$(atuin init bash)"
[ "$(command -v zoxide)" ] && eval "$(zoxide init bash)"
elif [ "$(basename "$SHELL")" = "zsh" ]; then
[ "$(command -v atuin)" ] && eval "$(atuin init zsh)"
[ "$(command -v zoxide)" ] && eval "$(zoxide init zsh)"
fi
208 changes: 208 additions & 0 deletions ublue/bling/src/ublue-bling
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
#!/usr/bin/env bash

set -eou pipefail

# coloring / ugum chooser
source /usr/lib/ujust/ujust.sh

get_config() {
MOTD_CONFIG_FILE="${MOTD_CONFIG_FILE:-/etc/ublue-os/bling.json}"
QUERY="$1"
FALLBACK="$2"
shift
shift
OUTPUT="$(jq -r "$QUERY" "$MOTD_CONFIG_FILE" 2>/dev/null || echo "$FALLBACK")"
if [ "$OUTPUT" == "null" ] ; then
echo "$FALLBACK"
return
fi
echo "$OUTPUT"
}


# Exit Handling
function Exiting() {
printf "%s%sExiting...%s\n" "${red}" "${bold}" "${normal}"
printf "Rerun script with %s%sujust $BLING_CLI_NAME%s\n" "${blue}" "${bold}" "${normal}"
exit 0
}

# Trap function
function ctrl_c() {
printf "\n%s\n" "Signal SIGINT caught"
Exiting
}

# Brew Bundle Install
function brew-bundle() {
echo 'Installing bling from Homebrew 🍻🍻🍻'
brew bundle --file "$BLING_BREW_BUNDLE_PATH" --no-lock
}

# Check if bling is already sourced
# 0 for yes
# 1 for no
function is-bling-installed() {
shell="$1"
shift

line=""
BLING_SCRIPT_SOURCE="bling.sh"
TARGET_CONFIG_FILE="${HOME}/.bashrc"
case "${shell}" in
"fish")
BLING_SCRIPT_SOURCE="bling.fish"
TARGET_CONFIG_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish"
;;
"zsh")
BLING_SCRIPT_SOURCE="bling.sh"
TARGET_CONFIG_FILE="${ZDOTDIR:-$HOME}/.zshrc"
;;
"bash")
BLING_SCRIPT_SOURCE="bling.sh"
TARGET_CONFIG_FILE="${HOME}/.bashrc"
;;
*)
echo 'Unknown shell. You are on your own.'
exit 1
;;
esac
# Prevents grep erroring out when theres no config file for the shell
touch "$TARGET_CONFIG_FILE"
line="$(grep -n "source $BLING_CLI_DIRECTORY/$BLING_SCRIPT_SOURCE" "$TARGET_CONFIG_FILE")"
line="$(grep -Eo '^[^:]+' <<< "$line")"

if [[ -n "${line}" ]]; then
return 0
fi
return 1
}

# Add Bling
function add-bling() {
shell="$1"
shift
brew-bundle || Exiting

echo 'Setting up your Shell 🐚🐚🐚'
case "${shell}" in
"fish")
echo 'Adding bling to your config.fish 🐟🐟🐟'
cat<<EOF >> "${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish"
### bling.fish source start
test -f $BLING_CLI_DIRECTORY/bling.fish && source $BLING_CLI_DIRECTORY/bling.fish
### bling.fish source end
EOF
;;
"zsh")
echo 'Adding bling to your .zshrc 💤💤💤'
cat<<EOF >> "${ZDOTDIR:-$HOME}/.zshrc"
### bling.sh source start
test -f $BLING_CLI_DIRECTORY/bling.sh && source $BLING_CLI_DIRECTORY/bling.sh
### bling.sh source end
EOF
;;
"bash")
echo 'Adding bling to your .bashrc 💥💥💥'
cat<<EOF >> "${HOME}/.bashrc"
### bling.sh source start
test -f $BLING_CLI_DIRECTORY/bling.sh && source $BLING_CLI_DIRECTORY/bling.sh
### bling.sh source end
EOF
;;
*)
echo 'Unknown shell. You are on your own.'
exit 1
;;
esac
}

# Remove bling, handle if old method
function remove-bling() {
shell="$1"
shift

case "${shell}" in
"fish")
sed -i '/### bling.fish source start/,/### bling.fish source end/d' \
"${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish" \
|| \
line=$(grep -n "source $BLING_CLI_DIRECTORY/bling.fish" \
"${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish" \
| grep -Eo '^[^:]+') \
&& \
sed -i "${line}"d "${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish"
;;
"zsh")
sed -i '/### bling.sh source start/,/### bling.sh source end/d' \
"${ZDOTDIR:-$HOME}/.zshrc" \
|| \
line=$(grep -n "source $BLING_CLI_DIRECTORY/bling.sh" \
"${ZDOTDIR:-$HOME}/.zshrc" \
| grep -Eo '^[^:]+') && sed -i "${line}"d \
"${ZDOTDIR:-$HOME}/.zshrc"
;;
"bash")
sed -i '/### bling.sh source start/,/### bling.sh source end/d' \
"${HOME}/.bashrc" \
|| \
line=$(grep -n "source $BLING_CLI_DIRECTORY/bling.sh" \
"${HOME}/.bashrc" \
| grep -Eo '^[^:]+') && sed -i "${line}"d \
"${HOME}/.bashrc"
;;
esac
}

function main() {
# Get Shell
shell=$(basename "$SHELL")
reentry="$1"
clear
if [[ -n "${reentry:-}" ]]; then
printf "%s%s%s\n\n" "${bold}" "$reentry" "$normal"
fi

# Check if bling is enabled and display
printf "Shell:\t%s%s%s%s\n" "${green}" "${bold}" "${shell}" "${normal}"
if is-bling-installed "${shell}"; then
printf "Bling:\t%s%sEnabled%s\n" "${green}" "${bold}" "${normal}"
else
printf "Bling:\t%s%sDisabled%s\n" "${red}" "${bold}" "${normal}"
fi

# ugum enable/disable
CHOICE=$(Choose enable disable cancel)

# Enable/Disable. Recurse if bad option.
case "${CHOICE,,}" in
"enable")
if is-bling-installed "${shell}"; then
main "Bling is already configured ..."
fi
trap ctrl_c SIGINT
add-bling "${shell}"
printf "%s%sInstallation Complete%s ... please close and reopen your terminal!" "${green}" "${bold}" "${normal}"
exit 0
;;
"disable")
if ! is-bling-installed "${shell}"; then
main "Bling is not yet configured ..."
fi
trap ctrl_c SIGINT
remove-bling "${shell}"
printf "%s%sBling Removed%s ... please close and reopen your terminal\n" "${red}" "${bold}" "${normal}"
exit 0
;;
*)
Exiting
;;
esac
}

BLING_CLI_NAME="$(get_config '."bling-cli-name"' "bluefin-cli")"
BLING_CLI_DIRECTORY="$(get_config '."bling-cli-path"' "/usr/share/ublue-os/bling")"
BLING_BREW_BUNDLE_PATH="$(get_config '."bling-brew-bundle-path"' "/usr/share/ublue-os/homebrew/$BLING_CLI_NAME.Brewfile")"

# Entrypoint
main ""
34 changes: 34 additions & 0 deletions ublue/bling/ublue-bling.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
%global debug_package %{nil}

Name: ublue-bling
Version: 0.1.0
Release: 1%{?dist}
Summary: Universal Blue Bling CLI setup scripts

License: Apache-2.0
URL: https://github.com/ublue-os/packages
VCS: {{{ git_dir_vcs }}}
Source: {{{ git_dir_pack }}}

# Requires: ublue-os-just but we cant add it here because it is sourced from ublue-os/config now.
# It would be interesting to move it here but we need some discussion
Requires: gum

%description
Universal Blue Bling CLI setup scripts

%prep
{{{ git_dir_setup_macro }}}

%install
install -Dm0755 ./src/%{name} %{buildroot}%{_libexecdir}/ublue-motd
# Intented to either be symlinked into the correct directories or be used directly
install -Dm0755 ./src/bling.sh %{buildroot}%{_datadir}/ublue-os/bling/bling.sh
install -Dm0755 ./src/bling.fish %{buildroot}%{_datadir}/ublue-os/bling/bling.fish

%files
%{_libexecdir}/ublue-motd
%{_datadir}/ublue-os/bling/bling.*sh

%changelog
%autochangelog

0 comments on commit 43aaf05

Please sign in to comment.