gnu++ in Rust (2024-07-24)

I've discussed rewrites of commonly used gnu utilites in Rust. I've written a script that installs cargo and every Rust rewrite with it. The script also optionally writes alias definitions to the appropriate shell rc file. It assumes bash by default. The script source will be pasted below, and the repo is on my Github.

Source

#!/bin/bash

# Default list of crates
default_crates=(
    "bat"
    "choose"
    "exa"
    "lsd"
    "git-delta"
    "du-dust"
    "duf"
    "broot"
    "fd-find"
    "ripgrep"
    "sd"
    "bottom"
    "gping"
    "procs"
    "zoxide"
    "dns-doge"
    "mcfly"
    "xh"
    "hyperfine"
)

# Default shell config file
shell_config=".bashrc"

# Usage function
usage() {
    echo "Usage: $0 [-a] [-s SHELL_CONFIG] [-f PACKAGE_FILE] [-h]"
    echo "  -s: Specify shell configuration file (.bashrc or .zshrc)"
    echo "  -f: Specify a file with a list of packages"
    echo "  -h: Display this help message"
    exit 1
}

flag=0
# Parse command-line options
while getopts ":as:f:h" opt; do
    case $opt in
        a)
            flag=1
            ;;
        s)
            shell_config="$OPTARG"
            ;;
        f)
            package_file="$OPTARG"
            ;;
        h)
            usage
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            usage
            ;;
    esac
done

# Read packages from file if specified
if [ -n "$package_file" ]; then
    if [ -f "$package_file" ]; then
        mapfile -t default_crates < "$package_file"
    else
        echo "Error: Package file not found: $package_file"
        exit 1
    fi
fi

install_rust() {
  curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  PATH="$HOME/.cargo/bin:$PATH"
  printf "\n" >> "$HOME/$shell_config"
  echo "export PATH=$PATH" >> "$HOME/$shell_config"
}

# Function to set up aliases
setup_aliases() {
    {
        printf "\n"
        echo "#gnu++ Rust Alternative Aliases"
        echo "alias cat='bat --paging=always'"
        echo "alias ls='exa -la'"
        echo "alias diff='git-delta'"
        echo "alias du='du-dust'"
        echo "alias df='duf'"
        echo "alias tree='broot'"
        echo "alias find='fd'"
        echo "alias grep='rg'"
        echo "alias find='fd'"
        echo "alias cut='choose'"
        echo "alias sed='sd'"
        echo "alias top='btm'"
        echo "alias ps='procs'"
        echo "alias cd='z'"
        echo 'eval "$(zoxide init bash)"'
        echo "alias dig='doge'"
    } >> "$HOME/$shell_config"
    echo "Aliases added to $HOME/$shell_config"
}

# Install packages and set up aliases
install_rust_crates() {
    for package in "${default_crates[@]}"; do
        cargo install --locked "$package"
    done
}

main() {
install_rust
install_rust_crates
if [ "$flag" -eq 1 ]; then
  setup_aliases
fi
echo "Installation complete."
}

main

Usage

Usage: ./gnu++.bash [-a] [-s SHELL_CONFIG] [-f PACKAGE_FILE] [-h]
  -s: Specify shell configuration file (.bashrc or .zshrc)
  -f: Specify a file with a list of packages
  -h: Display this help message