From 6b7b3f6a2e5267e837cccce4f8c69f7bbe882fbf Mon Sep 17 00:00:00 2001 From: Menno van Leeuwen Date: Thu, 22 Aug 2024 16:13:24 +0200 Subject: [PATCH] new ways 2 --- bin/actions/export.sh | 8 ++ bin/actions/help.sh | 13 ++ bin/actions/update.sh | 25 ++++ bin/dotf | 61 +++++++++ bin/helpers/functions.sh | 142 ++++++++++++++++++++ bin/helpers/packages.sh | 74 ++++++++++ bin/resources/help.txt | 8 ++ bin/resources/logo.txt | 7 + config/gterminal.preferences | 27 ++++ {.config => config}/starship.toml | 0 {Setup => docs}/cli.md | 0 {Setup => docs}/gnome-extensions.md | 0 {Setup => docs}/packages.md | 0 {Setup => docs}/readme.md | 0 {.config/Code/User => vscode}/settings.json | 6 + .zshrc => zshrc | 2 + 16 files changed, 373 insertions(+) create mode 100755 bin/actions/export.sh create mode 100755 bin/actions/help.sh create mode 100755 bin/actions/update.sh create mode 100755 bin/dotf create mode 100755 bin/helpers/functions.sh create mode 100644 bin/helpers/packages.sh create mode 100644 bin/resources/help.txt create mode 100644 bin/resources/logo.txt create mode 100644 config/gterminal.preferences rename {.config => config}/starship.toml (100%) rename {Setup => docs}/cli.md (100%) rename {Setup => docs}/gnome-extensions.md (100%) rename {Setup => docs}/packages.md (100%) rename {Setup => docs}/readme.md (100%) rename {.config/Code/User => vscode}/settings.json (54%) rename .zshrc => zshrc (97%) diff --git a/bin/actions/export.sh b/bin/actions/export.sh new file mode 100755 index 0000000..b751da5 --- /dev/null +++ b/bin/actions/export.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env zsh + +source ~/dotfiles/bin/helpers/functions.sh + +printfe "%s\n" "cyan" "Exporting Gnome Terminal preferences" +dconf dump /org/gnome/terminal/ > ~/dotfiles/config/gterminal.preferences + +printfe "%s\n" "green" "Finished, don't forget to commit and push" \ No newline at end of file diff --git a/bin/actions/help.sh b/bin/actions/help.sh new file mode 100755 index 0000000..2aa3b14 --- /dev/null +++ b/bin/actions/help.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +source ~/dotfiles/bin/helpers/functions.sh + +# Print logo +tput setaf 2 +cat ~/dotfiles/bin/resources/logo.txt +println " " "cyan" +tput sgr0 + +# Print help +cat ~/dotfiles/bin/resources/help.txt +println " " "cyan" diff --git a/bin/actions/update.sh b/bin/actions/update.sh new file mode 100755 index 0000000..0eaef8e --- /dev/null +++ b/bin/actions/update.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env zsh + +source ~/dotfiles/bin/helpers/functions.sh + +printfe "%s\n" "cyan" "Pulling latest changes..." +git -C ~/dotfiles pull + +if [ $? -ne 0 ]; then + printfe "%s\n" "red" "Failed to pull latest changes" + exit 1 +fi + +printfe "%s\n" "cyan" "Updating symlinks..." +check_or_make_symlink ~/.zshrc ~/dotfiles/zshrc +check_or_make_symlink ~/.config/Code/User/settings.json ~/dotfiles/vscode/settings.json +check_or_make_symlink ~/.config/starship.toml ~/dotfiles/config/starship.toml + +printfe "%s\n" "cyan" "Ensuring packages are installed..." +source ~/dotfiles/bin/helpers/packages.sh +ensure_packages_installed + +printfe "%s\n" "cyan" "Importing Gnome Terminal preferences..." +cat ~/dotfiles/config/gterminal.preferences | dconf load /org/gnome/terminal/legacy/profiles:/ + +printfe "%s\n" "green" "Finished, don't forget restart your terminal" diff --git a/bin/dotf b/bin/dotf new file mode 100755 index 0000000..3c1e0ba --- /dev/null +++ b/bin/dotf @@ -0,0 +1,61 @@ +#!/usr/bin/env zsh + +source ~/dotfiles/bin/helpers/functions.sh + +logo() { + tput setaf 2 + cat ~/dotfiles/bin/resources/logo.txt + println " " "cyan" + tput sgr0 + + # Print if repo is dirty and the count of untracked files, modified files and staged files + if [[ $(git -C ~/dotfiles status --porcelain) ]]; then + printfe "%s" "yellow" "dotfiles repo is dirty " + printfe "%s" "red" "[$(git -C ~/dotfiles status --porcelain | grep -c '^??')] untracked " + printfe "%s" "yellow" "[$(git -C ~/dotfiles status --porcelain | grep -c '^ M')] modified " + printfe "%s" "green" "[$(git -C ~/dotfiles status --porcelain | grep -c '^M ')] staged " + fi + + printfe "%s" "blue" "[$(git -C ~/dotfiles rev-parse --short HEAD)]" + println "" "normal" + + if [[ $(git -C ~/dotfiles status --porcelain) ]]; then + # Continue? + printfe "%s" "red" "Continue anyway? [y/N] " + read -k 1 + + if [[ $REPLY != "y" ]]; then + println "" "normal" + exit 0 + fi + println "" "normal" + println "" "normal" + fi +} + +update() { + ~/dotfiles/bin/actions/update.sh $@ +} + +help() { + ~/dotfiles/bin/actions/help.sh $@ +} + +exports() { + ~/dotfiles/bin/actions/export.sh $@ +} + +# switch case for parameters +case $1 in + "update") + logo + update $@ + ;; + "export") + logo + exports $@ + ;; + "help"|"--help"|"") + help $@ + ;; +esac diff --git a/bin/helpers/functions.sh b/bin/helpers/functions.sh new file mode 100755 index 0000000..6dc532d --- /dev/null +++ b/bin/helpers/functions.sh @@ -0,0 +1,142 @@ +#!/usr/bin/env zsh + +#Color print function, usage: println "message" "color" +println() { + color=$2 + printfe "%s\n" $color "$1" +} + +# print colored with printf (args: format, color, message ...) +printfe() { + format=$1 + color=$2 + shift 2 + + red=$(tput setaf 1) + green=$(tput setaf 2) + yellow=$(tput setaf 3) + blue=$(tput setaf 4) + magenta=$(tput setaf 5) + cyan=$(tput setaf 6) + normal=$(tput sgr0) + + case $color in + "red") + color=$red + ;; + "green") + color=$green + ;; + "yellow") + color=$yellow + ;; + "blue") + color=$blue + ;; + "magenta") + color=$magenta + ;; + "cyan") + color=$cyan + ;; + *) + color=$normal + ;; + esac + + printf "$color$format$normal" "$@" +} + +ensure_package_installed() { + if ! command -v $1 &>/dev/null; then + println "$1 is not installed. Please install it." "red" + exit 1 + fi + println " - $1 is available." "green" +} + +ask_before_do() { + printfe "%s" "yellow" "Trying to run: " + printfe "%s" "cyan" "'$@' " + read -p "Continue? [y/N]: " -n 1 -r + echo "" + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + return + fi + + printfe "%s" "cyan" "Running '" + printfe "%s" "yellow" "$@" + println "'..." "cyan" + + # In case DRY_RUN is set to true we should just print the command and not run it + if [ "$DRY_RUN" = true ]; then + println "Would have run '$@'" "yellow" + return + else + $@ + fi +} + +ensure_sudo_privileges() { + if sudo -n true 2>/dev/null; then + return + else + println "$1" "yellow" + sudo true + fi +} + +ask_before_do_multi() { + if [ "$DRY_RUN" = true ]; then + println "Would have run: $1" "yellow" + else + read -p "$1 (y/n) " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + return false + fi + return true + fi +} + +add_to_hosts() { + local domain=$1 + local ip="127.0.0.1" + + # Check if domain already exists in /etc/hosts + if ! grep -q "$domain" /etc/hosts; then + println " - adding $domain to /etc/hosts" "yellow" + echo "$ip $domain" | sudo tee -a /etc/hosts >/dev/null + else + println " - $domain already exists in /etc/hosts" "green" + fi +} + +# Function to check if $1 is a path to a symlink, if not it will make a symlink to $2 +# In case there's a file at the location of the symlink, it will backup the file +# Parameters +# $1: file to check +# $2: link location +check_or_make_symlink() { + if [ ! -L $1 ]; then + if [ -f $1 ]; then + mv $1 $1.bak + printfe "%s\n" "yellow" "Backed up $1 to $1.bak" + fi + ln -s $2 $1 + printfe "%s\n" "green" "Created symlink $1 -> $2" + fi + + # Confirm the symlink that already exists point to the correct location + if [ -L $1 ]; then + if [ "$(readlink $1)" != $2 ]; then + printfe "%s\n" "yellow" "Symlink $1 exists but points to the wrong location" + printfe "%s\n" "yellow" "Expected: $2" + printfe "%s\n" "yellow" "Actual: $(readlink $1)" + printfe "%s\n" "yellow" "Fixing symlink" + rm $1 + ln -s $2 $1 + printfe "%s\n" "green" "Created symlink $1 -> $2" + fi + fi +} diff --git a/bin/helpers/packages.sh b/bin/helpers/packages.sh new file mode 100644 index 0000000..c9cfd6e --- /dev/null +++ b/bin/helpers/packages.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env zsh + +packages=( + "zsh" + "git" + "curl" + "wget" + "vim" + "tmux" + "sl" + "just" + "libglvnd-dev" + "libwayland-dev" + "libseat-dev" + "libxkbcommon-dev" + "libinput-dev" + "udev" + "dbus" + "libdbus-1-dev" + "libsystemd-dev" + "libpixman-1-dev" + "libssl-dev" + "libflatpak-dev" + "libpulse-dev" + "libexpat1-dev" + "libfontconfig-dev" + "libfreetype-dev" + "mold" + "cargo" + "libgbm-dev" + "libclang-dev" + "libpipewire-0.3-dev" + "libpam0g-dev" + "openssh-server" + "build-essential" + "flatpak" + "meson" + "pipx" + "python3-nautilus" + "gettext" + "fzf" + "neofetch" + "screenfetch" +) + +ensure_packages_installed() { + ensure_sudo_privileges "In order to install packages, please provide your password:" + + # Check if packages array contains duplicates + if [ $(echo $packages | tr ' ' '\n' | sort | uniq -d | wc -l) -ne 0 ]; then + printfe "%s\n" "red" "The packages array contains duplicates" + printfe "%s\n" "yellow" "Duplicates:" + printfe "%s\n" "blue" $(echo $packages | tr ' ' '\n' | sort | uniq -d) + exit 1 + fi + + for package in $packages; do + if ! command -v $package &> /dev/null; then + printfe "%s" "yellow" "Installing $package..." + echo -en "\r" + + sudo apt install -y $package &> /dev/null + + if [ $? -ne 0 ]; then + printfe "%s\n" "red" "Failed to install $package" + exit 1 + else + printfe "%s\n" "green" "$package installed successfully" + fi + else + printfe "%s\n" "green" "$package is already installed" + fi + done +} diff --git a/bin/resources/help.txt b/bin/resources/help.txt new file mode 100644 index 0000000..814eaeb --- /dev/null +++ b/bin/resources/help.txt @@ -0,0 +1,8 @@ + +All [] are optional parameters. And all <> are required parameters. +Usage: dotf [options] [optional parameters] + + update: Pull latest changes, and update symlinks and configurations. + export: Export dconf, gsettings, and other configurations. + help: Shows this help message + \ No newline at end of file diff --git a/bin/resources/logo.txt b/bin/resources/logo.txt new file mode 100644 index 0000000..e05d7a2 --- /dev/null +++ b/bin/resources/logo.txt @@ -0,0 +1,7 @@ + + _ ___ _ __ _ _ + /\/\ ___ _ __ _ __ ___( )__ / \___ | |_ / _(_) | ___ ___ + / \ / _ \ '_ \| '_ \ / _ \/ __| / /\ / _ \| __| |_| | |/ _ \/ __| +/ /\/\ \ __/ | | | | | | (_) \__ \ / /_// (_) | |_| _| | | __/\__ \ +\/ \/\___|_| |_|_| |_|\___/|___/ /___,' \___/ \__|_| |_|_|\___||___/ + diff --git a/config/gterminal.preferences b/config/gterminal.preferences new file mode 100644 index 0000000..9efde25 --- /dev/null +++ b/config/gterminal.preferences @@ -0,0 +1,27 @@ +[legacy/keybindings] +reset-and-clear='k' + +[legacy/profiles:/:b1dcc9dd-5262-4d8d-a863-c897e6d979b9] +background-color='rgb(23,20,33)' +cursor-blink-mode='on' +cursor-shape='underline' +default-size-columns=120 +default-size-rows=30 +font='MesloLGS Nerd Font 14' +foreground-color='rgb(208,207,204)' +use-system-font=false +use-theme-colors=false + +[legacy/profiles:/legacy/keybindings] +reset-and-clear='k' + +[legacy/profiles:/legacy/profiles:/:b1dcc9dd-5262-4d8d-a863-c897e6d979b9] +background-color='rgb(23,20,33)' +cursor-blink-mode='on' +cursor-shape='underline' +default-size-columns=120 +default-size-rows=30 +font='MesloLGS Nerd Font 14' +foreground-color='rgb(208,207,204)' +use-system-font=false +use-theme-colors=false diff --git a/.config/starship.toml b/config/starship.toml similarity index 100% rename from .config/starship.toml rename to config/starship.toml diff --git a/Setup/cli.md b/docs/cli.md similarity index 100% rename from Setup/cli.md rename to docs/cli.md diff --git a/Setup/gnome-extensions.md b/docs/gnome-extensions.md similarity index 100% rename from Setup/gnome-extensions.md rename to docs/gnome-extensions.md diff --git a/Setup/packages.md b/docs/packages.md similarity index 100% rename from Setup/packages.md rename to docs/packages.md diff --git a/Setup/readme.md b/docs/readme.md similarity index 100% rename from Setup/readme.md rename to docs/readme.md diff --git a/.config/Code/User/settings.json b/vscode/settings.json similarity index 54% rename from .config/Code/User/settings.json rename to vscode/settings.json index 1f454f9..7762543 100755 --- a/.config/Code/User/settings.json +++ b/vscode/settings.json @@ -3,4 +3,10 @@ "editor.fontFamily": "MesloLGS Nerd Font", "terminal.integrated.fontFamily": "MesloLGS Nerd Font", + "github.copilot.enable": { + "*": true, + "plaintext": true, + "markdown": false, + "scminput": false + }, } \ No newline at end of file diff --git a/.zshrc b/zshrc similarity index 97% rename from .zshrc rename to zshrc index 3e23b86..aa99ba9 100755 --- a/.zshrc +++ b/zshrc @@ -1,3 +1,5 @@ +export PATH=$PATH:~/dotfiles/bin + HISTFILE=~/.histfile HISTSIZE=1000 SAVEHIST=1000