# (C) 2010-2013 magicant

# This file contains functions that help completion of file size suffixes.


# Modifies the $PREFIX variable to include argument digits.
# If $TARGETWORD does not start with a digit, a non-zero status is returned.
function completion//prefixdigits {
	typeset word="${TARGETWORD#"$PREFIX"}"
	word=${word#[\'+-]}
	case $word in
	([[:digit:]]*)
		while [ "${word#[[:digit:]]}" != "$word" ]; do
			word=${word#[[:digit:]]}
		done
		PREFIX=${TARGETWORD%"$word"}
		return 0
		;;
	(*)
		PREFIX=${TARGETWORD%"$word"}
		return 1
		;;
	esac
}

# Completes file size suffixes specified in arguments according to the current
# $TARGETWORD and $PREFIX.
function completion//completesizesuffix {
	typeset arg
	for arg do
		case $arg in
		(b) #>>#
			complete -P "$PREFIX" -D "512 bytes"    b
			;; #<<#
		(k) #>>#
			complete -P "$PREFIX" -D "1024 bytes"   k
			;; #<<#
		(m) #>>#
			complete -P "$PREFIX" -D "1024^2 bytes" m
			;; #<<#
		(g) #>>#
			complete -P "$PREFIX" -D "1024^3 bytes" g
			;; #<<#
		(KMGB) #>>#
			complete -P "$PREFIX" -D "1024 bytes"   K
			complete -P "$PREFIX" -D "1000 bytes"   KB
			complete -P "$PREFIX" -D "1024^2 bytes" M
			complete -P "$PREFIX" -D "1000^2 bytes" MB
			complete -P "$PREFIX" -D "1024^3 bytes" G
			complete -P "$PREFIX" -D "1000^3 bytes" GB
			;; #<<#
		(GNU*) #>>#
			command -f completion//completesizesuffix KMGB
			complete -P "$PREFIX" -D "1024^4 bytes" T
			complete -P "$PREFIX" -D "1000^4 bytes" TB
			complete -P "$PREFIX" -D "1024^5 bytes" P
			complete -P "$PREFIX" -D "1000^5 bytes" PB
			complete -P "$PREFIX" -D "1024^7 bytes" Z
			complete -P "$PREFIX" -D "1000^7 bytes" ZB
			complete -P "$PREFIX" -D "1024^8 bytes" Y
			complete -P "$PREFIX" -D "1000^8 bytes" YB
			#<<#
			if [ "$arg" != GNU-E ]; then #>>#
				complete -P "$PREFIX" -D "1024^6 bytes" E
				complete -P "$PREFIX" -D "1000^6 bytes" EB
			fi #<<#
			;;
		esac
	done
	return 0
}

# Completes the argument to the --block-size option of df, du, and ls commands.
function completion//completeblocksize {
	if ! command -f completion//prefixdigits; then #>>#
		complete -P "$PREFIX" -D "print size using K, M, etc. for 1024^n" human-readable
		complete -P "$PREFIX" -D "print size using k, M, etc. for 1000^n" si
	fi #<<#
	command -f completion//completesizesuffix GNU
}


# vim: set ft=sh ts=8 sts=8 sw=8 noet:
