chore: move rarely used zsh functions to files

This commit is contained in:
thek4n 2024-12-19 12:11:40 +03:00
parent dd7123f98a
commit 3aa127e7e6
11 changed files with 137 additions and 141 deletions

View File

@ -20,4 +20,5 @@ docker:.docker/cli-plugins
ipython:.ipython/profile_default/ipython_config.py ipython:.ipython/profile_default/ipython_config.py
gdb:.config/gdb gdb:.config/gdb
sandbox: 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
all:%less %t %tmux %zsh %nvim %git" all:%less %t %tmux %zsh %nvim %git"

View File

@ -57,9 +57,6 @@ alias c='clear'
alias h='history 0' alias h='history 0'
alias j='jobs -l' alias j='jobs -l'
# utils
alias genpass="openssl rand -base64 12"
alias gensalt="dd if=/dev/urandom count=16 2>/dev/null | sha256sum | head -c 64"
if command -v colordiff &>/dev/null; then if command -v colordiff &>/dev/null; then
alias diff='colordiff' alias diff='colordiff'
@ -81,8 +78,6 @@ alias svi="sudo --preserve-env nvim"
# net # net
alias ports='ss -tlnp' alias ports='ss -tlnp'
alias wget='wget -c' alias wget='wget -c'
# alias myip='curl ipinfo.io/ip'
alias myip='dig +short myip.opendns.com @resolver1.opendns.com'
alias ip='ip -c' alias ip='ip -c'
alias fastping='ping -c 100 -i 0.1' alias fastping='ping -c 100 -i 0.1'

View File

@ -1,29 +1,5 @@
# vim: ft=zsh # vim: ft=zsh
destroy() {
local -r filename="${1}"
if [[ ! -f "${filename}" ]]; then
echo "destroy: File '${filename}' not found" >&2
return 1
fi
local filesize
filesize="$(du -hs "${filename}" | awk '{printf $1}')"
readonly filesize
echo -n "Sure want to destroy file '${filename}' with size ${filesize} [y/N] "
local response
read -r response
readonly response
if [[ ! "${response}" == [yY] ]]; then
return 1
fi
shred -zun 3 "${filename}"
}
py() { py() {
if [[ -z "$@" && -x "$(command -v ipython 2>/dev/null)" ]]; then if [[ -z "$@" && -x "$(command -v ipython 2>/dev/null)" ]]; then
ipython -i -c "q = exit" ipython -i -c "q = exit"
@ -65,46 +41,6 @@ va() {
fi fi
} }
extract() (
if [ -z "${1}" ]; then
echo "extract: error: usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
return 2
fi
if ! [ -f "${1}" ]; then
echo "extract: error: '${1}' file does not exist" >&2
return 1
fi
local -r name="${1%%.*}" # removes extension from filename
if [ -e "${name}" ]; then
echo "extract: error: '${name}' exists" >&2
return 1
fi
mkdir "${name}" && cd "${name}" || return 1
case $1 in
*.tar.bz2) tar xjf ../"${1}" ;;
*.tar.gz) tar xzf ../"${1}" ;;
*.tar.xz) tar xJf ../"${1}" ;;
*.lzma) unlzma ../"${1}" ;;
*.bz2) bunzip2 ../"${1}" ;;
*.rar) unrar x -ad ../"${1}" ;;
*.gz) gunzip ../"${1}" ;;
*.tar) tar xf ../"${1}" ;;
*.tbz2) tar xjf ../"${1}" ;;
*.tgz) tar xzf ../"${1}" ;;
*.zip) unzip ../"${1}" ;;
*.Z) uncompress ../"${1}" ;;
*.7z) 7z x ../"${1}" ;;
*.xz) unxz ../"${1}" ;;
*.exe) cabextract ../"${1}" ;;
*) echo "extract: error: '${1}' - unknown archive method" >&2 ;;
esac
)
mcd() { mcd() {
if [ -z "${1}" ]; then if [ -z "${1}" ]; then
cd "$(mktemp -td "${USER:-user}.XXXX")" cd "$(mktemp -td "${USER:-user}.XXXX")"
@ -118,19 +54,6 @@ open() {
nohup xdg-open "${1}" 1>/dev/null 2>&1 & nohup xdg-open "${1}" 1>/dev/null 2>&1 &
} }
split-file() {
if [[ ! -e "${1}" ]]; then
echo "file '${1}' not found" >&2
return 1
fi
local -r size="${2:-1G}"
mkdir "${1}.splitted"
cd "${1}.splitted"
split -d -b "${size}" "../${1}"
}
json() { json() {
if [ -t 0 ]; then if [ -t 0 ]; then
python -m json.tool <<< "$*" python -m json.tool <<< "$*"
@ -139,21 +62,6 @@ json() {
fi fi
} }
rmt() {
local -r trash="${TRASH:-"${HOME}/.trash"}"
local filename
for filename in "$@"
do
local filename_out_path
filename_out_path="${trash}$(realpath "${filename}")"
readonly filename_out_path
mkdir -p "$(dirname "${filename_out_path}")"
mv "${filename}" "${filename_out_path}_$(date +%s)"
done
}
showtips() { showtips() {
local -r tips_dir="${HOME}/.tips" local -r tips_dir="${HOME}/.tips"
@ -171,23 +79,6 @@ showtips() {
return 0 return 0
} }
most-often-commands() {
history 0 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn
}
django-create-project() (
set -ue
local -r project_name="${1}"
mkdir "${project_name}"
cd "${project_name}"
python3 -m virtualenv venv
. venv/bin/activate
pip install django
django-admin startproject core .
git init
)
cleanup-directory() { cleanup-directory() {
local -r directory="${1}" local -r directory="${1}"
@ -242,33 +133,6 @@ bak() {
done done
} }
mirror-site() (
set -eu
local -r name="${1}"; shift
mkdir -p "${name}" && cd "${name}"
local -r user_agent="Mozilla/5.0 ..."
wget \
--page-requisites \
--convert-links \
--adjust-extension \
--restrict-file-names=ascii \
--span-hosts \
--random-wait \
--execute robots=off \
--recursive \
--timestamping \
-l inf \
--no-remove-listing \
--no-parent \
--user-agent "${user_agent}" \
--reject '*.woff*,*.ttf,*.eot,*.js' \
--tries 10 \
$@
)
sha() { sha() {
if [ -v 2 ]; then if [ -v 2 ]; then
shasum -a 256 "${@}" shasum -a 256 "${@}"

23
home/user/.local/bin/destroy Executable file
View File

@ -0,0 +1,23 @@
#!/bin/sh
set -eu
readonly filename="${1}"
if [ ! -f "${filename}" ]; then
echo "destroy: File '${filename}' not found" >&2
exit 1
fi
filesize="$(du -hs "${filename}" | awk '{printf $1}')"
readonly filesize
printf "Sure want to destroy file '%s' with size %s [y/N] " "${filename}" "${filesize}"
read -r response
readonly response
if [ ! "${response}" = "y" ]; then
exit 1
fi
shred -zun 3 "${filename}"

View File

@ -0,0 +1,15 @@
#!/bin/sh
set -ue
readonly project_name="${1}"
mkdir "${project_name}"
cd "${project_name}"
python3 -m virtualenv venv
. venv/bin/activate
pip install django
django-admin startproject core .
git init

41
home/user/.local/bin/extract Executable file
View File

@ -0,0 +1,41 @@
#!/bin/sh
set -eu
if [ -z "${1}" ]; then
echo "extract: error: usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
exit 2
fi
if ! [ -f "${1}" ]; then
echo "extract: error: '${1}' file does not exist" >&2
exit 1
fi
readonly name="${1%%.*}" # removes extension from filename
if [ -e "${name}" ]; then
echo "extract: error: '${name}' exists" >&2
exit 1
fi
mkdir "${name}" && cd "${name}" || exit 1
case "${1}" in
*.tar.bz2) tar xjf ../"${1}" ;;
*.tar.gz) tar xzf ../"${1}" ;;
*.tar.xz) tar xJf ../"${1}" ;;
*.lzma) unlzma ../"${1}" ;;
*.bz2) bunzip2 ../"${1}" ;;
*.rar) unrar x -ad ../"${1}" ;;
*.gz) gunzip ../"${1}" ;;
*.tar) tar xf ../"${1}" ;;
*.tbz2) tar xjf ../"${1}" ;;
*.tgz) tar xzf ../"${1}" ;;
*.zip) unzip ../"${1}" ;;
*.Z) uncompress ../"${1}" ;;
*.7z) 7z x ../"${1}" ;;
*.xz) unxz ../"${1}" ;;
*.exe) cabextract ../"${1}" ;;
*) echo "extract: error: '${1}' - unknown archive method" >&2 ;;
esac

5
home/user/.local/bin/genpass Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
set -ue
openssl rand -base64 12

5
home/user/.local/bin/gensalt Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
set -ue
dd if=/dev/urandom count=16 2>/dev/null | sha256sum | head -c 64

View File

@ -0,0 +1,26 @@
#!/bin/sh
set -eu
readonly name="${1}"; shift
mkdir -p "${name}" && cd "${name}"
readonly user_agent="Mozilla/5.0 ..."
wget \
--page-requisites \
--convert-links \
--adjust-extension \
--restrict-file-names=ascii \
--span-hosts \
--random-wait \
--execute robots=off \
--recursive \
--timestamping \
-l inf \
--no-remove-listing \
--no-parent \
--user-agent "${user_agent}" \
--reject '*.woff*,*.ttf,*.eot,*.js' \
--tries 10 \
"$@"

6
home/user/.local/bin/myip Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
curl ipinfo.io/ip
echo
dig +short myip.opendns.com @resolver1.opendns.com

15
home/user/.local/bin/split-file Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
set -eu
if [ ! -e "${1}" ]; then
echo "file '${1}' not found" >&2
exit 1
fi
readonly size="${2:-1G}"
mkdir "${1}.splitted"
cd "${1}.splitted"
split -d -b "${size}" "../${1}"