home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / COLMIZE.ICN < prev    next >
Text File  |  1991-07-13  |  3KB  |  104 lines

  1. ############################################################################
  2. #
  3. #    Name:    colmize.icn
  4. #
  5. #    Title:    Arrange data into columns
  6. #
  7. #    Author:    Robert J. Alexander
  8. #
  9. #    Date:    June 15, 1990
  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,tag,tagspace,tagminwidth,rowwise,distribute)
  25.    local mean,cols,lines,width,i,x,wid,extra,t,j,first_tagfield,tagfield
  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.    # tag: a label to be placed on the first line of output
  34.    /tagminwidth := 0
  35.    /tagspace := 2
  36.    # rowwise: if nonnull, entries are listed in rowwise order rather than
  37.    # columnwise
  38.    #
  39.    #
  40.    #  Process the tag field information. The tag will appear on the
  41.    #  first line to the left of the data.
  42.    #
  43.    if \tag then {
  44.       tagminwidth <:= *tag + tagspace
  45.       maxcols -:= tagminwidth
  46.       first_tagfield := left(tag, tagminwidth - tagspace) || repl(" ",tagspace)
  47.       tagfield := repl(" ",tagminwidth)
  48.    } else 
  49.       tagfield := first_tagfield := ""
  50.    #  Starting with a trial number-of-columns that is guaranteed
  51.    #  to be too wide, successively reduce the number until the
  52.    #  items can be packed into the allotted width.
  53.    #
  54.    mean := 0
  55.    every mean +:= *!entries
  56.    mean := mean / (0 ~= *entries) | 1
  57.    every cols := (maxcols + space) * 2 / (mean + space) to 1 by -1 do {
  58.       lines := (*entries + cols - 1) / cols
  59.       width := list(cols,minwidth)
  60.       i := 0
  61.       if /rowwise then {                  # if column-wise
  62.      every x := !entries do {
  63.         width[i / lines + 1] <:= *x + space
  64.         i +:= 1
  65.         }
  66.      }
  67.       else {                              # else row-wise
  68.      every x := !entries do {
  69.         width[i % cols + 1] <:= *x + space
  70.         i +:= 1
  71.         }
  72.      }
  73.       wid := 0
  74.       every x := !width do wid +:= x
  75.       if wid <= maxcols + space then break
  76.       }
  77.    #
  78.    #  Now output the data in columns.
  79.    #
  80.    extra := (\distribute & (maxcols - wid) / (0 < cols - 1)) | 0
  81.    if /rowwise then {            # if column-wise
  82.       every i := 1 to lines do {
  83.          if i = 1 then
  84.             t := first_tagfield
  85.          else
  86.             t := tagfield
  87.      every j := 0 to cols - 1 do
  88.            t ||:= left(entries[i + j * lines],width[j + 1] + extra)
  89.      suspend trim(t)
  90.      }
  91.       }
  92.    else {                                # else row-wise
  93.       every i := 0 to lines - 1 do {
  94.          if i = 0 then
  95.             t := first_tagfield
  96.          else
  97.             t := tagfield
  98.      every j := 1 to cols do
  99.            t ||:= left(entries[j + i * cols],width[j] + extra)
  100.      suspend trim(t)
  101.      }
  102.       }
  103. end
  104.