184 lines
3.9 KiB
Bash

# 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"
}
va() {
if [[ -v 1 ]]; then
if [[ -f "${1}/bin/activate" ]]; then
source "${1}/bin/activate"
return 0
fi
echo "va: error: virtual environment ${1} not found, use 'python3 -m venv ${1}'" >&2
return 1
fi
if [[ -f "./venv/bin/activate" ]]; then
source "./venv/bin/activate"
return 0
fi
local activate_venv
activate_venv="$(find -P . -maxdepth 3 -type f -wholename '*/bin/activate' | sort | head -n 1)"
readonly activate_venv
if [[ -f "${activate_venv}" ]]; then
source "${activate_venv}"
return 0
else
echo "va: error: virtual environment not found, use python3 -m venv venv" >&2
return 1
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}" -atime "${age}"
echo "Deleting empty directories:"
find "${directory}" -type d -empty -print
else
echo "Deleting files not accessed for a ${age} days:"
find "${directory}" -atime "${age}" | xargs -r rm -fv
echo "Deleting empty directories:"
find "${directory}" -type d -empty -print | xargs -r rmdir
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 city="${1}"
curl "wttr.in/${city}" 2>/dev/null | head -n -1
}
gobuild() {
mkdir ./bin
go build $@ -o ./bin ./...
}