Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add list of all commands in help which uses the way-cool columnize ro…

…utine.
  • Loading branch information...
commit a914ef8057b567f4e11d9a04fb6f88773866bf7e 1 parent 513e990
@rocky authored
View
14 command/help.sh
@@ -25,10 +25,12 @@ _Dbg_do_help() {
if ((0==$#)) ; then
_Dbg_msg "Type 'help <command-name>' for help on a specific command.\n"
_Dbg_msg 'Available commands:'
-# typeset -a commands
-# for
-# commands=(${(ki)_Dbg_command_help})
-# print -C $_Dbg_help_cols $commands
+ typeset commands="${!_Dbg_command_help[@]}"
+ unset columnized; columnize "$commands" 45
+ typeset -i i
+ for ((i=0; i<${#columnized[@]}; i++)) ; do
+ _Dbg_msg " ${columnized[i]}"
+ done
else
typeset cmd=$1
if [[ -n ${_Dbg_command_help[$cmd]} ]] ; then
@@ -37,9 +39,9 @@ _Dbg_do_help() {
_Dbg_expand_alias $cmd
typeset cmd="$expanded_alias"
if [[ -n ${_Dbg_command_help[$cmd]} ]] ; then
- print ${_Dbg_command_help[$cmd]}
+ _Dbg_msg ${_Dbg_command_help[$cmd]}
else
- print "Don't have help for '$1'."
+ _Dbg_msg "Don't have help for '$1'."
fi
fi
fi
View
1  lib/Makefile.am
@@ -1,5 +1,6 @@
pkgdatadir = ${datadir}/@PACKAGE@/lib
pkgdata_DATA = \
+ columnize.sh \
fns.sh \
frame.sh \
gdb.sh \
View
138 lib/columnize.sh
@@ -0,0 +1,138 @@
+# -*- shell-script -*-
+# Copyright (C) 2008 Rocky Bernstein rocky@gnu.org
+#
+# kshdb is free software; you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation; either version 2, or (at your option) any later
+# version.
+#
+# kshdb is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+# for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with kshdb; see the file COPYING. If not, write to the Free Software
+# Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
+#
+# Code ported from my Ruby code which is in turn ported from a routine
+# from Python.
+
+# +colsep+ contains the string to use to separate entries.
+# columnize(list, displaywidth=80, colsep = ' ')
+columnize() {
+ typeset -a list
+ list=($1)
+ typeset -i displaywidth=${2:-80}
+ typeset colsep=${3:-' '}
+ typeset -i list_size=${#list[@]}
+ if ((list_size == 0)) ; then
+ columnized=("<empty>")
+ return
+ fi
+ if ((1 == list_size)); then
+ columnized=("${list[0]}")
+ return
+ fi
+ # Consider arranging list in 1 rows total, then 2 rows...
+ # Stop when at the smallest number of rows which
+ # can be arranged less than the display width.
+ typeset -i nrows=0
+ typeset -i ncols=0
+ typeset -a colwidths=()
+ typeset -i i=0
+ for (( i=0; i<list_size; i++ )) ; do
+ typeset -a colwidths=()
+ ((nrows++))
+
+ ((ncols=(list_size + nrows-1) / nrows))
+ typeset -i totwidth=-${#colsep}
+ typeset -i col
+ for (( col=0; col<=(ncols-1); col++ )); do
+ # get max column width for this column
+ colwidth=0
+ typeset -i row
+ for (( row=0; row<=(nrows-1); row++ )); do
+ ((i=row + nrows*col)) # [rows, cols]
+ if ((i >= list_size)); then
+ break
+ fi
+ typeset item=${list[$i]}
+ ((colwidth < ${#item})) && colwidth=${#item}
+ done
+ colwidths+=($colwidth)
+ ((totwidth+=colwidth + ${#colsep}))
+ if ((totwidth > displaywidth)); then
+ break
+ fi
+ done
+ if ((totwidth <= displaywidth)); then
+ break
+ fi
+ done
+ # The smallest number of rows computed and the
+ # max widths for each column has been obtained.
+ # Now we just have to format each of the
+ # rows.
+ for (( row=0; row<nrows; row++ )); do
+ typeset -i text_size=0
+ typeset -a texts=()
+ for ((col=0; col<ncols; col++)); do
+ ((i=row + nrows*col))
+ if ((i >= list_size)); then
+ item=''
+ else
+ item=${list[i]}
+ fi
+
+ texts[$text_size]="$item"
+ ((text_size++))
+ done
+ while (( text_size > 0 )) && [[ ${texts[text_size-1]} == '' ]] ; do
+ ((text_size--))
+ unset texts[$text_size]
+ done
+ text_row=''
+ for (( col=0; col<text_size; col++ )); do
+ fmt="%-${colwidths[col]}s"
+ text_cell="$(printf $fmt ${texts[col]})"
+ text_row+="$text_cell"
+ ((col != text_size-1)) && text_row+="${colsep}"
+ done
+ columnized+=("$text_row")
+ done
+}
+
+if [[ $0 == *columnize ]] ; then
+ #
+ [[ -n $ZSH_NAME ]] && [[ -n $ZSH_VERSION ]] && setopt ksharrays
+ print_columns() {
+ unset columnized
+ columnize "$@"
+ typeset -i i
+ echo '==============='
+ for ((i=0; i<${#columnized[@]}; i++)) ; do
+ printf "${columnized[$i]}\n"
+ done
+ }
+ print_columns
+ print_columns ''
+ print_columns oneitem
+ print_columns 'a 2 c' 10 ', '
+ print_columns \
+' one two three
+ 4ne 5wo 6hree
+ 7ne 8wo 9hree
+ 10e 11o 12ree' 18
+
+ print_columns \
+' 1 two three
+ for 5 six
+ 7 8' 12
+
+ print_columns \
+' 1 two 3
+ for 5 six
+ 7 8' 12
+
+fi
View
6 lib/help.sh
@@ -1,18 +1,18 @@
# -*- shell-script -*-
# Copyright (C) 2008 Rocky Bernstein rocky@gnu.org
#
-# zshdb is free software; you can redistribute it and/or modify it under
+# kshdb is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any later
# version.
#
-# zshdb is distributed in the hope that it will be useful, but WITHOUT ANY
+# kshdb is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
-# with zshdb; see the file COPYING. If not, write to the Free Software
+# with kshdb; see the file COPYING. If not, write to the Free Software
# Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
# A place to put help command text
Please sign in to comment.
Something went wrong with that request. Please try again.