diff options
-rw-r--r-- | source/1.0/doc/CHANGES-1.0.txt | 2 | ||||
-rw-r--r-- | source/1.0/src/shflags | 24 | ||||
-rwxr-xr-x | source/1.0/src/shflags_test_private.sh | 16 |
3 files changed, 31 insertions, 11 deletions
diff --git a/source/1.0/doc/CHANGES-1.0.txt b/source/1.0/doc/CHANGES-1.0.txt index 3e5c921..b6a07f6 100644 --- a/source/1.0/doc/CHANGES-1.0.txt +++ b/source/1.0/doc/CHANGES-1.0.txt @@ -30,6 +30,8 @@ when FLAGS() is called stating that the help flag already defined. Issue# 2: Passing the --nohelp option no longer gives help output. +Issue# 3: Added initial support for screen width detection. + Changes with 1.0.1 ------------------ diff --git a/source/1.0/src/shflags b/source/1.0/src/shflags index 689f564..33a282a 100644 --- a/source/1.0/src/shflags +++ b/source/1.0/src/shflags @@ -442,15 +442,22 @@ _flags_itemInList() return ${flags_return} } -# Returns the length of a string. +# Returns the width of the current screen. # -# Args: -# str: string: string to determine length of # Output: -# integer: length of string -_flags_strlen() +# integer: width in columns of the current screen. +_flags_screenColumns() { - echo "$1" |awk '{print length($0)}' + _flags_size_=`stty size 2>/dev/null` + if [ $? -eq ${FLAGS_TRUE} ]; then + _flags_cols_=`expr "${_flags_size_}" : '[0-9]* \([0-9]*\)'` + else + # TODO(kward): support 'tput cols' as well, if possible. be warned that it + # doesn't work when put inside backticks (i.e. a sub-shell). + _flags_cols_=80 + fi + echo ${_flags_cols_} + unset _flags_cols_ _flags_size_ } # Validate a boolean. @@ -920,8 +927,9 @@ flags_help() _flags_defaultStr="(default: ${_flags_defaultStr})" _flags_helpStr=" ${_flags_flagStr} ${_flags_help} ${_flags_defaultStr}" - _flags_helpStrLen=`_flags_strlen "${_flags_helpStr}"` - if [ ${_flags_helpStrLen} -lt 80 ]; then + _flags_helpStrLen=`expr "${_flags_helpStr}" : '.*'` + flags_screenCols_=`_flags_screenColumns` + if [ ${_flags_helpStrLen} -lt ${flags_screenCols_} ]; then echo "${_flags_helpStr}" >&2 else _flags_emptyStr="`echo ${_flags_flagStr} |sed 's/./ /g'`" diff --git a/source/1.0/src/shflags_test_private.sh b/source/1.0/src/shflags_test_private.sh index 9e28c8b..db508a5 100755 --- a/source/1.0/src/shflags_test_private.sh +++ b/source/1.0/src/shflags_test_private.sh @@ -52,7 +52,15 @@ testItemInList() assertFalse 'empty lists should not match' $? } -testValidateBoolean() { +testScreenColumns() +{ + cols=`_flags_screenColumns` + value=`expr "${cols}" : '\([0-9]*\)'` + assertNotNull "unexpected screen width (${cols})" "${value}" +} + +testValidateBoolean() +{ # valid values for value in ${TH_BOOL_VALID}; do _flags_validateBoolean "${value}" @@ -66,7 +74,8 @@ testValidateBoolean() { done } -testValidateFloat() { +testValidateFloat() +{ # valid values for value in ${TH_INT_VALID} ${TH_FLOAT_VALID}; do _flags_validateFloat "${value}" @@ -80,7 +89,8 @@ testValidateFloat() { done } -testValidateInteger() { +testValidateInteger() +{ # valid values for value in ${TH_INT_VALID}; do _flags_validateInteger "${value}" |