diff --git a/TARGETS.sh b/TARGETS.sh index de64f97..21881ef 100644 --- a/TARGETS.sh +++ b/TARGETS.sh @@ -25,5 +25,5 @@ gdb:.config/gdb sandbox: utils:.local/bin/myip .local/bin/genpass .local/bin/gensalt .local/bin/django-create-project .local/bin/mirror-site .local/bin/split-file .local/bin/extract .local/bin/destroy chromium:.local/bin/pchromium .local/share/applications/pchromium.desktop -scripts:.local/bin/httpstatus .local/bin/bb .local/bin/emoji .local/bin/mksh .local/bin/nato .local/bin/pastas .local/bin/timer .local/bin/tryna .local/bin/trynafail +scripts:.local/bin/httpstatus .local/bin/bb .local/bin/emoji .local/bin/mksh .local/bin/nato .local/bin/pastas .local/bin/timer .local/bin/tryna .local/bin/trynafail .local/bin/bak .local/bin/cleanup-directory .local/bin/cleanup-directory-log .local/bin/cleanup-downloads .local/bin/gobuild .local/bin/json .local/bin/mcd .local/bin/open .local/bin/py .local/bin/sha .local/bin/showtips .local/bin/weather all:%less %t %note %tmux %zsh %nvim %git" diff --git a/home/user/.config/zsh/functions b/home/user/.config/zsh/functions index 3f81c33..7d817a4 100644 --- a/home/user/.config/zsh/functions +++ b/home/user/.config/zsh/functions @@ -1,13 +1,5 @@ # vim: ft=zsh -py() { - if [[ -z "$@" && -x "$(command -v ipython 2>/dev/null)" ]]; then - ipython -i -c "q = exit" - else - python3 "$@" - fi -} - ve() { local -r venv_name="${1:-venv}" python3 -m venv "${venv_name}" && . "${venv_name}/bin/activate" @@ -41,146 +33,6 @@ va() { fi } -mcd() { - local dir - - if [ -z "${1}" ]; then - dir="$(mktemp -ut "${USER:-user}.XXXX")" - else - dir="${1}" - fi - - mkdir -p "${dir}" && cd "${dir}" -} - -open() { - if [ ! -e "${1}" ]; then - printf 'File %s not found' "${1}" >&2 - exit 1 - fi - nohup xdg-open "${1}" 1>/dev/null 2>&1 & -} - -json() { - if [ -t 0 ]; then - python -m json.tool <<< "$*" - else # pipe - python -m json.tool - fi -} - -showtips() { - local -r tips_dir="${HOME}/.tips" - - if [ ! -d "${tips_dir}" ]; then - mkdir "${tips_dir}" 2>/dev/null - git init "${tips_dir}" - fi - - set -o nullglob - local filename - for filename in "${tips_dir}"/* - do - cat "${filename}" - done - return 0 -} - -cleanup-directory() { - local -r directory="$(realpath "${1}")" - local -r age="+21" # Notation: +n => "At least n days" - - if [ ! -d "${directory}" ]; then - echo "Directory '${directory}' not found" >&2 - return 1 - fi - - if [ -n "${DRYRUN}" ]; then - echo "Deleting files not accessed for a ${age} days:" - find "${directory}" -mindepth 1 -atime "${age}" -not -type d -not -name cleanup.log - - echo "Deleting empty directories:" - find "${directory}" -mindepth 1 -type d -empty - else - echo "Deleting files not accessed for a ${age} days:" - find "${directory}" -mindepth 1 -atime "${age}" -not -type d -not -name cleanup.log -print -delete - - echo "Deleting empty directories:" - find "${directory}" -mindepth 1 -type d -empty -print -delete - fi -} - -cleanup-directory-log() { - local -r directory="$(realpath "${1}")" - if [ ! -d "${directory}" ]; then - echo "Directory '${directory}' not found" >&2 - return 1 - fi - - local log_file="${directory}/cleanup.log" - - if [ -n "${DRYRUN}" ]; then - log_file="/dev/null" - fi - - touch "${log_file}" - - echo "$(date -Iseconds) : Start cleanup" >> "${log_file}" - cleanup-directory "${directory}" | tee -a "${log_file}" -} - -cleanup-downloads() { - local -r downloads_directory="${XDG_DOWNLOAD_DIR}" - cleanup-directory-log "${downloads_directory}" -} - -_bak() { - local -r filename="${1}" - mv "${filename}" "${filename}.bak" -} - -_unbak() { - local -r filename="${1}" - local -r new_filename="${filename/%.bak/}" - - if [[ -e "${new_filename}" ]]; then - echo "Filename '${new_filename}' already exists" >&2 - return 1 - fi - - mv "${filename}" "${new_filename}" -} - -bak() { - local filename - for filename in "$@"; do - if [[ "${filename: -4}" = ".bak" ]]; then - _unbak "${filename}" - else - _bak "${filename}" - fi - done -} - -sha() { - if [ -v 2 ]; then - shasum -a 256 "${@}" - elif [ -v 1 ]; then - shasum -a 256 "${@}" | head -c 64 - else - shasum -a 256 | head -c 64 - fi -} - -weather() { - local -r city="${1}" - curl "wttr.in/${city}" 2>/dev/null | head -n -1 -} - -gobuild() { - go build $@ -o ./bin/ ./... -} - top-commands() { local -r num_args="${1:-1}" diff --git a/home/user/.local/bin/bak b/home/user/.local/bin/bak new file mode 100755 index 0000000..eb7b30f --- /dev/null +++ b/home/user/.local/bin/bak @@ -0,0 +1,34 @@ +#!/bin/sh +set -eu + +ends_with_bak() { + case "${1}" in + *.bak) return 0 ;; + *) return 1 ;; + esac +} + +_bak() { + filename="${1}" + mv "${filename}" "${filename}.bak" +} + +_unbak() { + filename="${1}" + new_filename="$(echo "${filename}" | sed 's/\.bak$//')" + + if [ -e "${new_filename}" ]; then + echo "Filename '${new_filename}' already exists" >&2 + return 1 + fi + + mv "${filename}" "${new_filename}" +} + +for filename in "${@}"; do + if ends_with_bak "${filename}"; then + _unbak "${filename}" + else + _bak "${filename}" + fi +done diff --git a/home/user/.local/bin/cleanup-directory b/home/user/.local/bin/cleanup-directory new file mode 100755 index 0000000..eb63fe3 --- /dev/null +++ b/home/user/.local/bin/cleanup-directory @@ -0,0 +1,26 @@ +#!/bin/sh +set -eu + + +directory="$(realpath "${1}")" +readonly directory +readonly age="+21" # Notation: +n => "At least n days" + +if [ ! -d "${directory}" ]; then + echo "Directory '${directory}' not found" >&2 + return 1 +fi + +if [ -n "${DRYRUN}" ]; then + echo "Deleting files not accessed for a ${age} days:" + find "${directory}" -mindepth 1 -atime "${age}" -not -type d -not -name cleanup.log + + echo "Deleting empty directories:" + find "${directory}" -mindepth 1 -type d -empty +else + echo "Deleting files not accessed for a ${age} days:" + find "${directory}" -mindepth 1 -atime "${age}" -not -type d -not -name cleanup.log -print -delete + + echo "Deleting empty directories:" + find "${directory}" -mindepth 1 -type d -empty -print -delete +fi diff --git a/home/user/.local/bin/cleanup-directory-log b/home/user/.local/bin/cleanup-directory-log new file mode 100755 index 0000000..f5d74c5 --- /dev/null +++ b/home/user/.local/bin/cleanup-directory-log @@ -0,0 +1,22 @@ +#!/bin/sh +set -eu + + +directory="$(realpath "${1}")" +readonly directory + +if [ ! -d "${directory}" ]; then + echo "Directory '${directory}' not found" >&2 + return 1 +fi + +readonly log_file="${directory}/cleanup.log" + +if [ -n "${DRYRUN}" ]; then + log_file="/dev/null" +fi + +touch "${log_file}" + +echo "$(date -Iseconds) : Start cleanup" >> "${log_file}" +cleanup-directory "${directory}" | tee -a "${log_file}" diff --git a/home/user/.local/bin/cleanup-downloads b/home/user/.local/bin/cleanup-downloads new file mode 100755 index 0000000..c8a922b --- /dev/null +++ b/home/user/.local/bin/cleanup-downloads @@ -0,0 +1,5 @@ +#!/bin/sh +set -eu + +readonly downloads_directory="${XDG_DOWNLOAD_DIR}" +cleanup-directory-log "${downloads_directory}" diff --git a/home/user/.local/bin/gobuild b/home/user/.local/bin/gobuild new file mode 100755 index 0000000..f11b236 --- /dev/null +++ b/home/user/.local/bin/gobuild @@ -0,0 +1,5 @@ +#!/bin/sh +set -eu + +#shellcheck disable=SC2068 +go build $@ -o ./bin/ ./... diff --git a/home/user/.local/bin/json b/home/user/.local/bin/json new file mode 100755 index 0000000..17d414e --- /dev/null +++ b/home/user/.local/bin/json @@ -0,0 +1,8 @@ +#!/bin/sh +set -eu + +if [ -t 0 ]; then + echo "${@}" | python -m json.tool +else + python -m json.tool +fi diff --git a/home/user/.local/bin/mcd b/home/user/.local/bin/mcd new file mode 100755 index 0000000..9c075de --- /dev/null +++ b/home/user/.local/bin/mcd @@ -0,0 +1,10 @@ +#!/bin/sh +set -eu + +if [ -z "${1}" ]; then + dir="$(mktemp -ut "${USER:-user}.XXXX")" +else + dir="${1}" +fi + +mkdir -p "${dir}" && cd "${dir}" diff --git a/home/user/.local/bin/open b/home/user/.local/bin/open new file mode 100755 index 0000000..1d7426d --- /dev/null +++ b/home/user/.local/bin/open @@ -0,0 +1,9 @@ +#!/bin/sh +set -eu + + +if [ ! -e "${1}" ]; then + printf 'File %s not found' "${1}" >&2 + exit 1 +fi +setsid xdg-open "${1}" 1>/dev/null 2>&1 diff --git a/home/user/.local/bin/py b/home/user/.local/bin/py new file mode 100755 index 0000000..e224e7e --- /dev/null +++ b/home/user/.local/bin/py @@ -0,0 +1,9 @@ +#!/bin/sh +set -eu + + +if [ -z "${*}" ] && [ -x "$(command -v ipython 2>/dev/null)" ]; then + ipython -i -c "q = exit" +else + python3 "${@}" +fi diff --git a/home/user/.local/bin/sha b/home/user/.local/bin/sha new file mode 100755 index 0000000..00139a6 --- /dev/null +++ b/home/user/.local/bin/sha @@ -0,0 +1,11 @@ +#!/bin/sh +set -eu + + +if [ -n "${2+x}" ]; then + shasum -a 256 "${@}" +elif [ -n "${1+x}" ]; then + shasum -a 256 "${@}" | head -c 64 +else + shasum -a 256 | head -c 64 +fi diff --git a/home/user/.local/bin/showtips b/home/user/.local/bin/showtips new file mode 100755 index 0000000..5eb95d0 --- /dev/null +++ b/home/user/.local/bin/showtips @@ -0,0 +1,12 @@ +#!/bin/sh +set -eu + + +readonly tips_dir="${HOME}/.tips" + +if [ ! -d "${tips_dir}" ]; then + mkdir "${tips_dir}" 2>/dev/null + git init "${tips_dir}" +fi + +find "${tips_dir}" -mindepth 1 -maxdepth 1 -type f -exec cat {} \; diff --git a/home/user/.local/bin/weather b/home/user/.local/bin/weather new file mode 100755 index 0000000..f3db7e0 --- /dev/null +++ b/home/user/.local/bin/weather @@ -0,0 +1,5 @@ +#!/bin/sh +set -eu + +readonly city="${1}" +curl "wttr.in/${city}" 2>/dev/null | head -n -1