This commit is contained in:
Alexander Navarro 2023-02-21 18:22:19 -03:00
parent 4524a86376
commit 0552ffdae8
37 changed files with 8770 additions and 49 deletions

View file

@ -3,8 +3,6 @@
set -Eeuo pipefail
shopt -s nullglob
check-dependencies megatools rclone rsync fd
source "$DOTS/config/zsh/config/colors.zsh" && define_colors
LOCAL_BACKUP_PATH="$HOME/Drives/Backups/auto-backups/"
@ -17,6 +15,7 @@ DATE_FORMAT="+%F"
SOURCES=(
"$HOME/Drives/Stuff/Pictures/"
"$HOME/Drives/Stuff/Music/"
"root@berry.net:/home/aleidk/services/"
)
MAX_BACKUPS=10
@ -56,7 +55,7 @@ backup() {
# shellcheck disable=2086
rsync \
--super \
--fake-super \
--compress \
--mkpath \
--archive \
@ -77,7 +76,7 @@ backup() {
# clean failed backups
for dir in $dst/.partial_*; do
date="$(echo "$dir" | cut -d "_" -f 2)"
rm -r "$dir" "${dst}/${date}"
rm -r "$dir" "${dst:?}/${date}"
done
echo -e ""
}
@ -106,7 +105,7 @@ delete_old() {
}
sync() {
config="$(rclone config dump 2>/dev/null | jq '."$RCLONE_CLOUD_NAME" // empty')"}
config="$(rclone config dump 2>/dev/null | jq ".$RCLONE_CLOUD_NAME // empty")"
if [[ -z "$config" ]]; then
echo -e "${RED}${SHL}No Rclone configuration! skiping sync.${RST}${EHL}\n"
@ -114,14 +113,14 @@ sync() {
fi
for dir in $LOCAL_BACKUP_PATH/*; do
name="$(basename "$dir")"
remote_path="${RCLONE_CLOUD_NAME}:${REMOTE_BACKUP_PATH}/${name}"
newest="$(fd -t d --exact-depth 1 . "$dir" | sort -r | head -n 1)"
name="$(basename "$dir")"
today=$(date "$DATE_FORMAT")
if [[ "$today" == "$(rclone cat "${remote_path}/.last-sync" 2>/dev/null)" ]]; then
echo -e "${MGN}${DIM}Last sync for this backup was today, skiping...${RST}"
return
echo -e "${MGN}${DIM}Last sync ${BLU}for${RST} $name was today, skiping...${RST}"
continue
else
echo -e "Syncthing latest backup for ${BLU}$name${RST} in ${RED}$remote_path${RST}"
fi
@ -143,7 +142,8 @@ ensure_path_exist "$LOCAL_BACKUP_PATH"
for path in "${SOURCES[@]}"; do
backup "$path"
delete_old "$path"
sync
done
sync
echo -e "${GRN}Backups done!${RST}"

90
scripts/fzf-dnf Executable file
View file

@ -0,0 +1,90 @@
#!/usr/bin/bash
readonly basename="$(basename "$0")"
if ! hash fzf &> /dev/null; then
printf 'Error: Missing dep: fzf is required to use %s.\n' "${basename}" >&2
exit 64
fi
#Colors
declare -r esc=$'\033'
declare -r BLUE="${esc}[1m${esc}[34m"
declare -r RED="${esc}[31m"
declare -r GREEN="${esc}[32m"
declare -r YELLOW="${esc}[33m"
declare -r CYAN="${esc}[36m"
# Base commands
readonly QRY="dnf --cacheonly --quiet repoquery "
readonly PRVW="dnf --cacheonly --quiet --color=always info"
readonly QRY_PRFX=' '
readonly QRY_SFFX=' > '
# Install mode
readonly INS_QRYS="${QRY} --qf '${CYAN}%{name}'"
readonly INS_PRVW="${PRVW}"
readonly INS_PRMPT="${CYAN}${QRY_PRFX}Install packages${QRY_SFFX}"
# Remove mode
readonly RMV_QRYS="${QRY} --installed --qf '${RED}%{name}'"
readonly RMV_PRVW="${PRVW} --installed"
readonly RMV_PRMPT="${RED}${QRY_PRFX}Remove packages${QRY_SFFX}"
# Remove-userinstalled mode
readonly RUI_QRYS="${QRY} --userinstalled --qf '${YELLOW}%{name}'"
readonly RUI_PRVW="${PRVW} --installed"
readonly RUI_PRMPT="${YELLOW}${QRY_PRFX}Remove User-Installed${QRY_SFFX}"
# Updates mode
readonly UPD_QRY="${QRY} --upgrades --qf '${GREEN}%{name}'"
readonly UPD_QRYS="if [[ $(${UPD_QRY} | wc -c) -ne 0 ]]; then ${UPD_QRY}; else echo ${GREEN}No updates available.; echo Try refreshing metadata cache...; fi"
readonly UPD_PRVW="${PRVW}"
readonly UPD_PRMPT="${GREEN}${QRY_PRFX}Upgrade packages${QRY_SFFX}"
mapfile -d '' fhelp <<-EOF
"${basename}"
Interactive package manager for Fedora
Alt-i Install mode (default)
Alt-r Remove mode
Alt-e Remove User-Installed mode
Alt-u Updates mode
Alt-m Update package metadata cache
Enter Confirm selection
Tab Mark package ()
Shift-Tab Unmark package
Ctrl-a Select all
? Help (this page)
ESC Quit
EOF
declare tmp_file
if tmp_file="$(mktemp --tmpdir "${basename}".XXXXXX)"; then
printf 'in' > "${tmp_file}" &&
SHELL='/bin/bash' \
FZF_DEFAULT_COMMAND="${INS_QRYS}" \
fzf \
--ansi \
--multi \
--query=$* \
--header=" ${basename} | Press Alt+? for help or ESC to quit" \
--header-first \
--prompt="${INS_PRMPT}" \
--marker=' ' \
--preview-window='right,67%,wrap' \
--preview="${INS_PRVW} {1}" \
--bind="enter:execute(if grep -q 'in' \"${tmp_file}\"; then sudo dnf install {+};
elif grep -q 'rm' \"${tmp_file}\"; then sudo dnf remove {+}; \
elif grep -q 'up' \"${tmp_file}\"; then sudo dnf upgrade {+}; fi; \
read -s -r -n1 -p $'\n${BLUE}Press any key to continue...' && printf '\n')" \
--bind="alt-i:unbind(alt-i)+reload(${INS_QRYS})+change-preview(${INS_PRVW} {1})+change-prompt(${INS_PRMPT})+execute-silent(printf 'in' > \"${tmp_file}\")+first+rebind(alt-r,alt-e,alt-u)" \
--bind="alt-r:unbind(alt-r)+reload(${RMV_QRYS})+change-preview(${RMV_PRVW} {1})+change-prompt(${RMV_PRMPT})+execute-silent(printf 'rm' > \"${tmp_file}\")+first+rebind(alt-i,alt-e,alt-u)" \
--bind="alt-e:unbind(alt-e)+reload(${RUI_QRYS})+change-preview(${RUI_PRVW} {1})+change-prompt(${RUI_PRMPT})+execute-silent(printf 'rm' > \"${tmp_file}\")+first+rebind(alt-i,alt-r,alt-u)" \
--bind="alt-u:unbind(alt-u)+reload(${UPD_QRYS})+change-preview(${UPD_PRVW} {1})+change-prompt(${UPD_PRMPT})+execute-silent(printf 'up' > \"${tmp_file}\")+first+rebind(alt-i,alt-r,alt-e)" \
--bind="alt-m:execute(sudo dnf makecache;read -s -r -n1 -p $'\n${BLUE}Press any key to continue...' && printf '\n')" \
--bind="alt-?:preview(printf \"${fhelp[0]}\")" \
--bind="ctrl-a:select-all"
rm -f "${tmp_file}" &> /dev/null
else
printf 'Error: Failed to create tmp file. $TMPDIR (or /tmp if $TMPDIR is unset) may not be writable.\n' >&2
exit 65
fi

175
scripts/fzf-flatpak Executable file
View file

@ -0,0 +1,175 @@
#!/usr/bin/env sh
# CLR=$(for i in {0..7}; do echo "tput setaf $i"; done)
BLK=\$(tput setaf 0); RED=\$(tput setaf 1); GRN=\$(tput setaf 2); YLW=\$(tput setaf 3); BLU=\$(tput setaf 4);
MGN=\$(tput setaf 5); CYN=\$(tput setaf 6); WHT=\$(tput setaf 7); BLD=\$(tput bold); RST=\$(tput sgr0);
AWK_COLOR_VAR=" -v BLK=${BLK} -v RED=${RED} -v GRN=${GRN} -v YLW=${YLW} -v BLU=${BLU} -v MGN=${MGN} -v CYN=${CYN} -v WHT=${WHT} -v BLD=${BLD} -v RST=${RST}"
FZF_FLATPAK_HELP="$(
cat <<-EOF
${BLU}${BLD}Fzf-Flatpak.sh
${BLU}${BLD}M-f M-i ${RST}${CYN}Install apps (flathub repo)
${BLU}${BLD}M-f M-u ${RST}${CYN}Uninstall apps
${BLU}${BLD}M-f M-r ${RST}${CYN}Run apps
${BLU}${BLD}M-? ${RST}${CYN}Help (this page)
${BLU}${BLD}ESC ${RST}${CYN}Exit
EOF
)"
if [[ $- =~ i ]]; then
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
_fzf_flatpak_fzf() {
fzf-tmux -p85% -- \
--tiebreak=begin \
-m --ansi --nth=1.. \
--color='header:italic:underline' \
--color='fg+:blue,border:blue' \
--layout=reverse --height=50% --border \
--preview-window "nohidden,50%,<50(down,60%,border-rounded)" \
--bind "del:execute(flatpak remove --unused > /dev/tty; read -r)" \
--bind="alt-?:preview(printf \"${FZF_FLATPAK_HELP}\")" "$@"
}
_fzf_flatpak_check() {
which flatpak > /dev/null 2>&1 && return
[[ -n $TMUX ]] && tmux display-message "Flatpak Isn't Installed"
return 1
}
__fzf_flatpak=${BASH_SOURCE[0]:-${(%):-%x}}
__fzf_flatpak=$(readlink -f "$__fzf_flatpak" 2> /dev/null || /usr/bin/ruby --disable-gems -e 'puts File.expand_path(ARGV.first)' "$__fzf_flatpak" 2> /dev/null)
_fzf_flatpak_install() {
_fzf_flatpak_check || return
flatpak remote-ls flathub --cached --columns=app,name,description 2>/dev/null \
| awk -v cyn=$(tput setaf 6) -v blu=$(tput setaf 4) -v bld=$(tput bold) -v res=$(tput sgr0) \
'{
app_info="";
for(i=3;i<=NF;i++){
app_info=app_info" "$i
}; print blu bld $2" -" res cyn app_info "|" $1}' \
| column -t -s "|" -R 3 \
| _fzf_flatpak_fzf \
--prompt=" Install > " \
--header=$'M-u: Update / Del: Remove Unused \n\n' \
--preview "flatpak --system remote-info flathub {-1} | awk $AWK_COLOR_VAR -F\":\" '{print YLW BLD \$1 RST MGN \$2}'" \
--bind="alt-u:execute(flatpak update > /dev/tty; read -r)" \
--bind="alt-m:change-preview(flatpak metadata {-1})" \
--bind "enter:execute(flatpak install flathub {+-1} > /dev/tty)+clear-screen" "$@"
}
# _fzf_flatpak_remotes?() {
# flatpak remotes --columns=name | tail -n +1
# }
# _fzf_flatpak_format_installed_lists() {
# local color1 color2 bold reset
# color1="$1"
# color2="$2"
# bold="$(tput bold)"
# reset="$(tput sgr0)"
# awk -v c1="$color1" -v c2="$color2" -v bld="$bold" -v res="$reset" \
# '{
# app_id="";
# for(i=2;i<=NF;i++){
# app_id=app_id" "$i
# }; print bld c1 app_id " && - " res c2 $1}' \
# | column -t -s "&&"
# }
_fzf_flatpak_installed_lists() {
awk -v cyn=$(tput setaf 6) -v blu=$(tput setaf 4) -v bld=$(tput bold) -v res=$(tput sgr0) \
'{
app_id="";
for(i=2;i<=NF;i++){
app_id=app_id" "$i
}; print bld cyn app_id " && - " res blu $1}' \
| column -t -s "&&"
}
_fzf_flatpak_uninstall_lists() {
awk -v mgn=$(tput setaf 5) -v red=$(tput setaf 1) -v bld=$(tput bold) -v res=$(tput sgr0) \
'{
app_id="";
for(i=2;i<=NF;i++){
app_id=app_id" "$i
}; print bld mgn app_id " && - " res red $1}' \
| column -t -s "&&"
}
_fzf_flatpak_installed_lists-applications() {
flatpak list --app --columns=application,name | _fzf_flatpak_installed_lists
}
_fzf_flatpak_uninstall_lists_with_runtimes() {
flatpak list --columns=application,name | _fzf_flatpak_uninstall_lists
}
# `flatpak run` only accepts one argument so it isn't possible to run multiple apps
_fzf_flatpak_fzf_installed_lists() {
_fzf_flatpak_fzf \
--header=$'M-u: Uninstall / Del: Remove Unused / F4: Kill / M-r: Run\n\n' \
--bind "f4:execute(flatpak kill {+-1})" \
--bind "alt-r:change-prompt( Run > )+execute-silent(touch /tmp/run && rm -r /tmp/uns)" \
--bind "alt-u:change-prompt( Uninstall > )+execute-silent(touch /tmp/uns && rm -r /tmp/run)" \
--bind "enter:execute(
if [ -f /tmp/uns ]; then
flatpak uninstall {+-1} > /dev/tty;
elif [ -f /tmp/run ]; then
flatpak run {-1} > /dev/null;
fi
)" "$@"
rm -f /tmp/{uns,run} &> /dev/null
}
_fzf_flatpak_uninstall() {
_fzf_flatpak_check || return
touch /tmp/uns
_fzf_flatpak_uninstall_lists_with_runtimes | _fzf_flatpak_fzf_installed_lists \
--prompt=" Uninstall > " \
--preview "flatpak info {-1} | awk $AWK_COLOR_VAR -F\":\" '{print RED BLD \$1 RST MGN \$2}'" \
}
_fzf_flatpak_run_apps() {
_fzf_flatpak_check || return
touch /tmp/run
_fzf_flatpak_installed_lists-applications | _fzf_flatpak_fzf_installed_lists \
--prompt=" Run > " \
--preview "flatpak info {-1} | awk $AWK_COLOR_VAR -F\":\" '{print CYN BLD \$1 RST BLU \$2}'" \
}
if [[ -n "${BASH_VERSION:-}" ]]; then
__fzf_flatpak_init() {
bind '"\er": redraw-current-line'
local o
for o in "$@"; do
bind '"\M-f\M-'${o:0:1}'": "`_fzf_flatpak_'$o'`\e\M-e\er"'
bind '"\M-f'${o:0:1}'": "`_fzf_flatpak_'$o'`\e\M-e\er"'
done
}
elif [[ -n "${ZSH_VERSION:-}" ]]; then
__fzf_flatpak_join() {
local item
while read item; do
echo -n "${(q)item} "
done
}
__fzf_flatpak_init() {
local o
zle
for o in "$@"; do
eval "fzf-flatpak-$o-widget() { local result=\$(_fzf_flatpak_$o | __fzf_flatpak_join); zle && { zle reset-prompt; zle -R }; LBUFFER+=\$result }"
eval "zle -N fzf-flatpak-$o-widget"
eval "bindkey '^[f^[${o[1]}' fzf-flatpak-$o-widget"
eval "bindkey '^[f[${o[1]}' fzf-flatpak-$o-widget"
done
}
fi
__fzf_flatpak_init install uninstall run_apps
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------
fi

View file

@ -8,8 +8,40 @@ WALLPAPER_PREFIX="Fondos_PC.*"
RANDOM_PICTURE=$(fd -apt f "$WALLPAPER_PREFIX" "$WALLPAPER_DIR" | shuf -n 1)
CURRENT_DESKTOP="$(echo "$XDG_CURRENT_DESKTOP" | awk '{for (i=1;i<=NF;i++) { $i=toupper(substr($i,1,1)) tolower(substr($i,2)) }}1')"
# Extra actions
declare -A CONFIG
CONFIG["delete_current"]=false
show_help() {
echo -e "${BLD}Usage: ${GRN}set-random-wallpaper${RST}${YLW} [OPTION]${RST}"
echo -e ""
echo -e "${BLD} Options:${RST}"
echo -e "${YLW} -d, delete ${RST}delete current wallpaper"
echo -e "${YLW} -h, --help ${RST}Show this help"
}
case "$1" in
delete | -d)
CONFIG["delete_current"]=true
;;
help | -h)
show_help
;;
*) ;;
esac
shift
case "$CURRENT_DESKTOP" in
Gnome)
if [[ "${CONFIG[delete_current]}" == true ]]; then
file_path="$(gsettings get org.gnome.desktop.background picture-uri)"
file_path="${file_path:8:-1}"
rm -iv "$file_path"
fi
gsettings set org.gnome.desktop.background picture-uri "file://$RANDOM_PICTURE"
gsettings set org.gnome.desktop.background picture-uri-dark "file://$RANDOM_PICTURE"
;;
@ -32,7 +64,7 @@ KDE)
d.currentConfigGroup = Array("Wallpaper","org.kde.image","General");
d.writeConfig("Image", "file:///'$RANDOM_PICTURE'")
}}
'
'
;;
Sway)
MODES=(