158 lines
3.2 KiB
Bash
158 lines
3.2 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() {
|
|
test -e "${1}" || return 1
|
|
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="${1}"
|
|
|
|
if [ ! -d "${directory}" ]; then
|
|
echo "Directory '${directory}' not found" >&2
|
|
return 1
|
|
fi
|
|
|
|
age="+21" # Notation: +n => "At least n days"
|
|
echo "Deleting files not accessed for a ${age} days:"
|
|
find "${directory}" -atime "${age}" -exec rm -fv {} \;
|
|
|
|
echo "Deleting empty directories:"
|
|
find "${directory}" -type d -empty -print -delete
|
|
}
|
|
|
|
cleanup-downloads() {
|
|
local -r downloads_directory="${XDG_DOWNLOAD_DIR}"
|
|
local -r log_file="${downloads_directory}/cleanup.log"
|
|
|
|
touch "${log_file}"
|
|
|
|
echo "$(date -Iseconds) : Start cleanup" >> "${log_file}"
|
|
cleanup-directory "${downloads_directory}" | tee -a "${log_file}"
|
|
}
|
|
|
|
_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() {
|
|
go build $@ -o . ./...
|
|
}
|