<commit_before># ~/.shell/functions.sh


# test if function to declare already exists
for func in EC man dict ips rename extract histop itunes; do
    if typeset -f $func > /dev/null; then
        echo "WARNING: $func is aleary exists"
    fi
done

# set trap to intercept the non-zero return code of last program
function EC() { echo -e '\e[1;31m'non-zero return code: $?'\e[m'; }
#trap EC ERR

# put a red `!' in front of the PS1 if last command exit with an error code
function prompt_command() {
    if [ $? -ne 0 ]; then
        printf "\e[01;31m!\e[m"
    fi
}

#PROMPT_COMMAND=prompt_command

function man () {
	env \
	LESS_TERMCAP_mb=$'\E[01;31m' \
	LESS_TERMCAP_md=$'\E[01;38;5;74m' \
	LESS_TERMCAP_me=$'\E[00m' \
	LESS_TERMCAP_se=$'\E[00m' \
	LESS_TERMCAP_so=$'\E[32;5;246m' \
	LESS_TERMCAP_ue=$'\E[00m' \
	LESS_TERMCAP_us=$'\E[04;36;5;146m' \
	man "$@"
}

# look words in nix's dict
function dict() {
	/usr/bin/grep "$@" /usr/share/dict/words
}

# show ip
function ips() {
    ip -family inet -oneline address show | perl -ne'BEGIN {
        my $fmt = "%-8s%-8s%-20s%-16s%-s\n";
        printf($fmt, "Index", "Device", "IPv4 Address", "Broadcast", "Scope");
    }

    my $fmt = "%-8s%-8s%-20s%-16s%-s\n";
    if (my ($index, $dev, $inet, $brd, $scope, $flags) = /^(\d+):\s+(\w+)\s+inet\s+([^\s]+)(?:\s+brd\s+([^\s]+)|)\s+scope\s+([^\\]+)\\\s+(.*)$/) {
        printf($fmt, $index, $dev, $inet, defined($brd) ? $brd : " ", $scope);
    }'
}

# rename file or directory
function rename() {
	[ $#@ -eq 2 ] || { printf "rename <oldname> <newname>\n"; return 1; }
	[ -e "$1" ] || { printf "No such file or directory: $1\n"; return 1; }
	[ -n "$2" ] || { printf "Please specify a newname\n"; return 1; }
	local to="$(dirname $1)/$2"
	[ "$1" != "$to" ] || { printf "Newname is the same as oldname\n"; return 1; }
	[ -e "$to" ] && { printf "File or directory already exist: $to\n"; return 1; }
	command mv -i -- "$1" "$to"
}

# extract files from compressed status
# TODO: test if the extract binary exists
# 	case $1 in
# 		*.tar.gz)	prog=tar args=xzf ;;
#	esac
#	if whence $prog
#		$prog $args $1
#	else
#		echo "extract needs $prog, but not exist"
#	fi
function extract() {
    [ $# -eq 0 ] && echo "Usage: extract <archives>" && return 1
    while (( $# > 0 )); do
	    if [[ -f $1 ]]
	    then
		    case $1 in
			  *.tar.bz2|*.tbz|*.tbz2) tar xjf $1 ;;
			  *.tar.gz|*.tgz) tar xzf $1 ;;
              *.tar.xz) xz --decompress $1; set -- $@ ${1:0:-3} ;;
              *.tar.Z) uncompress $1; set -- $@ ${1:0:-2} ;;
              *.pax.gz) gunzip $1; set -- $@ ${1:0:-3} ;;
              *.tar) tar xf $1 ;;
			  *.bz2) bunzip2 $1 ;;
			  *.rar) unrar x $1 ;;
			  *.gz) gunzip $1 ;;
			  *.zip|*.war|*.jar) unzip $1 ;;
              *.Z) uncompress $1 ;;
              *.txz) mv "$1" "${1:0:-4}.tar.xz"; set -- $@ "${1:0:-4}.tar.xz" ;;
			  *.xz) xz --decompress $1 ;;
              *.7z) 7za x $1 ;;
              *.pax) pax -r -f $1 ;;
              *.rpm) rpm2cpio $1 | cpio -idmv ;;
              *.pkg) pkgutil --expand $1 ${1:0:-4} ;;
			  *) echo "'$1' cannot be extracted via extract" 1>&2 ;;
		    esac
	    else
		    echo "extract: '$1' is not a valid file" 1>&2
	    fi
        shift
    done
}

if [ -n "$BASH_VERSION" ]; then
    :
elif [ -n "$ZSH_VERSOIN" ]; then
    compdef '_files -g "*.gz *.tgz *.txz *.xz *.7z *.Z *.bz2 *.tbz *.zip *.rar *.tar *.lha"' extract
fi

# History accepts a range in zsh entries as [first] [last] arguments, so to
# get them all run history 1.
# Note that the history list starts at 1 and not 0
# print top command in history
function histop() {
	local FNAME='histop'
	if [[ $# -ne 0 ]]; then
		case "$1" in
			-h|--help)
				echo "Usage: $FNAME -n <num>"
				echo "num: print num lines of most used command"
				return 0
				;;
			-n)
				if [ $# -eq 2 -a $2 -ge 1 -a $2 -le $HISTSIZE ]; then
					local n=$2
					shift
				else
					echo "option -n needs an number"
					return 1
				fi
				;;<commit_msg>add some find utils function from abs; add more info to shrc banner<commit_after># ~/.shell/functions.sh


# test if function to declare already exists
for func in EC man dict ips rename extract histop itunes; do
    if typeset -f $func > /dev/null; then
        echo "WARNING: $func is aleary exists"
    fi
done

# set trap to intercept the non-zero return code of last program
#function _err() { echo -e '\e[1;31m'non-zero return code: $?'\e[m'; }
#trap _err ERR

# do stuff before exit
function _exit() {
    echo "bye"
}
#trap _exit EXIT

# put a red `!' in front of the PS1 if last command exit with an error code
function prompt_command() {
    if [ $? -ne 0 ]; then
        printf "\e[01;31m!\e[m"
    fi
}

#PROMPT_COMMAND=prompt_command

function man () {
	env \
	LESS_TERMCAP_mb=$'\E[01;31m' \
	LESS_TERMCAP_md=$'\E[01;38;5;74m' \
	LESS_TERMCAP_me=$'\E[00m' \
	LESS_TERMCAP_se=$'\E[00m' \
	LESS_TERMCAP_so=$'\E[32;5;246m' \
	LESS_TERMCAP_ue=$'\E[00m' \
	LESS_TERMCAP_us=$'\E[04;36;5;146m' \
	man "$@"
}

# look words in nix's dict
function dict() {
	/usr/bin/grep "$@" /usr/share/dict/words
}

# show ip
function ips() {
    ip -family inet -oneline address show | perl -ne'BEGIN {
        my $fmt = "%-8s%-8s%-20s%-16s%-s\n";
        printf($fmt, "Index", "Device", "IPv4 Address", "Broadcast", "Scope");
    }

    my $fmt = "%-8s%-8s%-20s%-16s%-s\n";
    if (my ($index, $dev, $inet, $brd, $scope, $flags) = /^(\d+):\s+(\w+)\s+inet\s+([^\s]+)(?:\s+brd\s+([^\s]+)|)\s+scope\s+([^\\]+)\\\s+(.*)$/) {
        printf($fmt, $index, $dev, $inet, defined($brd) ? $brd : " ", $scope);
    }'
}

# rename file or directory
function rename() {
	[ $#@ -eq 2 ] || { printf "rename <oldname> <newname>\n"; return 1; }
	[ -e "$1" ] || { printf "No such file or directory: $1\n"; return 1; }
	[ -n "$2" ] || { printf "Please specify a newname\n"; return 1; }
	local to="$(dirname $1)/$2"
	[ "$1" != "$to" ] || { printf "Newname is the same as oldname\n"; return 1; }
	[ -e "$to" ] && { printf "File or directory already exist: $to\n"; return 1; }
	command mv -i -- "$1" "$to"
}

# extract files from compressed status
# TODO: test if the extract binary exists
# 	case $1 in
# 		*.tar.gz)	prog=tar args=xzf ;;
#	esac
#	if whence $prog
#		$prog $args $1
#	else
#		echo "extract needs $prog, but not exist"
#	fi
function extract() {
    [ $# -eq 0 ] && echo "Usage: extract <archives>" && return 1
    while (( $# > 0 )); do
	    if [[ -f $1 ]]
	    then
		    case $1 in
			  *.tar.bz2|*.tbz|*.tbz2) tar xjf $1 ;;
			  *.tar.gz|*.tgz) tar xzf $1 ;;
              *.tar.xz) xz --decompress $1; set -- $@ ${1:0:-3} ;;
              *.tar.Z) uncompress $1; set -- $@ ${1:0:-2} ;;
              *.pax.gz) gunzip $1; set -- $@ ${1:0:-3} ;;
              *.tar) tar xf $1 ;;
			  *.bz2) bunzip2 $1 ;;
			  *.rar) unrar x $1 ;;
			  *.gz) gunzip $1 ;;
			  *.zip|*.war|*.jar) unzip $1 ;;
              *.Z) uncompress $1 ;;
              *.txz) mv "$1" "${1:0:-4}.tar.xz"; set -- $@ "${1:0:-4}.tar.xz" ;;
			  *.xz) xz --decompress $1 ;;
              *.7z) 7za x $1 ;;
              *.pax) pax -r -f $1 ;;
              *.rpm) rpm2cpio $1 | cpio -idmv ;;
              *.pkg) pkgutil --expand $1 ${1:0:-4} ;;
			  *) echo "'$1' cannot be extracted via extract" 1>&2 ;;
		    esac
	    else
		    echo "extract: '$1' is not a valid file" 1>&2
	    fi
        shift
    done
}

if [ -n "$BASH_VERSION" ]; then
    :
elif [ -n "$ZSH_VERSOIN" ]; then
    compdef '_files -g "*.gz *.tgz *.txz *.xz *.7z *.Z *.bz2 *.tbz *.zip *.rar *.tar *.lha"' extract
fi

# Find a file with a pattern in name
function ff() {
    find . -type -iname '*'"$*"'*' -ls;
}

# Find a file with pattern $1 in the name and Execute $2 on it
function fe() {
    find . -type f -iname '*'"${1:-}"'*' \
        -exec ${2:-file} {} \; ;
}

# Find a pattern in a set of files and highlight them
# see http://tldp.org/LDP/abs/html/sample-bashrc.html
function fstr() {
    OPTIND=1
    local case=""
    while getopts :it opt; do
        case "$opt" in
            i) case="-i "
                ;;
            *) cat <<End-Of-Usage
fstr: find string in files.
Usage: fstr [-i] <pattern> <filename pattern>
End-Of-Usage
                ;;
        esac
    done
    shift $(($OPTIND - 1))
    if [ "$#" -lt 1 ]; then
        echo $usage
        return 1
    fi
    find . -type f -name "${2:-*}" -print0 | \
        xargs -0 egrep --color=auto -sn ${case} "$1"
}

# History accepts a range in zsh entries as [first] [last] arguments, so to
# get them all run history 1.
# Note that the history list starts at 1 and not 0
# print top command in history
function histop() {
	local FNAME='histop'
	if [[ $# -ne 0 ]]; then
		case "$1" in
			-h|--help)
				echo "Usage: $FNAME -n <num>"
				echo "num: print num lines of most used command"
				return 0
				;;
			-n)
				if [ $# -eq 2 -a $2 -ge 1 -a $2 -le $HISTSIZE ]; then
					local n=$2
					shift
				else
					echo "option -n needs an number"
					return 1
				fi
				;;
{'fraction_non_alphanumeric': 0.19984334787960165, 'fraction_numerical': 0.031554212823095, 'mean_word_length': 2.239579557810801, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4289184', 'n_tokens_mistral': 3931, 'n_tokens_neox': 3489, 'n_words': 1073}