zsh refactor

This commit is contained in:
thek4n 2024-11-18 14:15:15 +03:00
parent f9dbce3b07
commit 4461341ccf
2 changed files with 71 additions and 78 deletions

View File

@ -1,19 +1,5 @@
# vim: ft=zsh # vim: ft=zsh
_sod() {
if [ -d "$1" ] && [ -n "$(ls "$1")" ]; then
local filename
for filename in $(ls "$1" | sort -n)
do
source "${1}/${filename}"
done
fi
}
lt() {
du -h "${1:-.}" 2>/dev/null | sort -h
}
destroy() { destroy() {
local -r filename="${1}" local -r filename="${1}"
@ -39,7 +25,8 @@ destroy() {
} }
workon() { workon() {
source "/opt/pythonenv/${1:-$(basename "$PWD")}/bin/activate" local -r venvs="${VENVS:-/opt/pythonenv}"
source "${venvs}/${1:-$(basename "${PWD}")}/bin/activate"
} }
py() { py() {
@ -51,7 +38,8 @@ py() {
} }
ve() { ve() {
python3 -m virtualenv "${1:-venv}" && . "${1:-venv}/bin/activate" local -r venv_name="${1:-venv}"
python3 -m venv "${venv_name}" && . "${venv_name}/bin/activate"
} }
va() { va() {
@ -60,7 +48,7 @@ va() {
source "${1}/bin/activate" source "${1}/bin/activate"
return 0 return 0
fi fi
echo "va: error: virtual environment ${1} not found, use 'python3 -m virtualenv ${1}'" >&2 echo "va: error: virtual environment ${1} not found, use 'python3 -m venv ${1}'" >&2
return 1 return 1
fi fi
@ -73,70 +61,70 @@ va() {
activate_venv="$(find -P . -maxdepth 3 -type f -wholename '*/bin/activate' | sort | head -n 1)" activate_venv="$(find -P . -maxdepth 3 -type f -wholename '*/bin/activate' | sort | head -n 1)"
readonly activate_venv readonly activate_venv
if [[ -f "$activate_venv" ]]; then if [[ -f "${activate_venv}" ]]; then
source "$activate_venv" source "${activate_venv}"
return 0 return 0
else else
echo "va: error: virtual environment not found, use python3 -m virtualenv venv" >&2 echo "va: error: virtual environment not found, use python3 -m venv venv" >&2
return 1 return 1
fi fi
} }
extract() ( extract() (
if [ -z "$1" ]; then 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>" 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 return 2
fi fi
if ! [ -f "$1" ]; then if ! [ -f "${1}" ]; then
echo "extract: error: '$1' file does not exist" >&2 echo "extract: error: '${1}' file does not exist" >&2
return 1 return 1
fi fi
local -r name="${1%%.*}" # removes extension from filename local -r name="${1%%.*}" # removes extension from filename
if [ -e "$name" ]; then if [ -e "${name}" ]; then
echo "extract: error: '$name' exists" >&2 echo "extract: error: '${name}' exists" >&2
return 1 return 1
fi fi
mkdir "$name" && cd "$name" || return 1 mkdir "${name}" && cd "${name}" || return 1
case $1 in case $1 in
*.tar.bz2) tar xjf ../"$1" ;; *.tar.bz2) tar xjf ../"${1}" ;;
*.tar.gz) tar xzf ../"$1" ;; *.tar.gz) tar xzf ../"${1}" ;;
*.tar.xz) tar xJf ../"$1" ;; *.tar.xz) tar xJf ../"${1}" ;;
*.lzma) unlzma ../"$1" ;; *.lzma) unlzma ../"${1}" ;;
*.bz2) bunzip2 ../"$1" ;; *.bz2) bunzip2 ../"${1}" ;;
*.rar) unrar x -ad ../"$1" ;; *.rar) unrar x -ad ../"${1}" ;;
*.gz) gunzip ../"$1" ;; *.gz) gunzip ../"${1}" ;;
*.tar) tar xf ../"$1" ;; *.tar) tar xf ../"${1}" ;;
*.tbz2) tar xjf ../"$1" ;; *.tbz2) tar xjf ../"${1}" ;;
*.tgz) tar xzf ../"$1" ;; *.tgz) tar xzf ../"${1}" ;;
*.zip) unzip ../"$1" ;; *.zip) unzip ../"${1}" ;;
*.Z) uncompress ../"$1" ;; *.Z) uncompress ../"${1}" ;;
*.7z) 7z x ../"$1" ;; *.7z) 7z x ../"${1}" ;;
*.xz) unxz ../"$1" ;; *.xz) unxz ../"${1}" ;;
*.exe) cabextract ../"$1" ;; *.exe) cabextract ../"${1}" ;;
*) echo "extract: error: '$1' - unknown archive method" >&2 ;; *) echo "extract: error: '${1}' - unknown archive method" >&2 ;;
esac esac
) )
mcd() { mcd() {
if [ -z "$1" ]; then if [ -z "${1}" ]; then
cd "$(mktemp -td "${USER:-user}.XXXX")" cd "$(mktemp -td "${USER:-user}.XXXX")"
else else
mkdir -p "$1" && cd "$1" mkdir -p "${1}" && cd "${1}"
fi fi
} }
open() { open() {
test -e "$1" || return 1 test -e "${1}" || return 1
nohup xdg-open "$1" 1>/dev/null 2>&1 & nohup xdg-open "${1}" 1>/dev/null 2>&1 &
} }
split-file() { split-file() {
if [[ ! -e "$1" ]]; then if [[ ! -e "${1}" ]]; then
echo "file '${1}' not found" >&2 echo "file '${1}' not found" >&2
return 1 return 1
fi fi
@ -145,7 +133,7 @@ split-file() {
mkdir "${1}.splitted" mkdir "${1}.splitted"
cd "${1}.splitted" cd "${1}.splitted"
split -d -b "$size" "../${1}" split -d -b "${size}" "../${1}"
} }
json() { json() {
@ -174,28 +162,26 @@ rmt() {
for filename in "$@" for filename in "$@"
do do
local filename_out_path local filename_out_path
filename_out_path="${trash}$(realpath "$filename")" filename_out_path="${trash}$(realpath "${filename}")"
readonly filename_out_path readonly filename_out_path
mkdir -p "$(dirname "$filename_out_path")" mkdir -p "$(dirname "${filename_out_path}")"
mv "$filename" "${filename_out_path}_$(date +%s)" mv "${filename}" "${filename_out_path}_$(date +%s)"
done done
} }
showtips() { showtips() {
local -r tips_dir="${HOME}/.tips" local -r tips_dir="${HOME}/.tips"
if [ ! -d "$tips_dir" ]; then if [ ! -d "${tips_dir}" ]; then
mkdir "$tips_dir" 2>/dev/null mkdir "${tips_dir}" 2>/dev/null
git init "$tips_dir" git init "${tips_dir}"
fi fi
local content
local filename local filename
for filename in "$tips_dir"/* for filename in "${tips_dir}"/*
do do
content="$(cat "$filename")" cat "${filename}"
echo "$content"
done done
return 0 return 0
} }
@ -204,11 +190,6 @@ most-often-commands() {
history 0 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn history 0 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn
} }
# Enable X11Forwarding and disable mux session
sshx() {
ssh -X -o ControlMaster=no -o ControlPath=none "$@"
}
django-create-project() ( django-create-project() (
set -ue set -ue
local -r project_name="${1}" local -r project_name="${1}"
@ -283,7 +264,7 @@ f() {
mirror-site() ( mirror-site() (
set -eu set -eu
local -r name=$1; shift local -r name="${1}"; shift
mkdir -p "${name}" && cd "${name}" mkdir -p "${name}" && cd "${name}"
@ -301,7 +282,7 @@ mirror-site() (
-l inf \ -l inf \
--no-remove-listing \ --no-remove-listing \
--no-parent \ --no-parent \
--user-agent "$user_agent" \ --user-agent "${user_agent}" \
--reject '*.woff*,*.ttf,*.eot,*.js' \ --reject '*.woff*,*.ttf,*.eot,*.js' \
--tries 10 \ --tries 10 \
$@ $@

View File

@ -4,23 +4,35 @@ _so() {
[ -r "${1}" ] && [ -f "${1}" ] && source "${1}" [ -r "${1}" ] && [ -f "${1}" ] && source "${1}"
} }
_sod() {
local -r dir_to_source="${1}"
if [ -d "${dir_to_source}" ] && [ -n "$(ls "${dir_to_source}")" ]; then
local filename
for filename in $(ls "${dir_to_source}" | sort -n)
do
source "${dir_to_source}/${filename}"
done
fi
}
_so "${ZDOTDIR}/export" source "${ZDOTDIR}/export"
source "${ZDOTDIR}/options"
source "${ZDOTDIR}/completion"
source "${ZDOTDIR}/history"
source "${ZDOTDIR}/prompt"
source "${ZDOTDIR}/other"
source "${ZDOTDIR}/aliases"
source "${ZDOTDIR}/functions"
_so "${ZDOTDIR}/hosts/${HOST}" _so "${ZDOTDIR}/hosts/${HOST}"
_so "${ZDOTDIR}/options" source "${ZDOTDIR}/plugin"
_so "${ZDOTDIR}/completion"
_so "${ZDOTDIR}/history"
_so "${ZDOTDIR}/prompt"
_so "${ZDOTDIR}/other"
_so "${ZDOTDIR}/aliases"
_so "${ZDOTDIR}/functions"
_so "${ZDOTDIR}/plugin" source "${ZDOTDIR}/mappings"
_so "${ZDOTDIR}/mappings" source "${ZDOTDIR}/calculator"
_so "${ZDOTDIR}/calculator"
_sod "${ZDOTDIR}/zshrc.d" _sod "${ZDOTDIR}/zshrc.d"