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
_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() {
local -r filename="${1}"
@ -39,7 +25,8 @@ destroy() {
}
workon() {
source "/opt/pythonenv/${1:-$(basename "$PWD")}/bin/activate"
local -r venvs="${VENVS:-/opt/pythonenv}"
source "${venvs}/${1:-$(basename "${PWD}")}/bin/activate"
}
py() {
@ -51,7 +38,8 @@ py() {
}
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() {
@ -60,7 +48,7 @@ va() {
source "${1}/bin/activate"
return 0
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
fi
@ -73,70 +61,70 @@ va() {
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"
if [[ -f "${activate_venv}" ]]; then
source "${activate_venv}"
return 0
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
fi
}
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>"
return 2
fi
if ! [ -f "$1" ]; then
echo "extract: error: '$1' file does not exist" >&2
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
if [ -e "${name}" ]; then
echo "extract: error: '${name}' exists" >&2
return 1
fi
mkdir "$name" && cd "$name" || return 1
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 ;;
*.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() {
if [ -z "$1" ]; then
if [ -z "${1}" ]; then
cd "$(mktemp -td "${USER:-user}.XXXX")"
else
mkdir -p "$1" && cd "$1"
mkdir -p "${1}" && cd "${1}"
fi
}
open() {
test -e "$1" || return 1
nohup xdg-open "$1" 1>/dev/null 2>&1 &
test -e "${1}" || return 1
nohup xdg-open "${1}" 1>/dev/null 2>&1 &
}
split-file() {
if [[ ! -e "$1" ]]; then
if [[ ! -e "${1}" ]]; then
echo "file '${1}' not found" >&2
return 1
fi
@ -145,7 +133,7 @@ split-file() {
mkdir "${1}.splitted"
cd "${1}.splitted"
split -d -b "$size" "../${1}"
split -d -b "${size}" "../${1}"
}
json() {
@ -174,28 +162,26 @@ rmt() {
for filename in "$@"
do
local filename_out_path
filename_out_path="${trash}$(realpath "$filename")"
filename_out_path="${trash}$(realpath "${filename}")"
readonly filename_out_path
mkdir -p "$(dirname "$filename_out_path")"
mv "$filename" "${filename_out_path}_$(date +%s)"
mkdir -p "$(dirname "${filename_out_path}")"
mv "${filename}" "${filename_out_path}_$(date +%s)"
done
}
showtips() {
local -r tips_dir="${HOME}/.tips"
if [ ! -d "$tips_dir" ]; then
mkdir "$tips_dir" 2>/dev/null
git init "$tips_dir"
if [ ! -d "${tips_dir}" ]; then
mkdir "${tips_dir}" 2>/dev/null
git init "${tips_dir}"
fi
local content
local filename
for filename in "$tips_dir"/*
for filename in "${tips_dir}"/*
do
content="$(cat "$filename")"
echo "$content"
cat "${filename}"
done
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
}
# Enable X11Forwarding and disable mux session
sshx() {
ssh -X -o ControlMaster=no -o ControlPath=none "$@"
}
django-create-project() (
set -ue
local -r project_name="${1}"
@ -283,7 +264,7 @@ f() {
mirror-site() (
set -eu
local -r name=$1; shift
local -r name="${1}"; shift
mkdir -p "${name}" && cd "${name}"
@ -301,7 +282,7 @@ mirror-site() (
-l inf \
--no-remove-listing \
--no-parent \
--user-agent "$user_agent" \
--user-agent "${user_agent}" \
--reject '*.woff*,*.ttf,*.eot,*.js' \
--tries 10 \
$@

View File

@ -4,23 +4,35 @@ _so() {
[ -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}/options"
_so "${ZDOTDIR}/completion"
_so "${ZDOTDIR}/history"
_so "${ZDOTDIR}/prompt"
_so "${ZDOTDIR}/other"
_so "${ZDOTDIR}/aliases"
_so "${ZDOTDIR}/functions"
source "${ZDOTDIR}/plugin"
_so "${ZDOTDIR}/plugin"
source "${ZDOTDIR}/mappings"
_so "${ZDOTDIR}/mappings"
_so "${ZDOTDIR}/calculator"
source "${ZDOTDIR}/calculator"
_sod "${ZDOTDIR}/zshrc.d"