p.sh rewrite

This commit is contained in:
2024-04-23 11:23:02 +01:00
parent 40afd833d4
commit 96f875ac88
3 changed files with 211 additions and 163 deletions

View File

@@ -490,7 +490,7 @@
programs.bat = { programs.bat = {
enable = true; enable = true;
extraPackages = with pkgs.bat-extras; [ batdiff batman batgrep ]; extraPackages = with pkgs.bat-extras; [ batdiff batman ];
}; };
programs.git = { programs.git = {
@@ -507,7 +507,6 @@
ls = "eza --icons"; ls = "eza --icons";
ll = "eza -lhiM --git --total-size --icons --tree"; ll = "eza -lhiM --git --total-size --icons --tree";
tree = "eza --tree"; tree = "eza --tree";
grep = "batgrep";
code = "codium"; code = "codium";
cat = "bat"; cat = "bat";
man = "batman"; man = "batman";

17
scripts/oneline.sh Normal file
View File

@@ -0,0 +1,17 @@
oneline() {
# Print a command's output as a single line only.
# Example usage: for f in 'first line' 'second line' '3rd line'; do echo "$f"; sleep 1; done | oneline
local ws
while IFS= read -r line; do
if ((${#line} >= $COLUMNS)); then
# Moving cursor back to the front of the line so user input doesn't force wrapping
printf '\r%s\r' "${line:0:$COLUMNS}"
else
ws=$(($COLUMNS - ${#line}))
# by writing each line twice, we move the cursor back to position
# thus: LF, content, whitespace, LF, content
printf '\r%s%*s\r%s' "$line" "$ws" " " "$line"
fi
done
echo
}

View File

@@ -16,198 +16,230 @@ UNDERLINE="\e[4m"
ENDCOLOR="\e[0m" ENDCOLOR="\e[0m"
detectDistro() {
if [ "$(uname -s)" = "Darwin" ]; then
echo "macOS"
else
if [ "$(grep -Ei 'debian|buntu|mint' /etc/*release)" ]; then
echo "Debian"
elif [ "$(grep -Ei 'arch|manjaro|artix' /etc/*release)" ]; then
echo "Arch"
else
echo 1
return 1
fi
fi
}
oneline() {
# Print a command's output as a single line only.
# Example usage: for f in 'first line' 'second line' '3rd line'; do echo "$f"; sleep 1; done | oneline
local ws
while IFS= read -r line; do
if ((${#line} >= $COLUMNS)); then
# Moving cursor back to the front of the line so user input doesn't force wrapping
printf '\r%s\r' "${line:0:$COLUMNS}"
else
ws=$(($COLUMNS - ${#line}))
# by writing each line twice, we move the cursor back to position
# thus: LF, content, whitespace, LF, content
printf '\r%s%*s\r%s' "$line" "$ws" " " "$line"
fi
done
echo
}
p() ( p() (
distro=$(detectDistro) packageManagers=()
update() { if [ "$(command -v flatpak)" ]; then
if [[ $distro == "Arch" ]]; then packageManagers+=("flatpak")
~/dotfiles/Linux/scripts/arch-maintenance.sh fi
elif [[ $distro == "Debian" ]]; then if [ "$(command -v nix)" ]; then
~/dotfiles/Linux/scripts/debian-maintenance.sh packageManagers+=("nix")
elif [[ $distro == "macOS" ]]; then fi
~/dotfiles/macOS/scripts/macos-maintenance.sh if [ "$(command -v brew)" ]; then
else packageManagers+=("brew")
echo -e "${RED}[E] System not supported.${ENDCOLOR}" fi
return 1 if [ "$(command -v apt)" ]; then
packageManagers+=("apt")
elif [ "$(command -v pacman)" ]; then
packageManagers+=("pacman")
elif [ "$(command -v dnf)" ]; then
packageManagers+=("dnf")
fi
updateP() {
if [[ ${packageManagers[@]} =~ "flatpak" ]]; then
flatpak update
flatpak uninstall --unused --delete-data
fi
if [[ ${packageManagers[@]} =~ "nix" ]]; then
nix-channel --update
sudo nix-channel --update
nix-collect-garbage --delete-older-than 7d
fi
if [[ ${packageManagers[@]} =~ "brew" ]]; then
brew update
brew doctor
brew upgrade
fi
if [[ ${packageManagers[@]} =~ "apt" ]]; then
sudo apt update
sudo apt upgrade
sudo apt dist-upgrade
sudo apt autoremove
sudo apt autoclean
elif [[ ${packageManagers[@]} =~ "pacman" ]]; then
sudo sed -i 's/^#ParallelDownloads/ParallelDownloads/' /etc/pacman.conf
sudo pacman -Syu
sudo pacman -Rsn $(pacman -Qdtq)
if [ ! "$(command -v reflector)" ]; then
sudo pacman -Sy --noconfirm reflector rsync curl
fi
iso=$(curl -4 ifconfig.co/country-iso)
extra="FR"
sudo reflector -a 48 -c $iso -c $extra -f 5 -l 30 --verbose --sort rate --save /etc/pacman.d/mirrorlist
if [ ! "$(command -v paccache)" ]; then
sudo pacman -Sy --noconfirm pacman-contrib
fi
paccache -rk1
elif [[ ${packageManagers[@]} =~ "dnf" ]]; then
sudo dnf upgrade --refresh
sudo dnf autoremove
fi fi
} }
check() { checkP() {
nix_apps=$(nix-env -q)
if [[ $distro == "Debian" ]]; then
distro_apps=$(dpkg-query -l | grep '^ii' | awk '{print $2}')
elif [[ $distro == "Arch" ]]; then
distro_apps=$(pacman -Q)
else
distro_apps=""
fi
app_name=$(echo "$1" | tr '[:upper:]' '[:lower:]') app_name=$(echo "$1" | tr '[:upper:]' '[:lower:]')
app_name=$(echo "$app_name" | tr " " -) app_name=$(echo "$app_name" | tr " " -)
echo $nix_apps | grep -wq $app_name if [[ ${packageManagers[@]} =~ "flatpak" ]]; then
nix_success=$? flatpak_apps=$(flatpak list --columns=application)
if [[ $nix_success == 0 ]]; then echo $flatpak_apps | grep -iq $app_name
echo -e "${GREEN}Nix: $(echo $nix_apps | tr ' ' '\n' | grep -w $app_name)${ENDCOLOR}" flatpak_success=$?
if [[ $flatpak_success == 0 ]]; then
echo -e "${GREEN}${BOLD}Flatpak:${ENDCOLOR}${GREEN} $(echo $flatpak_apps | tr ' ' '\n' | grep -i $app_name)${ENDCOLOR}"
fi
fi
if [[ ${packageManagers[@]} =~ "nix" ]]; then
nix_apps=$(nix-env -q)
echo $nix_apps | grep -iq $app_name
nix_success=$?
if [[ $nix_success == 0 ]]; then
echo -e "${GREEN}${BOLD}Nix:${ENDCOLOR}${GREEN} $(echo $nix_apps | tr ' ' '\n' | grep -i $app_name)${ENDCOLOR}"
fi
fi
if [[ ${packageManagers[@]} =~ "brew" ]]; then
brew_apps=$(brew list)
echo $brew_apps | grep -iq $app_name
brew_success=$?
if [[ $brew_success == 0 ]]; then
echo -e "${GREEN}${BOLD}Brew:${ENDCOLOR}${GREEN} $(echo $brew_apps | tr ' ' '\n' | grep -i $app_name)${ENDCOLOR}"
fi
fi
if [[ ${packageManagers[@]} =~ "apt" ]]; then
distro_apps=$(dpkg-query -l | grep '^ii' | awk '{print $2}')
elif [[ ${packageManagers[@]} =~ "pacman" ]]; then
distro_apps=$(pacman -Q)
elif [[ ${packageManagers[@]} =~ "dnf" ]]; then
distro_apps=$(dnf list)
fi fi
echo $distro_apps | grep -Eq "(^|\s)$app_name($|\s)" echo $distro_apps | grep -Eiq "(^|\s)$app_name($|\s)"
distro_success=$? distro_success=$?
if [[ $distro_success == 0 ]]; then if [[ $distro_success == 0 ]]; then
echo -e "${GREEN}$distro: $(echo $distro_apps | tr ' ' '\n' | grep -E "(^|\s)$app_name($|\s)")${ENDCOLOR}" echo -e "${GREEN}${BOLD}Distro:${ENDCOLOR}${GREEN} $(echo $distro_apps | tr ' ' '\n' | grep -Ei "(^|\s)$app_name($|\s)")${ENDCOLOR}"
fi fi
if [[ $nix_success == 0 && $distro_success == 0 ]]; then if [[ $flatpak_success != 0 && $nix_success != 0 && $brew_success != 0 && $distro_success != 0 ]]; then
return 2 #both
elif [[ $nix_success == 0 ]]; then
return 3 #nix
elif [[ $distro_success == 0 ]]; then
return 4 #distro
else
echo -e "${YELLOW}$app_name not installed.${ENDCOLOR}" echo -e "${YELLOW}$app_name not installed.${ENDCOLOR}"
return 1 return 1
fi fi
# Get a list of all apps -> see if it's installed and print what installed it, and version.
} }
if [[ $distro == "Debian" ]]; then installP() {
install="sudo apt-get install" checkP $1
remove="sudo apt-get remove" if [[ $? != 1 ]]; then
search="apt-cache search" echo "$1 is already installed."
elif [[ $distro == "Arch" ]]; then return 0
if pacman -Qs paru >/dev/null; then
install="paru -S"
remove="paru -R"
search="paru -Si"
else
install="sudo pacman -S"
remove="sudo pacman -R"
search="pacman -Ss"
fi fi
fi if [[ ${packageManagers[@]} =~ "flatpak" ]]; then
echo "Attempting flatpak install..."
flatpak install $1
if [[ $? == 0 ]]; then
return 0
fi
fi
if [[ ${packageManagers[@]} =~ "nix" ]]; then
echo "Attempting nix install..."
nix-env -iA nixpkgs.$1
if [[ $? == 0 ]]; then
return 0
fi
fi
if [[ ${packageManagers[@]} =~ "brew" ]]; then
echo "Attempting brew install..."
brew install $1
if [[ $? == 0 ]]; then
return 0
fi
fi
if [[ ${packageManagers[@]} =~ "apt" ]]; then
echo "Attempting apt install..."
sudo apt install $1
if [[ $? == 0 ]]; then
return 0
fi
elif [[ ${packageManagers[@]} =~ "pacman" ]]; then
echo "Attempting pacman install..."
sudo pacman -Sy $1
if [[ $? == 0 ]]; then
return 0
fi
elif [[ ${packageManagers[@]} =~ "dnf" ]]; then
echo "Attempting dnf install..."
sudo dnf install $1
if [[ $? == 0 ]]; then
return 0
fi
fi
echo "ERROR - $1 not found."
return 1
}
# Install Nix. removeP() {
if ! nix --version &>/dev/null; then checkP $1
echo -e "${YELLOW}[E] Nix not found.${ENDCOLOR}" if [[ $? != 0 ]]; then
echo -e "${GREEN}[+] Installing the Nix package manager...${ENDCOLOR}" echo "$1 is not installed."
curl -L https://nixos.org/nix/install | sh return 0
. $HOME/.nix-profile/etc/profile.d/nix.sh fi
echo -e "${GREEN}[I] Installed Nix.${ENDCOLOR}" if [[ ${packageManagers[@]} =~ "flatpak" ]]; then
fi echo "Attempting flatpak uninstall..."
flatpak uninstall $1
if [[ $? == 0 ]]; then
return 0
fi
fi
if [[ ${packageManagers[@]} =~ "nix" ]]; then
echo "Attempting nix uninstall..."
nix-env --uninstall $1
if [[ $? == 0 ]]; then
return 0
fi
fi
if [[ ${packageManagers[@]} =~ "brew" ]]; then
echo "Attempting brew uninstall..."
brew uninstall $1
if [[ $? == 0 ]]; then
return 0
fi
fi
if [[ ${packageManagers[@]} =~ "apt" ]]; then
echo "Attempting apt uninstall..."
sudo apt remove $1
if [[ $? == 0 ]]; then
return 0
fi
elif [[ ${packageManagers[@]} =~ "pacman" ]]; then
echo "Attempting pacman uninstall..."
sudo pacman -Rs $1
if [[ $? == 0 ]]; then
return 0
fi
elif [[ ${packageManagers[@]} =~ "dnf" ]]; then
echo "Attempting dnf uninstall..."
sudo dnf remove $1
if [[ $? == 0 ]]; then
return 0
fi
fi
echo "ERROR - $1 not found."
return 1
}
# If no parameter # If no parameter or u
if [ -z $1 ] || [ $1 = "u" ]; then if [ -z $1 ] || [ $1 = "u" ]; then
echo -e "${GREEN}[+] Running update script...${ENDCOLOR}" updateP
update
echo -e "${GREEN}[+] Updating Nix...${ENDCOLOR}"
nix-env -u
return 0 return 0
elif [ $1 = "i" ]; then # If first parameter is i (install) elif [ $1 = "i" ]; then # If first parameter is i (install)
check $2 installP $2
if [[ $? != 1 ]]; then
echo "$2 is already installed."
return 1
fi
nix search nixpkgs $2 --extra-experimental-features 'nix-command flakes' &>/dev/null
nix_found=$?
$search $2 &>/dev/null
distro_found=$?
if [[ $nix_found == 0 && $distro_found == 0 ]]; then
echo "1. Nix: $2 found."
echo "2. $distro: $2 found."
echo -n "Choose a package manager: "
read p_choice
if [[ $p_choice == 1 ]]; then
nix-env -iA nixpkgs.$2
elif [[ $p_choice == 2 ]]; then
$install $2
else
return 1
fi
return 0
fi
if [[ $nix_found == 0 ]]; then
nix-env -iA nixpkgs.$2
elif [[ $distro_found == 0 ]]; then
$install $2
else
echo "$2 not found."
return 1
fi
return 0
elif [ $1 = "r" ]; then # If first parameter is r (remove) elif [ $1 = "r" ]; then # If first parameter is r (remove)
check $2 removeP $2
check_result=$?
if [[ $check_result == 2 ]]; then
echo "1. Nix: $2 installed."
echo "2. $distro: $2 installed."
echo -n "Choose a package manager: "
read p_choice
if [[ $p_choice == 1 ]]; then
nix-env -e $2
elif [[ $p_choice == 2 ]]; then
$remove $2
else
return 1
fi
return 0
fi
if [[ $check_result == 3 ]]; then
nix-env -e $2
elif [[ $check_result == 4 ]]; then
$remove $2
else
echo "$2 is not installed."
return 1
fi
return 0
elif [ $1 = "c" ]; then # If first parameter is c (check) elif [ $1 = "c" ]; then # If first parameter is c (check)
check $2 checkP $2
else else
echo -e "${YELLOW}${UNDERLINE}[i] Usage:${ENDCOLOR}" echo -e "${YELLOW}${UNDERLINE}[i] Usage:${ENDCOLOR}"
echo -e "p (u) ${FAINT}- update os${ENDCOLOR}" echo -e "p (u) ${FAINT}- update os${ENDCOLOR}"
echo -e "p i package ${FAINT}- install package${ENDCOLOR}" echo -e "p i package ${FAINT}- install package${ENDCOLOR}"
echo -e "p r package ${FAINT}- remove package${ENDCOLOR}" echo -e "p r package ${FAINT}- remove package${ENDCOLOR}"
echo -e "p c package ${FAINT}- check if package is installed (true/false; 2 -> ERROR)${ENDCOLOR}" echo -e "p c package ${FAINT}- check if package is installed${ENDCOLOR}"
return 1 return 1
fi fi
) )