mirror of
https://codeberg.org/hyperreal/dotfiles
synced 2024-11-01 16:53:07 +01:00
315 lines
9.0 KiB
Plaintext
315 lines
9.0 KiB
Plaintext
|
# snakify filenames
|
||
|
snakify () {
|
||
|
emulate -L zsh
|
||
|
local filename=$(basename -- "$1")
|
||
|
local extension="${filename##*.}"
|
||
|
local filename="${filename%.*}"
|
||
|
local new_filename=$(echo ${filename// /_} | tr '-' '_' | tr -cd '[:alnum:]._-')
|
||
|
local new_basename=$(echo "$new_filename.$extension")
|
||
|
mv -i "$1" "$new_basename"
|
||
|
mv -i "$new_basename" "${new_basename:l}"
|
||
|
}
|
||
|
|
||
|
# Print timestamp
|
||
|
tstamp() {
|
||
|
emulate -L zsh
|
||
|
date '+%Y-%m-%d %H:%M:%S'
|
||
|
}
|
||
|
|
||
|
# Find history events by search pattern and list them by date.
|
||
|
whatwhen () {
|
||
|
emulate -L zsh
|
||
|
local usage help ident format_l format_s first_char remain first last
|
||
|
usage='USAGE: whatwhen [options] <searchstring> <search range>'
|
||
|
help='Use `whatwhen -h'\'' for further explanations.'
|
||
|
ident=${(l,${#${:-Usage: }},, ,)}
|
||
|
format_l="${ident}%s\t\t\t%s\n"
|
||
|
format_s="${format_l//(\\t)##/\\t}"
|
||
|
# Make the first char of the word to search for case
|
||
|
# insensitive; e.g. [aA]
|
||
|
first_char=[${(L)1[1]}${(U)1[1]}]
|
||
|
remain=${1[2,-1]}
|
||
|
# Default search range is `-100'.
|
||
|
first=${2:-\-100}
|
||
|
# Optional, just used for `<first> <last>' given.
|
||
|
last=$3
|
||
|
case $1 in
|
||
|
("")
|
||
|
printf '%s\n\n' 'ERROR: No search string specified. Aborting.'
|
||
|
printf '%s\n%s\n\n' ${usage} ${help} && return 1
|
||
|
;;
|
||
|
(-h)
|
||
|
printf '%s\n\n' ${usage}
|
||
|
print 'OPTIONS:'
|
||
|
printf $format_l '-h' 'show help text'
|
||
|
print '\f'
|
||
|
print 'SEARCH RANGE:'
|
||
|
printf $format_l "'0'" 'the whole history,'
|
||
|
printf $format_l '-<n>' 'offset to the current history number; (default: -100)'
|
||
|
printf $format_s '<[-]first> [<last>]' 'just searching within a give range'
|
||
|
printf '\n%s\n' 'EXAMPLES:'
|
||
|
printf ${format_l/(\\t)/} 'whatwhen grml' '# Range is set to -100 by default.'
|
||
|
printf $format_l 'whatwhen zsh -250'
|
||
|
printf $format_l 'whatwhen foo 1 99'
|
||
|
;;
|
||
|
(\?)
|
||
|
printf '%s\n%s\n\n' ${usage} ${help} && return 1
|
||
|
;;
|
||
|
(*)
|
||
|
# -l list results on stout rather than invoking $EDITOR.
|
||
|
# -i Print dates as in YYYY-MM-DD.
|
||
|
# -m Search for a - quoted - pattern within the history.
|
||
|
fc -li -m "*${first_char}${remain}*" $first $last
|
||
|
;;
|
||
|
esac
|
||
|
}
|
||
|
|
||
|
alias x=extract
|
||
|
|
||
|
extract() {
|
||
|
setopt localoptions noautopushd
|
||
|
|
||
|
if (( $# == 0 )); then
|
||
|
cat >&2 <<'EOF'
|
||
|
Usage: extract [-option] [file ...]
|
||
|
|
||
|
Options:
|
||
|
-r, --remove Remove archive after unpacking.
|
||
|
EOF
|
||
|
fi
|
||
|
|
||
|
local remove_archive=1
|
||
|
if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
|
||
|
remove_archive=0
|
||
|
shift
|
||
|
fi
|
||
|
|
||
|
local pwd="$PWD"
|
||
|
while (( $# > 0 )); do
|
||
|
if [[ ! -f "$1" ]]; then
|
||
|
echo "extract: '$1' is not a valid file" >&2
|
||
|
shift
|
||
|
continue
|
||
|
fi
|
||
|
|
||
|
local success=0
|
||
|
local extract_dir="${1:t:r}"
|
||
|
local file="$1" full_path="${1:A}"
|
||
|
case "${file:l}" in
|
||
|
(*.tar.gz|*.tgz) (( $+commands[pigz] )) && { pigz -dc "$file" | tar xv } || tar zxvf "$file" ;;
|
||
|
(*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$file" ;;
|
||
|
(*.tar.xz|*.txz)
|
||
|
tar --xz --help &> /dev/null \
|
||
|
&& tar --xz -xvf "$file" \
|
||
|
|| xzcat "$file" | tar xvf - ;;
|
||
|
(*.tar.zma|*.tlz)
|
||
|
tar --lzma --help &> /dev/null \
|
||
|
&& tar --lzma -xvf "$file" \
|
||
|
|| lzcat "$file" | tar xvf - ;;
|
||
|
(*.tar.zst|*.tzst)
|
||
|
tar --zstd --help &> /dev/null \
|
||
|
&& tar --zstd -xvf "$file" \
|
||
|
|| zstdcat "$file" | tar xvf - ;;
|
||
|
(*.tar) tar xvf "$file" ;;
|
||
|
(*.tar.lz) (( $+commands[lzip] )) && tar xvf "$file" ;;
|
||
|
(*.tar.lz4) lz4 -c -d "$file" | tar xvf - ;;
|
||
|
(*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$file" ;;
|
||
|
(*.gz) (( $+commands[pigz] )) && pigz -dk "$file" || gunzip -k "$file" ;;
|
||
|
(*.bz2) bunzip2 "$file" ;;
|
||
|
(*.xz) unxz "$file" ;;
|
||
|
(*.lrz) (( $+commands[lrunzip] )) && lrunzip "$file" ;;
|
||
|
(*.lz4) lz4 -d "$file" ;;
|
||
|
(*.lzma) unlzma "$file" ;;
|
||
|
(*.z) uncompress "$file" ;;
|
||
|
(*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$file" -d "$extract_dir" ;;
|
||
|
(*.rar) unrar x -ad "$file" ;;
|
||
|
(*.rpm)
|
||
|
command mkdir -p "$extract_dir" && builtin cd -q "$extract_dir" \
|
||
|
&& rpm2cpio "$full_path" | cpio --quiet -id ;;
|
||
|
(*.7z) 7za x "$file" ;;
|
||
|
(*.deb)
|
||
|
command mkdir -p "$extract_dir/control" "$extract_dir/data"
|
||
|
builtin cd -q "$extract_dir"; ar vx "$full_path" > /dev/null
|
||
|
builtin cd -q control; extract ../control.tar.*
|
||
|
builtin cd -q ../data; extract ../data.tar.*
|
||
|
builtin cd -q ..; command rm *.tar.* debian-binary ;;
|
||
|
(*.zst) unzstd "$file" ;;
|
||
|
(*.cab) cabextract -d "$extract_dir" "$file" ;;
|
||
|
(*.cpio) cpio -idmvF "$file" ;;
|
||
|
(*)
|
||
|
echo "extract: '$file' cannot be extracted" >&2
|
||
|
success=1 ;;
|
||
|
esac
|
||
|
|
||
|
(( success = success > 0 ? success : $? ))
|
||
|
(( success == 0 && remove_archive == 0 )) && rm "$full_path"
|
||
|
shift
|
||
|
|
||
|
# Go back to original working directory in case we ran cd previously
|
||
|
builtin cd -q "$pwd"
|
||
|
done
|
||
|
}
|
||
|
|
||
|
# ARCHIVE: Create a tarball from given directory
|
||
|
function create-archive()
|
||
|
{
|
||
|
local archive_name
|
||
|
archive_name="$1.tar.gz"
|
||
|
archive_name=${archive_name/\//}
|
||
|
tar cvfz "$archive_name" "$1"
|
||
|
echo "Created archive $archive_name"
|
||
|
}
|
||
|
compdef _directories create-archive
|
||
|
|
||
|
# Create a /overview/ of all available function()'s; the description for
|
||
|
# each funtion() *must* be the first line above the string `function'!
|
||
|
# Otherwise it wont work.
|
||
|
# Display all function()'s with her descriptions
|
||
|
function funlist()
|
||
|
{
|
||
|
grep -B 1 "^function" $HOME/.zshrc.d/zshfunctions | \
|
||
|
grep -v "^\-\-$" | \
|
||
|
awk '{ if(/^#/) { gsub(/^#[:space:]*/, ""); ht=$0 }; getline; gsub(/ ?\(\)/, ":"); printf("-> %-20s %s\n", $2, ht); }' | \
|
||
|
sort -u -k 3
|
||
|
}
|
||
|
|
||
|
# show directory stack and ask for a dir to switch to
|
||
|
function dstack() {
|
||
|
emulate -L zsh
|
||
|
autoload -U colors
|
||
|
local color=$fg_bold[blue]
|
||
|
integer i=0
|
||
|
dirs -p | while read dir
|
||
|
do
|
||
|
local num="${$(printf "%-4d " $i)/ /.}"
|
||
|
printf " %s $color%s$reset_color\n" $num $dir
|
||
|
(( i++ ))
|
||
|
done
|
||
|
integer dir=-1
|
||
|
read -r 'dir?Jump to directory: ' || return
|
||
|
(( dir == -1 )) && return
|
||
|
if (( dir < 0 || dir >= i ))
|
||
|
then
|
||
|
echo d: no such directory stack entry: $dir
|
||
|
return 1
|
||
|
fi
|
||
|
cd ~$dir
|
||
|
}
|
||
|
|
||
|
# zremovecomp() remove *.zwc files
|
||
|
function zremovecomp() {
|
||
|
local i
|
||
|
for i in ${HOME}/*.zwc(N); do
|
||
|
printf "Removing $i\n"
|
||
|
command rm -f $i
|
||
|
done
|
||
|
}
|
||
|
|
||
|
# grep(1)'ing $HISTFILE
|
||
|
histgrep () { fc -fl -m "*(#i)$1*" 1 | grep -i --color $1 }
|
||
|
|
||
|
# A nicer output of cal(1)
|
||
|
# MISC: Colorize the output of cal(1)
|
||
|
function calendar() {
|
||
|
if [[ ! -f /usr/bin/cal ]] ; then
|
||
|
echo "Please install cal before trying to use it!"
|
||
|
return
|
||
|
fi
|
||
|
if [[ "$#" = "0" ]] ; then
|
||
|
/usr/bin/cal | egrep -C 40 --color "\<$(date +%e| tr -d ' ')\>"
|
||
|
else
|
||
|
/usr/bin/cal $@ | egrep -C 40 --color "\<($(date +%B)|$(date +%e | tr -d ' '))\>"
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
# PROG: invoke this every time when u change .zshrc to recompile it.
|
||
|
function src()
|
||
|
{
|
||
|
autoload -U zrecompile
|
||
|
[ -f ~/.zshrc ] && zrecompile -p ~/.zshrc
|
||
|
[ -f ~/.zcompdump ] && zrecompile -p ~/.zcompdump
|
||
|
[ -f ~/.zcompdump ] && zrecompile -p ~/.zcompdump
|
||
|
[ -f ~/.zshrc.zwc.old ] && command rm -f ~/.zshrc.zwc.old
|
||
|
[ -f ~/.zcompdump.zwc.old ] && command rm -f ~/.zcompdump.zwc.old
|
||
|
source ~/.zshrc
|
||
|
}
|
||
|
|
||
|
# Do an ls after cd
|
||
|
cd() { builtin cd "$@" && ls; }
|
||
|
|
||
|
# Create new directory and enter it
|
||
|
mkd() { mkdir -p "$@" && cd "$_"; }
|
||
|
|
||
|
# pids
|
||
|
pids() { pgrep -a "$@"; }
|
||
|
|
||
|
# zshall manpage
|
||
|
manzsh() { /usr/bin/man zshall | most +/"$1" ; }
|
||
|
|
||
|
# Restart zsh
|
||
|
restart() {
|
||
|
exec $SHELL $SHELL_ARGS "$@"
|
||
|
}
|
||
|
|
||
|
rsrc() { cd && clear && restart; }
|
||
|
|
||
|
# Parse HackerNews feed (https://hnrss.org/newest)
|
||
|
hn() {
|
||
|
python3 <(cat <<EOF
|
||
|
|
||
|
import feedparser
|
||
|
HNFeed = feedparser.parse("https://hnrss.org/newest")
|
||
|
|
||
|
class bcolors:
|
||
|
HEADER = '\033[95m'
|
||
|
ENDC = '\033[0m'
|
||
|
|
||
|
for item in HNFeed.entries:
|
||
|
print(f"{bcolors.HEADER}%s{bcolors.ENDC}" % item.title)
|
||
|
print(item.links[0].href)
|
||
|
print("")
|
||
|
|
||
|
EOF
|
||
|
) | bat -p
|
||
|
}
|
||
|
|
||
|
# fetch open source license
|
||
|
license() {
|
||
|
local base_url
|
||
|
base_url="https://api.github.com/licenses"
|
||
|
|
||
|
local headers
|
||
|
headers="Accept: application/vnd.github.drax-preview+json"
|
||
|
|
||
|
local res
|
||
|
if (( $# == 0 )); then
|
||
|
res=$(curl --silent --header $headers $base_url)
|
||
|
|
||
|
echo "Available licenses:"
|
||
|
echo
|
||
|
echo "$res" | jq ".[].key" | tr -d '"'
|
||
|
fi
|
||
|
|
||
|
local license="$argv[1]"
|
||
|
res=$(curl --silent --header $headers $base_url/$license | jq ."body")
|
||
|
echo -e $res | tr -d '"'
|
||
|
}
|
||
|
|
||
|
# use nmap from podman to check for open SSH ports on local net
|
||
|
if test -x "$(command -v podman)"; then
|
||
|
nmap_ssh() {
|
||
|
if sudo podman image exists localhost/nmap; then
|
||
|
sudo podman run -it --rm \
|
||
|
--cap-add=NET_RAW \
|
||
|
--cap-add=NET_ADMIN \
|
||
|
--network host \
|
||
|
--name nmap \
|
||
|
nmap -sV -p 22 -open 10.0.0.0/24
|
||
|
else
|
||
|
echo "localhost/nmap image does not exist"
|
||
|
fi
|
||
|
}
|
||
|
fi
|
||
|
|
||
|
# vim:set ft=zsh ai et sw=4 ts=4:
|