home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / sharew / exoten / icon / colmize.icn < prev    next >
Encoding:
Text File  |  1990-03-05  |  2.6 KB  |  84 lines

  1. ############################################################################
  2. #
  3. #    Name:    colmize.icn
  4. #
  5. #    Title:    Arrange data into columns
  6. #
  7. #    Author:    Robert J. Alexander
  8. #
  9. #    Date:    December 5, 1989
  10. #
  11. ############################################################################
  12. #
  13. #  colmize() -- Arrange data into columns.
  14. #
  15. #  Procedure to arrange a number of data items into multiple columns.
  16. #  Items are arranged in column-wise order, that is, the sequence runs
  17. #  down the first column, then down the second, etc.
  18. #
  19. #  This procedure goes to great lengths to print the items in as few
  20. #  vertical lines as possible.
  21. #
  22. ############################################################################
  23.  
  24. procedure colmize(entries,maxcols,space,minwidth,rowwise,distribute)
  25.    local mean,cols,lines,width,i,x,wid,extra,t,j
  26.    #
  27.    #  Process arguments -- provide defaults.
  28.    #
  29.    # entries: a list of items to be columnized
  30.    /maxcols := 80                        # max width of output lines
  31.    /space := 2                           # min nbr of spaces between columns
  32.    /minwidth := 0                        # min column width
  33.    # rowwise: if nonnull, entries are listed in rowwise order rather than
  34.    # columnwise
  35.    #
  36.    #  Starting with a trial number-of-columns that is guaranteed
  37.    #  to be too wide, successively reduce the number until the
  38.    #  items can be packed into the allotted width.
  39.    #
  40.    mean := 0
  41.    every mean +:= *!entries
  42.    mean := mean / (0 ~= *entries) | 1
  43.    every cols := (maxcols + space) * 2 / (mean + space) to 1 by -1 do {
  44.       lines := (*entries + cols - 1) / cols
  45.       width := list(cols,minwidth)
  46.       i := 0
  47.       if /rowwise then {                  # if column-wise
  48.      every x := !entries do {
  49.         width[i / lines + 1] <:= *x + space
  50.         i +:= 1
  51.         }
  52.      }
  53.       else {                              # else row-wise
  54.      every x := !entries do {
  55.         width[i % cols + 1] <:= *x + space
  56.         i +:= 1
  57.         }
  58.      }
  59.       wid := 0
  60.       every x := !width do wid +:= x
  61.       if wid <= maxcols + space then break
  62.       }
  63.    #
  64.    #  Now output the data in columns.
  65.    #
  66.    extra := (\distribute & (maxcols - wid) / (0 < cols - 1)) | 0
  67.    if /rowwise then {            # if column-wise
  68.       every i := 1 to lines do {
  69.      t := ""
  70.      every j := 0 to cols - 1 do
  71.            t ||:= left(entries[i + j * lines],width[j + 1] + extra)
  72.      suspend trim(t)
  73.      }
  74.       }
  75.    else {                                # else row-wise
  76.       every i := 0 to lines - 1 do {
  77.      t := ""
  78.      every j := 1 to cols do
  79.            t ||:= left(entries[j + i * cols],width[j] + extra)
  80.      suspend trim(t)
  81.      }
  82.       }
  83. end
  84.