install.fairie/docs/scripts/profile/functions.sh.tmpl.md
2023-08-08 03:06:49 -04:00

4.8 KiB

title description sidebar_label slug githubLocation scriptLocation repoLocation
Functions Houses the functions that are included by `~/.bashrc` and `~/.zshrc` Functions /scripts/profile/functions.sh.tmpl https://github.com/megabyte-labs/install.doctor/blob/master/home/dot_config/shell/functions.sh.tmpl https://github.com/megabyte-labs/install.doctor/raw/master/home/dot_config/shell/functions.sh.tmpl home/dot_config/shell/functions.sh.tmpl

Functions

Houses the functions that are included by ~/.bashrc and ~/.zshrc

Overview

This script is included by ~/.bashrc and ~/.zshrc to provide various commands and functions.

Source Code

#!/usr/bin/env sh
# @file Functions
# @brief Houses the functions that are included by `~/.bashrc` and `~/.zshrc`
# @description
#     This script is included by `~/.bashrc` and `~/.zshrc` to provide various commands and functions.

# Install WebDriverAgent on iOS device
appiumwebdriver() {
  # read -r "Enter the UDID of the device you wish to install WebDriverAgent on: " UDID_INPUT
  mkdir -p Resources/WebDriverAgent.bundle
  bash ./Scripts/bootstrap.sh -d
  cd /Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-webdriveragent || return
  xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination "id=${UDID_INPUT}" test
}

asdf-update () {
  asdf update > /dev/null 2>&1
  asdf plugin update --all > /dev/null 2>&1
  diff <(cat ~/.tool-versions | awk '{print $1}' | xargs -I {} bash -c 'echo {} $(asdf latest {})') <(cat ~/.tool-versions)
}

# Change directories and view contents at the same time
cl() {
  DIR="$*"
  # if no DIR given, go home
  if [ $# -lt 1 ]; then
    DIR=$HOME
  fi
  builtin cd "${DIR}" &&
    # use your preferred ls command
    ls -F --color=auto
}

# Safer cp with progress bar and backup to /tmp
cpv() {
  rsync -pogbr -hhh --backup-dir="/tmp/rsync-${USERNAME}" -e /dev/null --progress "$@"
}

# Checks status of a website on downforeveryoneorjustme.com
down4me() {
  curl -s "http://www.downforeveryoneorjustme.com/$1" | sed '/just you/!d;s/<[^>]*>//g'
}

# Easier version of find command
find() {
  if [ $# = 1 ]; then
    # shellcheck disable=SC2145
    command find . -iname "*$@*"
  else
    command find "$@"
  fi
}

# Opens current repository in browser
gitopen() {
  git remote -v | head -n 1 | awk -F "@" '{print $2}' | awk -F " " '{print $1}' | sed 's/:/\//g' | sed 's/.git//g' | awk '{print "http://"$1}' | xargs open
}

# Open Mac OS X desktop on a Linux machine
macosx() {
  docker run -it --device /dev/kvm -p 50922:10022 -v /tmp/.X11-unix:/tmp/.X11-unix -e "DISPLAY=${DISPLAY:-:0.0}" sickcodes/docker-osx:big-sur
}

# Generate a random string of X length
randomstring() {
  if [ -z "$1" ]; then
    head /dev/urandom | tr -dc A-Za-z0-9 | head -c "$1"
  else
    echo "Pass the number of characters you would like the string to be. Example: randomstring 14"
  fi
}

# Reset Docker to factory settings
resetdocker() {
  set +e
  CONTAINER_COUNT="$(docker ps -a -q | wc -l)"
  if [ "$CONTAINER_COUNT" -gt 0 ]; then
    docker stop "$(docker ps -a -q)"
    docker rm "$(docker ps -a -q)"
  fi
  VOLUME_COUNT="$(docker volume ls -q | wc -l)"
  if [ "$VOLUME_COUNT" -gt 0 ]; then
    docker volume rm "$(docker volume ls -q)"
  fi
  NETWORK_COUNT="$(docker network ls -q | wc -l)"
  if [ "$NETWORK_COUNT" -gt 0 ]; then
    docker network rm "$(docker network ls -q)"
  fi
  docker system prune -a --force
}

# ripgrep-all
rgafzf() {
	RG_PREFIX="rga --files-with-matches"
	local file
	file="$(
		FZF_DEFAULT_COMMAND="$RG_PREFIX '$1'" \
			fzf --sort --preview="[[ ! -z {} ]] && rga --pretty --context 5 {q} {}" \
				--phony -q "$1" \
				--bind "change:reload:$RG_PREFIX {q}" \
				--preview-window="70%:wrap"
	)" &&
	echo "opening $file" &&
	xdg-open "$file"
}

#  Easy file sharing from the command line, using transfer.sh
transfer() {
  if [ $# -eq 0 ]; then
    echo -e "No arguments specified.\nUsage:\n  transfer <file|directory>\n  ... | transfer <file_name>" >&2
    return 1
  fi
  if tty -s; then
    file="$1"
    file_name=$(basename "$file")
    if [ ! -e "$file" ]; then
      echo "$file: No such file or directory" >&2
      return 1
    fi
    if [ -d "$file" ]; then
      file_name="$file_name.zip"
      (cd "$file" && zip -r -q - .) | curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name" | tee /dev/null,
    else
      curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name" <"$file" | tee /dev/null
    fi
  else
    file_name=$1
    curl --progress-bar --upload-file "-" "https://transfer.sh/$file_name" | tee /dev/null
  fi
}

update-all() {
  brew update
  brew upgrade
  brew upgrade --cask --greedy
  brew cleanup --prune=1
  asdf update || true
  asdf plugin-update --all
  rustup self update || true
  rustup update || true
  npm update -g || true
  chezmoi upgrade
  z4h update
}