diff --git a/.README.md b/.README.md index b5c82b1..976217e 100644 --- a/.README.md +++ b/.README.md @@ -497,6 +497,7 @@ Command|Description Command|Description ---|--- `find_ext`|Find all file extensions recursively. +`find_function`|Find all dotfiles functions. ## Fix. @@ -536,6 +537,13 @@ Command|Description `gg `|Get git repo from my own git. `ggc`|Run git garbage collection. +## Help. + +Command|Description +---|--- +`help `|Get help for a dotfile function. +`h `|Same as `help`. + ## Ls. Command|Description diff --git a/.bashrc b/.bashrc index 7805ef2..f3ac851 100644 --- a/.bashrc +++ b/.bashrc @@ -1,21 +1,18 @@ -#!/bin/bash - # If not running interactively, don't do anything. [[ "$-" != *i* ]] && return -#home="/var/home/voronind" -home="${HOME}" -module="${home}/.config/bash/module/*.sh" +# Src system bashrc. +[[ -f /etc/bashrc ]] && source /etc/bashrc -# src default -if [[ -f /etc/bashrc ]]; then - source /etc/bashrc -fi - -# src custom -for file in ${module}; do - source "${file}" +# Src custom modules. +for module in ${HOME}/.config/bash/module/*.sh; do + source "${module}" done -# alias to reload -alias bashrc="source ~/.bashrc" +# Alias to reload. +function bashrc() { + source ~/.bashrc +} + +# Export all functions. +export -f $(find_functions | tr '\n' ' ') diff --git a/.config/bash/module/android.sh b/.config/bash/module/android.sh index 29b9d1a..435f332 100644 --- a/.config/bash/module/android.sh +++ b/.config/bash/module/android.sh @@ -1,3 +1,3 @@ -emulator() { +function emulator() { ${ANDROID_SDK_ROOT}/emulator/emulator -avd default &> /dev/null & disown } diff --git a/.config/bash/module/archive.sh b/.config/bash/module/archive.sh index 79be19b..926a899 100644 --- a/.config/bash/module/archive.sh +++ b/.config/bash/module/archive.sh @@ -1,11 +1,12 @@ -_ARCHIVE_PATTERN="_[0-9]{12}-[[:alnum:]]{40}.t[xg]z" -_ARCHIVE_DATE() { +export _ARCHIVE_PATTERN="_[0-9]{12}-[[:alnum:]]{40}.t[xg]z" + +function _ARCHIVE_DATE() { date +%Y%m%d%H%M } # archive file with maximum compression and checksum. # usage: archive [FILES] -archive() { +function archive() { local IFS=$'\n' local targets=("${@}") local count=0 @@ -52,7 +53,7 @@ archive() { # archive file with minimal compression and checksum. # usage: archive_fast [FILES] -archive_fast() { +function archive_fast() { local IFS=$'\n' local targets=("${@}") local count=0 @@ -99,7 +100,7 @@ archive_fast() { # check archive hashes. # usage: archive_check [FILES] -archive_check() { +function archive_check() { local IFS=$'\n' local targets=("${@}") local total=${#} @@ -143,7 +144,7 @@ archive_check() { # Delete old versions of an archives. # Usage: archive_prune [NAME] -archive_prune() { +function archive_prune() { local IFS=$'\n' local targets=("${@}") local count=0 @@ -194,7 +195,7 @@ archive_prune() { # extract previously created archive with checksum validation. # usage: unarchive [FILES] -unarchive() { +function unarchive() { local IFS=$'\n' local targets=("${@}") local count=0 @@ -260,7 +261,7 @@ unarchive() { # rename archive. if no name specified, it simplifies archive's name. # usage: archive_name [ARCHIVE] [NAME] -archive_name() { +function archive_name() { local IFS=$'\n' local targets="${1}" local name="${2}" @@ -323,7 +324,7 @@ archive_name() { } # convert an old archive to a new format. TODO: remove me after some time when there won't be any old archives. -archive_convert() { +function archive_convert() { local IFS=$'\n' local old_format="_[[:alnum:]]{40}.tar.[xg]z" local targets=($(ls | grep -E ${old_format})) @@ -365,7 +366,7 @@ archive_convert() { done } -_archive_parse() { +function _archive_parse() { local input="${1}" local name="${input%_*}" local format="${input##*.}" @@ -379,12 +380,7 @@ _archive_parse() { echo "${format}" } -# export everything, primarily for use with parallel.. -export -f archive archive_fast archive_check unarchive archive_name _ARCHIVE_DATE -export _ARCHIVE_PATTERN - -# autocomplete. -_archive_name() { +function _archive_name() { local IFS=$'\n' COMPREPLY=() @@ -402,7 +398,7 @@ _archive_name() { fi } -_archive_grep() { +function _archive_grep() { _autocomplete_grep ${_ARCHIVE_PATTERN} } diff --git a/.config/bash/module/autocomplete.sh b/.config/bash/module/autocomplete.sh index 08805af..47b7f85 100644 --- a/.config/bash/module/autocomplete.sh +++ b/.config/bash/module/autocomplete.sh @@ -1,7 +1,7 @@ # bash autocomplete. # usage: _foo() { _autocomplete "{foo,bar}" } ; complete -F _foo foo # there are also options like -o nospace. see man for more info. -_autocomplete() { +function _autocomplete() { local IFS=$'\n' local commands="${*}" @@ -16,7 +16,7 @@ _autocomplete() { } # autocomplete only first argument. -_autocomplete_first() { +function _autocomplete_first() { local IFS=$'\n' local commands="${*}" @@ -33,7 +33,7 @@ _autocomplete_first() { } # Autocomplete by grepping file names. -_autocomplete_grep() { +function _autocomplete_grep() { local IFS=$'\n' COMPREPLY=() @@ -47,7 +47,7 @@ _autocomplete_grep() { } # autocomplete nested program. -_autocomplete_nested() { +function _autocomplete_nested() { # local IFS=$'\n' local cur prev words cword split i _init_completion -s || return diff --git a/.config/bash/module/battery.sh b/.config/bash/module/battery.sh index 30985ab..ebccd30 100644 --- a/.config/bash/module/battery.sh +++ b/.config/bash/module/battery.sh @@ -1,4 +1,4 @@ # check battery level. -battery_level() { +function battery_level() { cat /sys/class/power_supply/BAT*/capacity } diff --git a/.config/bash/module/bootstrap.sh b/.config/bash/module/bootstrap.sh index 25a1b01..721a1f2 100644 --- a/.config/bash/module/bootstrap.sh +++ b/.config/bash/module/bootstrap.sh @@ -1,11 +1,11 @@ # install Cargo/Rust. -bootstrap_rust() { +function bootstrap_rust() { curl https://sh.rustup.rs -sSf | sh rustup component add rust-analyzer } # install TeXLive. -bootstrap_texlive() { +function bootstrap_texlive() { cd /tmp wget https://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz tar -xf install-tl-unx.tar.gz @@ -15,13 +15,13 @@ bootstrap_texlive() { } # install grub theme. -bootstrap_grub() { +function bootstrap_grub() { wget -O- https://github.com/shvchk/fallout-grub-theme/raw/master/install.sh | bash echo 'GRUB_HIDDEN_TIMEOUT=' >> /etc/default/grub grub2-mkconfig -o /etc/grub2.cfg } # install ffmpeg. -bootstrap_ffmpeg() { +function bootstrap_ffmpeg() { flatpak install org.kde.kdenlive } diff --git a/.config/bash/module/cd.sh b/.config/bash/module/cd.sh index 091160e..63b8fab 100644 --- a/.config/bash/module/cd.sh +++ b/.config/bash/module/cd.sh @@ -1,6 +1,6 @@ # CD (back) to directory. # Usage: cdd -cdd() { +function cdd() { local target="${1}" local array local result diff --git a/.config/bash/module/checksum.sh b/.config/bash/module/checksum.sh index 24fa8a3..00054a7 100644 --- a/.config/bash/module/checksum.sh +++ b/.config/bash/module/checksum.sh @@ -1,6 +1,6 @@ # Save file checksums. # Usage: checksum_create [FILES] -checksum_create() { +function checksum_create() { local IFS=$'\n' local targets=("${@}") local count=0 @@ -46,7 +46,7 @@ checksum_create() { } # Check stored values against actual files. -checksum_check() { +function checksum_check() { local IFS=$'\n' local targets=("${@}") local count=0 @@ -95,17 +95,17 @@ checksum_check() { fi } -checksum() { +function checksum() { find -type f | parallel -j $(_core_count) -- sha1sum {} >> checksum.sha1 } -_checksum_create() { +function _checksum_create() { local path="${1%/*}" local name="${1##*/}" sha1sum "${path}/${name}" > "${path}/.${name}.sha1" } -_checksum_check() { +function _checksum_check() { local file="${1##*\ \ }" local stored="${1%%\ \ *}" @@ -124,6 +124,3 @@ _checksum_check() { return 0 } - -export -f checksum_create checksum_check -export -f _checksum_check _checksum_create diff --git a/.config/bash/module/chmod.sh b/.config/bash/module/chmod.sh index e5b9419..85b5b9b 100644 --- a/.config/bash/module/chmod.sh +++ b/.config/bash/module/chmod.sh @@ -1,4 +1,4 @@ # add executable flag to file. -x() { +function x() { chmod +x "${@}" } diff --git a/.config/bash/module/color.sh b/.config/bash/module/color.sh index 53d3c39..f83c0b3 100644 --- a/.config/bash/module/color.sh +++ b/.config/bash/module/color.sh @@ -19,6 +19,6 @@ export color_yellow="\033[0;33m" export color_byellow="\033[1;33m" # print all available colors with their names colored in corresponding color. -color_test() { +function color_test() { echo -e "${color_default}color_default\n${color_blue}color_blue\n${color_bblue}color_bblue\n${color_cyan}color_cyan\n${color_bcyan}color_bcyan\n${color_green}color_green\n${color_bgreen}color_bgreen\n${color_purple}color_purple\n${color_bpurple}color_bpurple\n${color_red}color_red\n${color_bred}color_bred\n${color_white}color_white\n${color_bwhite}color_bwhite\n${color_yellow}color_yellow\n${color_byellow}color_byellow" } diff --git a/.config/bash/module/cp.sh b/.config/bash/module/cp.sh index 195e7bd..25a17b1 100644 --- a/.config/bash/module/cp.sh +++ b/.config/bash/module/cp.sh @@ -1,24 +1,24 @@ # replace default cp with rsync. -cp() { +function cp() { rsync -ahP --chmod=u+w -- "${@}" } # copy and also merge all changes (delete dst files that do not exist in src). -cp_merge() { +function cp_merge() { rsync -ahP --chmod=u+w --delete -- "${@}" } # copy by creating hardlinks. -cp_link() { +function cp_link() { /usr/bin/cp -lr -- "${@}" } # default cp, a.k.a builtin cp. -bcp() { +function bcp() { /usr/bin/cp "${@}" } # cp_merge without writing anything. -cp_test() { +function cp_test() { rsync -ahP --chmod=u+w --delete -n -- "${@}" } diff --git a/.config/bash/module/date.sh b/.config/bash/module/date.sh index 5f92df8..7bf68c8 100644 --- a/.config/bash/module/date.sh +++ b/.config/bash/module/date.sh @@ -1,9 +1,9 @@ # print today date in yyyyMMdd format. -today() { +function today() { date +%Y%m%d } # current day of week number. -dow() { +function dow() { date +%u } diff --git a/.config/bash/module/dconf.sh b/.config/bash/module/dconf.sh index 1e8f5c2..947d21a 100644 --- a/.config/bash/module/dconf.sh +++ b/.config/bash/module/dconf.sh @@ -1,9 +1,9 @@ -_GDCONF_PATH="${HOME}/.config/linux/gnome.dconf" +export _GDCONF_PATH="${HOME}/.config/linux/gnome.dconf" -dconf_load() { +function dconf_load() { sed -i -e s/voronind/$(whoami)/g ${_GDCONF_PATH} ; dconf load / < ${_GDCONF_PATH} } -dconf_save() { +function dconf_save() { dconf dump / > gnome.dconf } diff --git a/.config/bash/module/disk.sh b/.config/bash/module/disk.sh index a9992c2..dd108e7 100644 --- a/.config/bash/module/disk.sh +++ b/.config/bash/module/disk.sh @@ -1,9 +1,9 @@ # show only physical drive sizes. -df() { +function df() { df --si | sed -e '1p' -e "/^\/dev\//!d" } # show combined size in SI. -du() { +function du() { du -sh --si -- "${@}" } diff --git a/.config/bash/module/docker.sh b/.config/bash/module/docker.sh index d66900d..9d0b41d 100644 --- a/.config/bash/module/docker.sh +++ b/.config/bash/module/docker.sh @@ -1,91 +1,90 @@ # show container's volumes. -docker_volumes() { +function docker_volumes() { docker inspect -f '{{ .Mounts }}' "${@}" } # Check if any container exited. -docker_health() { +function docker_health() { docker ps -a | grep Exited } # prune everything. -docker_prune() { +function docker_prune() { docker system prune --volumes --all } # Docker compose shortcut. # Usage: dc [SERVICE] -dc() { +function dc() { docker compose "${@}" } # Docker compose up. -dcu() { +function dcu() { docker compose up -d "${@}" } # Docker compose down. -dcd() { +function dcd() { docker compose down "${@}" } # Docker compose pull. -dcp() { +function dcp() { docker compose pull "${@}" } # Docker compose logs. -dcl() { +function dcl() { docker compose logs -f "${@}" } # Docker compose restart. -dcr() { +function dcr() { docker compose restart "${@}" } # Docker compose stop. -dcs() { +function dcs() { docker compose stop "${@}" } # Docker compose down & up specified services. # Usage: dcdu [SERVICES] -dcdu() { +function dcdu() { dcd "${@}" dcu "${@}" } # Docker compose pull & up specified services. # Usage: dcpu [SERVICES] -dcpu() { +function dcpu() { dcp "${@}" dcu "${@}" } # Docker compose up & attach to logs for specified services. # Usage: dcul [SERVICES] -dcul() { +function dcul() { dcu "${@}" && dcl "${@}" } # Find out container's IP address. # Usage: docker_up -docker_ip() { +function docker_ip() { docker inspect -f '\''{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'\' "${1}" | sed "s/^.//" | sed "s/.$//" } # Update all docker images. -docker_update() { +function docker_update() { docker images --format "{{.Repository}}:{{.Tag}}" | xargs -L1 docker pull } -# autocomplete. -_dc_services() { +function _dc_services() { _autocomplete "$(docker compose config --services 2> /dev/null)" } -_dc_containers() { +function _dc_containers() { _autocomplete "$(docker ps --format "\""{{.Names}}"\"")" } diff --git a/.config/bash/module/export.sh b/.config/bash/module/export.sh index 49eb75f..2d18926 100644 --- a/.config/bash/module/export.sh +++ b/.config/bash/module/export.sh @@ -1,5 +1,5 @@ # add all links in ~/app/bin/ -PATH=$( find -L $HOME/app/bin/ -type d -printf ":%p" 2> /dev/null ):$PATH +export PATH=$( find -L $HOME/app/bin/ -type d -printf ":%p" 2> /dev/null ):$PATH # specify locale. #export LC_ALL=C @@ -23,3 +23,6 @@ export ANDROID_SDK_ROOT="$HOME/.android/sdk" export TEXMFVAR="$HOME/app/tex/data/var" export TEXMFCONFIG="$HOME/app/tex/data/config" export TEXMFLOCAL="$HOME/app/tex/data/local" + +# Dotfiles exports. +export DOTFILES_PATH="${HOME}/.config/bash/module" diff --git a/.config/bash/module/ffmpeg.sh b/.config/bash/module/ffmpeg.sh index e544a9e..a035680 100644 --- a/.config/bash/module/ffmpeg.sh +++ b/.config/bash/module/ffmpeg.sh @@ -1,6 +1,6 @@ # mux audio into containers. file names in sound and current dirrectories must match. tmp usage for anime downloads. # usage: ffmpeg_mux_audio -ffmpeg_mux_audio() { +function ffmpeg_mux_audio() { if [[ "$1" = "" ]]; then echo "usage: ffmpeg_mux_audio " return 2 @@ -11,7 +11,7 @@ ffmpeg_mux_audio() { # Mux cover into music file. # Usage: ffmpeg_mux_cover -ffmpeg_mux_cover() { +function ffmpeg_mux_cover() { if [[ "${1}" = "" ]]; then echo "Usage: ffmpeg_mux_cover " return 2 @@ -41,7 +41,7 @@ ffmpeg_mux_cover() { # Change music metadata. # Usage: ffmpeg_music_meta -ffmpeg_music_meta() { +function ffmpeg_music_meta() { if [[ "${1}" = "" ]]; then echo "Usage: ffmpeg_music_meta " return 2 @@ -71,7 +71,7 @@ ffmpeg_music_meta() { rm -d out/ } -_ffprobe_fps() { +function _ffprobe_fps() { local fps=$(ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "${1}") fps="${fps%%/*}" echo "${fps}" @@ -82,9 +82,7 @@ _ffprobe_keyint() { echo $((fps*5)) } -_ffprobe_ba() { +function _ffprobe_ba() { local ba=$(ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "${1}") [[ "${ba}" != "N/A" ]] && echo $((ba/1024)) || echo 128 } - -export -f _ffprobe_keyint _ffprobe_ba _ffprobe_fps diff --git a/.config/bash/module/file.sh b/.config/bash/module/file.sh index 41bd777..c1fc21d 100644 --- a/.config/bash/module/file.sh +++ b/.config/bash/module/file.sh @@ -1,5 +1,5 @@ # Open file/dir in GUI. # Usage: o -o() { +function o() { xdg-open "${@}" } diff --git a/.config/bash/module/find.sh b/.config/bash/module/find.sh index 4116f76..2c20125 100644 --- a/.config/bash/module/find.sh +++ b/.config/bash/module/find.sh @@ -1,5 +1,10 @@ # Find all file extensions. -find_ext() { +function find_ext() { local types=($(find -type f | sed -e "s/.*\///" -e "s/^\.//" -e "/\./!d" -e "s/.*\.//")) echo "${types[@]}" | tr ' ' '\n' | sort -u } + +# Find all module functions. +function find_functions() { + cat "${DOTFILES_PATH}"/* | grep "^function.*()" | sed -e "s/^function //" -e "s/().*//" +} diff --git a/.config/bash/module/fix.sh b/.config/bash/module/fix.sh index 994fecd..0e1bfb5 100644 --- a/.config/bash/module/fix.sh +++ b/.config/bash/module/fix.sh @@ -1,7 +1,7 @@ # fix when ethernet mistakenly detects 100 Mb instead of 1000 Mb. # usage: fix_ethernet_speed # SPEED is one of 10/100/1000 etc. -fix_ethernet_speed() { +function fix_ethernet_speed() { local device="${1}" local speed="${2}" @@ -15,6 +15,6 @@ fix_ethernet_speed() { # fix files wrong sftp password. # alias fix_files_sftp="secret-tool clear protocol sftp server 192.168.1.2" -fix_files_sftp() { +function fix_files_sftp() { secret-tool clear protocol sftp } diff --git a/.config/bash/module/git.sh b/.config/bash/module/git.sh index b3bf7a8..a4d3b31 100644 --- a/.config/bash/module/git.sh +++ b/.config/bash/module/git.sh @@ -1,103 +1,103 @@ # Git push. -gps() { +function gps() { git push "${@}" } # Git force push. -gpsf() { +function gpsf() { git push --force "${@}" } # Git pull. -gpl() { +function gpl() { git pull "${@}" } # Git log. -gl() { +function gl() { git log "${@}" } # Git status. -gs() { +function gs() { git status "${@}" } # Git stash. -gst() { +function gst() { git stash "${@}" } # Git diff. -gd() { +function gd() { git diff "${@}" } # Git commit. -gc() { +function gc() { git commit -m "${@}" } # Git checkout. -gch() { +function gch() { git checkout "${@}" } # Git checkout branch. -gchb() { +function gchb() { git checkout -b "${@}" } # Git branch. -gb() { +function gb() { git branch --all "${@}" } # Git branch delete. -gbd() { +function gbd() { git branch -D "${@}" } # Git branch delete all except current. -gbda() { +function gbda() { git branch | grep -v ^* | xargs git branch -D } # Git fetch all. -gf() { +function gf() { git fetch --all -v -p } # Git tag. -gt() { +function gt() { git tag "${@}" } # Git ignore files. -gi() { +function gi() { git ls-files -ci --exclude-standard -z | xargs -0 git rm --cached } # Git patch create. # Usage: gpc -gpc() { +function gpc() { git diff > "${@}" } # Git patch (apply). # Usage: gp -gp() { +function gp() { git apply "${@}" } # Run git garbage collection. -ggc() { +function ggc() { git gc --aggressive --no-cruft --prune=now } # Preview diff while adding. adds current dir by default. # Usage: ga [FILES] -ga() { +function ga() { local target=${@} if [[ "${target}" = "" ]]; then @@ -110,7 +110,7 @@ ga() { # rebase by X commits or from root. when COUNT is 0 - rebase from root. default is 2. # usage: gr [COMMIT COUNT] -gr() { +function gr() { local base="${1}" # rebase last 2 commits by default. @@ -128,7 +128,7 @@ gr() { # specify git user as Dmitry Voronin with provided email. # usage: gu [EMAIL] -gu() { +function gu() { local name="Dmitry Voronin" local email="${1}" @@ -143,33 +143,33 @@ gu() { # Get my git repo. # Usage: gg -gg() { +function gg() { git clone https://git.voronind.com/voronind/"${1}" } # See diff for a specific commit. # Usage: gdc -gdc() { +function gdc() { git diff "${1}^!" } # Show current branch. -_git_current_branch() { +function _git_current_branch() { git branch --show-current 2> /dev/null } # Show origin's url. -_git_origin_url() { +function _git_origin_url() { git remote get-url origin } # Get this dotfiles url. -_git_dotfiles_url() { +function _git_dotfiles_url() { echo 'https://git.voronind.com/voronind/linux.git' } # Check if current git repo is this dotfiles. -_git_is_dotfiles() { +function _git_is_dotfiles() { # [[ "$(_git_origin_url)" = "$(_git_dotfiles_url)" ]] local dir="${PWD}" @@ -206,7 +206,7 @@ __git_complete gt _git_tag &> /dev/null __git_complete gp _git_apply &> /dev/null __git_complete ga _git_add &> /dev/null -_gu() { +function _gu() { _autocomplete_first account@voronind.com dd.voronin@fsight.ru } diff --git a/.config/bash/module/help.sh b/.config/bash/module/help.sh index 8145e6d..fcc894d 100644 --- a/.config/bash/module/help.sh +++ b/.config/bash/module/help.sh @@ -1 +1,16 @@ -# TODO: Help function that parses help header from modules. +# Get help about bash function. +function help() { + local fun="${1}" + + if [[ $(find_functions | grep "${fun}") = "" ]]; then + echo "Function not found." + return 1 + fi + + sed -e '$s/$/\n/' -s "${DOTFILES_PATH}"/* | sed -n -e "/function ${fun}()/q;p" | tac | sed -n -e "/^$/q;p" | tac | sed -e "s/^# \+//" +} + +# Short for help. +function h() { + help "${@}" +} diff --git a/.config/bash/module/ls.sh b/.config/bash/module/ls.sh index ccb8a42..92361e6 100644 --- a/.config/bash/module/ls.sh +++ b/.config/bash/module/ls.sh @@ -3,18 +3,18 @@ unalias l ll lll llll la lla &> /dev/null unset l ll lll llll la lla &> /dev/null # list files in dir. -l() { +function l() { ls -lhv --si --group-directories-first "$@" } # list last modified files first. -ll() { +function ll() { ls -lhvtr --si "$@" } # list files in tree structure. # usage: lll [DEPTH] [DIRS] -lll() { +function lll() { local IFS=$'\n' local depth="${1}" local target=("${@:2}") @@ -27,19 +27,16 @@ lll() { } # list files recursively. -llll() { +function llll() { ls -RlAhv --si --group-directories-first "$@" } # list all files in dir, incl. hidden files. -la() { +function la() { ls -lAh --si --group-directories-first "$@" } # list all files in dir, incl. hidden files, sorted by mtime. -lla() { +function lla() { ls -lAhtr --si "$@" } - -# export. -export -f l ll lll llll la lla diff --git a/.config/bash/module/name.sh b/.config/bash/module/name.sh index 4f769c5..c2fd94f 100644 --- a/.config/bash/module/name.sh +++ b/.config/bash/module/name.sh @@ -1,6 +1,6 @@ # rename files to strip all special characters. # usage: name [FILES] -name() { +function name() { local IFS=$'\n' local targets=("${@}") local count=0 @@ -64,7 +64,7 @@ name() { # rename all files to their hashes while keeping extensions. # usage: name_hash [FILES] -name_hash() { +function name_hash() { local IFS=$'\n' local targets=("${@}") local count=0 @@ -124,7 +124,7 @@ name_hash() { # check hashes for renamed files. # usage: name_hash_check [FILES] -name_hash_check() { +function name_hash_check() { local IFS=$'\n' local targets=("${@}") # target file(s). local total=${#} # total to process. @@ -166,7 +166,7 @@ name_hash_check() { # rename files for Jellyfin series, i.e. Episode S01E01.mkv # usage: name_series [FILES] -name_series() { +function name_series() { local IFS=$'\n' local season="${1}" local targets=("${@:2}") @@ -220,7 +220,7 @@ name_series() { # rename files for Kavita manga format. # usage: name_manga [FILES] -name_manga() { +function name_manga() { local IFS=$'\n' local season="${1}" local targets=("${@:2}") @@ -275,7 +275,7 @@ name_manga() { # rename files for new extension. # usage: name_ext [FILES] -name_ext() { +function name_ext() { local IFS=$'\n' local extension="${1}" local targets=("${@:2}") @@ -327,7 +327,7 @@ name_ext() { # Change file prefixes. # Usage: name_prefix [FILES] -name_prefix() { +function name_prefix() { local IFS=$'\n' local old="${1}" local new="${2}" @@ -374,7 +374,7 @@ name_prefix() { # Change file postfix. # Usage: name_postfix [FILES] -name_postfix() { +function name_postfix() { local IFS=$'\n' local old="${1}" local new="${2}" @@ -420,7 +420,7 @@ name_postfix() { } # Replace part of the name. -name_replace() { +function name_replace() { local IFS=$'\n' local old="${1}" local new="${2}" @@ -463,8 +463,4 @@ name_replace() { echo -e "${color_bred}Failed: ${failed}.${color_default}" false fi - } - -# export for parallel. -export -f name name_hash name_hash_check name_ext diff --git a/.config/bash/module/ncdu.sh b/.config/bash/module/ncdu.sh index 19a2169..e2c3289 100644 --- a/.config/bash/module/ncdu.sh +++ b/.config/bash/module/ncdu.sh @@ -1,4 +1,4 @@ # search only on current filesystem. -ncdu() { +function ncdu() { ncdu -x -- "${@}" } diff --git a/.config/bash/module/notify.sh b/.config/bash/module/notify.sh index 7f922d6..7e14967 100644 --- a/.config/bash/module/notify.sh +++ b/.config/bash/module/notify.sh @@ -1,9 +1,9 @@ # send Telegram notification. -notify() { +function notify() { curl -X POST -H 'Content-Type: Application/json' -d "{\"chat_id\":\"155897358\",\"text\":\"$1\"}" https://api.telegram.org/bot2046849441:AAHQpjRK4xpL8tEUyN4JTSDUUze4J0wSIy4/sendMessage &> /dev/null } # send silent Telegram notification. -notify_silent() { +function notify_silent() { curl -X POST -H 'Content-Type: Application/json' -d "{\"chat_id\":\"155897358\",\"text\":\"$1\",\"disable_notification\":\"true\"}" https://api.telegram.org/bot2046849441:AAHQpjRK4xpL8tEUyN4JTSDUUze4J0wSIy4/sendMessage &> /dev/null } diff --git a/.config/bash/module/own.sh b/.config/bash/module/own.sh index e4ff8fd..0a427f2 100644 --- a/.config/bash/module/own.sh +++ b/.config/bash/module/own.sh @@ -1,6 +1,6 @@ # change file ownership to specified user id and restrict access to him. # usage: own [USER] [FILES] -own() { +function own() { local file="${2}" local user="${1}" diff --git a/.config/bash/module/pack.sh b/.config/bash/module/pack.sh index 1143c38..8f33e00 100644 --- a/.config/bash/module/pack.sh +++ b/.config/bash/module/pack.sh @@ -1,8 +1,8 @@ -_UNPACK_SUPPORTED=".tar$|.tgz$|.txz$|.tar.gz$|.tar.xz$|.zip$|.iso$|.rar$" +export _UNPACK_SUPPORTED=".tar$|.tgz$|.txz$|.tar.gz$|.tar.xz$|.zip$|.iso$|.rar$" # Pack files into desired format. # Usage: pack [FILES] -pack() { +function pack() { local IFS=$'\n' local output="${1}" local targets=("${@:2}") @@ -61,7 +61,7 @@ pack() { # attempt to unpack everything. # usage: unpack [FILES] -unpack() { +function unpack() { local IFS=$'\n' local targets=("${@}") local count=0 @@ -140,65 +140,62 @@ unpack() { fi } -_pack_zip() { +function _pack_zip() { zip -9 -r "${@}" } -_pack_tgz() { +function _pack_tgz() { tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | gzip -1 > "${1}" } -_pack_txz() { +function _pack_txz() { tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | xz -9e > "${1}" } -_pack_tar() { +function _pack_tar() { tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') > "${1}" } -_pack_gz() { +function _pack_gz() { pv "${2}" | gzip -1 > "${1}" } -_pack_xz() { +function _pack_xz() { pv "${2}" | xz -9e > "${1}" } -_unpack_zip() { +function _unpack_zip() { unzip "${1}" } -_unpack_7z() { +function _unpack_7z() { 7za x "${1}" } -_unpack_tgz() { +function _unpack_tgz() { pv "${1}" | gzip -d | tar -xf - } -_unpack_txz() { +function _unpack_txz() { pv "${1}" | xz -d | tar -xf - } -_unpack_tar() { +function _unpack_tar() { pv "${1}" | tar -xf - } -_unpack_iso() { +function _unpack_iso() { 7za x "${1}" } -_unpack_rar() { +function _unpack_rar() { unrar x "${1}" } -_unpack_gz() { +function _unpack_gz() { pv "${1}" | gzip -d > "${1%.gz}" } -_unpack_xz() { +function _unpack_xz() { pv "${1}" | xz -d > "${1%.xz}" } - -# export functions. -export -f pack unpack _pack_tgz _pack_txz _pack_tar _pack_zip _unpack_zip _unpack_7z _unpack_tgz _unpack_txz _unpack_tar _unpack_iso _unpack_rar _pack_gz _pack_xz _unpack_gz _unpack_xz diff --git a/.config/bash/module/parse.sh b/.config/bash/module/parse.sh index c173f1c..0550b00 100644 --- a/.config/bash/module/parse.sh +++ b/.config/bash/module/parse.sh @@ -1,6 +1,6 @@ # parse data and output simplified format. # usage: parse_simplify -parse_simplify() { +function parse_simplify() { echo "${*}" | \ sed -e "s/ /_/g" \ -e "s/[^[:alnum:]_-]//g" \ @@ -12,7 +12,7 @@ parse_simplify() { # Parse to CamelCase. # Usage: parse_camel -parse_camel() { +function parse_camel() { local IFS=${IFS}_- local parts=($(parse_simplify ${1})) local result @@ -27,7 +27,7 @@ parse_camel() { # parse data keeping only alphanumeric characters. # usage: parse_alnum -parse_alnum() { +function parse_alnum() { echo "${*}" | \ sed -e "s/[^[:alnum:]]//g" } diff --git a/.config/bash/module/permissions.sh b/.config/bash/module/permissions.sh index 09644e1..6e2c05d 100644 --- a/.config/bash/module/permissions.sh +++ b/.config/bash/module/permissions.sh @@ -1,9 +1,9 @@ # recursively change permissions to allow read sharing with group and others. -perm_share() { +function perm_share() { find . -type d -exec chmod 755 {} \;; find . -type f -exec chmod 644 {} \; } # recursively change permissions to restrict access for group and others. -perm() { +function perm() { find . -type d -exec chmod 700 {} \;; find . -type f -exec chmod 600 {} \; } diff --git a/.config/bash/module/ps.sh b/.config/bash/module/ps.sh index 539397b..a9b3ddd 100644 --- a/.config/bash/module/ps.sh +++ b/.config/bash/module/ps.sh @@ -1,3 +1,3 @@ -ps() { +function ps() { ps aux | grep "${@}" } diff --git a/.config/bash/module/ps1.sh b/.config/bash/module/ps1.sh index c114ad8..b6834f1 100644 --- a/.config/bash/module/ps1.sh +++ b/.config/bash/module/ps1.sh @@ -1,7 +1,7 @@ -PROMPT_COMMAND=(__prompt_command "${PROMPT_COMMAND[@]}") +export PROMPT_COMMAND=(__prompt_command "${PROMPT_COMMAND[@]}") # custom terminal prompt format. -__prompt_command() { +function __prompt_command() { local last_status="${?}" local is_error=false local is_root=false @@ -80,7 +80,7 @@ __prompt_command() { fi } -_ps1error() { +function _ps1error() { local type case ${1} in 1) type="GENERAL" ;; diff --git a/.config/bash/module/recursive.sh b/.config/bash/module/recursive.sh index fd17824..f2461b2 100644 --- a/.config/bash/module/recursive.sh +++ b/.config/bash/module/recursive.sh @@ -1,6 +1,6 @@ # Run something recursively over all directories. # Usage: recursive -recursive() { +function recursive() { local IFS=$'\n' local current="${PWD}" local dirs=$(find -type d) @@ -32,7 +32,7 @@ recursive() { # Run something recursively over all directories. # Usage: recursive1 # TODO: create generic function. -recursive1() { +function recursive1() { local IFS=$'\n' local current="${PWD}" local dirs=$(find -mindepth 1 -maxdepth 1 -type d) diff --git a/.config/bash/module/rust.sh b/.config/bash/module/rust.sh index 2aba02a..308323b 100644 --- a/.config/bash/module/rust.sh +++ b/.config/bash/module/rust.sh @@ -1,4 +1,4 @@ # Open Rust book. -rust_book() { +function rust_book() { rustup docs --book } diff --git a/.config/bash/module/su.sh b/.config/bash/module/su.sh index 180c372..62f3975 100644 --- a/.config/bash/module/su.sh +++ b/.config/bash/module/su.sh @@ -1,4 +1,4 @@ # I'm lazy af. -s() { +function s() { su } diff --git a/.config/bash/module/terminal.sh b/.config/bash/module/terminal.sh index e73a57a..5b914f6 100644 --- a/.config/bash/module/terminal.sh +++ b/.config/bash/module/terminal.sh @@ -1,4 +1,4 @@ -tsize() { +function tsize() { local width=$(tput cols) local height=$(tput lines) echo "${width}x${height}" diff --git a/.config/bash/module/tmp.sh b/.config/bash/module/tmp.sh index 825cb91..f48962b 100644 --- a/.config/bash/module/tmp.sh +++ b/.config/bash/module/tmp.sh @@ -1,5 +1,5 @@ # CD into host's primary tmp dir. -tmp() { +function tmp() { local host="${HOSTNAME}" local tmp_path diff --git a/.config/bash/module/tmux.sh b/.config/bash/module/tmux.sh index 047b851..78829c8 100644 --- a/.config/bash/module/tmux.sh +++ b/.config/bash/module/tmux.sh @@ -1,5 +1,5 @@ # create/attach to named session. -ta() { +function ta() { local name="$1" # set default name. @@ -12,18 +12,18 @@ ta() { } # detach. -td() { +function td() { tmux detach-client } # list. -tl() { +function tl() { tmux list-sessions } # Rename current session. Uses current dir name by default. # Usage: trn [NAME] -trn() { +function trn() { local name="${1}" [[ "${name}" = "" ]] && name="${PWD##*/}" @@ -33,7 +33,7 @@ trn() { # Assign name (to window). Uses current dir name by default. # Usage: tn [NAME] -tn() { +function tn() { local name="${1}" [[ "${name}" = "" ]] && name="${PWD##*/}" @@ -42,7 +42,7 @@ tn() { } # kill specified session or default one. -tk() { +function tk() { # set default name. if [[ "${1}" = "" ]]; then 1="main" @@ -55,7 +55,7 @@ tk() { } # kill all sessions. -tka() { +function tka() { local sessions=$(tmux list-sessions | sed -e 's/:.*//') for session in $sessions; do @@ -64,15 +64,15 @@ tka() { } # autocompletes. -_complete_tmux_session() { +function _complete_tmux_session() { _autocomplete_first "$(tmux list-sessions 2> /dev/null | sed -e 's/:.*//')" } -_complete_tmux_sessions() { +function _complete_tmux_sessions() { _autocomplete "$(tmux list-sessions 2> /dev/null | sed -e 's/:.*//')" } -_complete_tmux_name() { +function _complete_tmux_name() { _autocomplete_first "${PWD##*/}"$'\n'$(ls --classify | grep /$ | sed -e 's/\/$//') } diff --git a/.config/bash/module/todo.sh b/.config/bash/module/todo.sh index 5b7e188..e14e4f5 100644 --- a/.config/bash/module/todo.sh +++ b/.config/bash/module/todo.sh @@ -1,4 +1,4 @@ # Open ~/.todo.md file. -todo() { +function todo() { vi ~/.todo.md } diff --git a/.config/bash/module/toolbx.sh b/.config/bash/module/toolbx.sh index de6da5a..5df9307 100644 --- a/.config/bash/module/toolbx.sh +++ b/.config/bash/module/toolbx.sh @@ -1,6 +1,6 @@ # attach/create toolbx container with default or specified name. # usage: tb [NAME] -tba() { +function tba() { local name="${1}" # set default name. @@ -38,7 +38,7 @@ tba() { # remove toolbx container with default or specified name. # usage: tbk [NAME] -tbk() { +function tbk() { local name="${1}" # set default name. @@ -52,7 +52,7 @@ tbk() { # install rpm-fusion repository. # usage: tb_rpmfusion [NAME] -tb_rpmfusion() { +function tb_rpmfusion() { local name="${1}" # set default name. @@ -65,12 +65,12 @@ tb_rpmfusion() { } # list all containers. -tbl() { +function tbl() { toolbox list -c } # autocomplete. -_tb_containers() { +function _tb_containers() { _autocomplete_first "$(toolbox list -c | sed -e '1d' | awk '{ print $2 }')" } diff --git a/.config/bash/module/transcode.sh b/.config/bash/module/transcode.sh index d0d9544..b39be7f 100644 --- a/.config/bash/module/transcode.sh +++ b/.config/bash/module/transcode.sh @@ -1,6 +1,6 @@ # Convert between different formats. # Usage: transcode [FILES] -transcode() { +function transcode() { local IFS=$'\n' local targets=("${@:2}") local format="${1}" @@ -90,32 +90,32 @@ transcode() { fi } -_transcode_gz-xz() { +function _transcode_gz-xz() { [[ -f "${2}" ]] && return 1 pv "${1}" | gzip -d | xz -9e > "${2}" } -_transcode_xz-gz() { +function _transcode_xz-gz() { [[ -f "${2}" ]] && return 1 pv "${1}" | xz -d | gzip -1 > "${2}" } -_transcode_mp3() { +function _transcode_mp3() { ffmpeg -n -i "${1}" -c:a libmp3lame -b:a 320k -f mp3 "${2}" } -_transcode_flac() { +function _transcode_flac() { ffmpeg -n -i "${1}" -c:a flac -f flac "${2}" } -_transcode_mka() { +function _transcode_mka() { local braudio=$(_ffprobe_ba "${1}") [[ ${braudio} -gt 128 ]] && braudio=128 ffmpeg -n -i "${1}" -ac 2 -c:a libopus -b:a ${braudio}k -vn "${2}" } -_transcode_mkv() { +function _transcode_mkv() { local keyint=$(_ffprobe_keyint "${1}") local braudio=$(_ffprobe_ba "${1}") [[ ${braudio} -gt 128 ]] && braudio=128 @@ -123,6 +123,3 @@ _transcode_mkv() { # ffmpeg -n -i "${1}" -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 30 -svtav1-params "fast-decode=1:tune=0" -preset 8 -pix_fmt yuv420p10le -g ${keyint} -vf "scale=-2:min'(1080,ih)'" "${2}" ffmpeg -n -i "${1}" -map 0 -map -v -map V -map -t -c:s srt -ac 2 -c:a libopus -b:a ${braudio}k -c:v libsvtav1 -crf 30 -svtav1-params "fast-decode=1:tune=0" -pix_fmt yuv420p10le -g ${keyint} -vf "scale=-2:min'(1080,ih)'" "${2}" } - -# Export. -export -f transcode _transcode_gz-xz _transcode_xz-gz _transcode_mp3 _transcode_flac _transcode_mka _transcode_mkv diff --git a/.config/bash/module/try.sh b/.config/bash/module/try.sh index 9e1283f..8bb1a06 100644 --- a/.config/bash/module/try.sh +++ b/.config/bash/module/try.sh @@ -1,5 +1,5 @@ # retry command every 2 sec until it completes. -try() { +function try() { local result=-1 while [ "$result" != 0 ]; do diff --git a/.config/bash/module/util.sh b/.config/bash/module/util.sh index 8625b1e..854413d 100644 --- a/.config/bash/module/util.sh +++ b/.config/bash/module/util.sh @@ -1,4 +1,4 @@ # Get the number of avaialble cores (threads). -_core_count() { +function _core_count() { cat /proc/cpuinfo | grep ^processor | wc -l } diff --git a/.config/bash/module/vdl.sh b/.config/bash/module/vdl.sh index 1fcb1f3..ef5fc7c 100644 --- a/.config/bash/module/vdl.sh +++ b/.config/bash/module/vdl.sh @@ -1,6 +1,6 @@ # Download video from URL in background. When no [LINK] specified, it tries to update previously downloaded link. # Usage: vdl [LINK] -vdl() { +function vdl() { # Check that ffmpeg and ffprobe are available. if [[ "$(ffmpeg -version)" = "" ]] || [[ "$(ffprobe -version)" = "" ]]; then echo -e "${color_red}ffmpeg and ffprobe are required.${color_default}" @@ -26,12 +26,12 @@ vdl() { yt-dlp -f 'bestvideo[height<=?1081]+bestaudio/best' --download-archive index.txt --embed-thumbnail --embed-subs --write-auto-subs --embed-metadata --merge-output-format mkv -cio '%(playlist_index)000006d_%(id)s.%(ext)s' ${target} # || _vdl_retry } -vdl_vk() { +function vdl_vk() { vdl --format mp4 "${@}" } # Temporary fix for crashes. -_vdl_retry() { +function _vdl_retry() { for file in *.part; do local number="${file%%_*}" rm ${number}* @@ -40,6 +40,6 @@ _vdl_retry() { } # download all videos from file. -vdl_file() { +function vdl_file() { vdl -a "${@}" } diff --git a/.config/bash/module/vi.sh b/.config/bash/module/vi.sh index 5a44ab6..78ecedc 100644 --- a/.config/bash/module/vi.sh +++ b/.config/bash/module/vi.sh @@ -1,9 +1,9 @@ # default vim. -vi() { +function vi() { vi -c "filetype plugin indent on" -c "set autoindent" -c "set tabstop=2" -c "set shiftwidth=2" -c "set expandtab" -c "set number" -- "${@}" } # neovim. -v() { +function v() { nvim -- "${@}" } diff --git a/.config/bash/module/wallpaper.sh b/.config/bash/module/wallpaper.sh index c7c488e..84a20fd 100644 --- a/.config/bash/module/wallpaper.sh +++ b/.config/bash/module/wallpaper.sh @@ -1,5 +1,5 @@ # set specified file as a wallpaper. -wallpaper() { +function wallpaper() { path_wallpaper=~/.local/share/backgrounds/background.jpg cp "$1" $path_wallpaper chmod 644 $path_wallpaper diff --git a/.config/bash/module/watch.sh b/.config/bash/module/watch.sh index 0006c0d..4d18ccb 100644 --- a/.config/bash/module/watch.sh +++ b/.config/bash/module/watch.sh @@ -1,12 +1,12 @@ # Watch command output with 2 second interval. # Usage: w -w() { +function w() { watch -n 2 "${@}" } # Watch command output with minimal interval. # Usage: ww -ww() { +function ww() { watch -n 0 "${@}" }