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

  1. ############################################################################
  2. #
  3. #    Name:    printcol.icn
  4. #
  5. #    Title:    Format columnar data
  6. #
  7. #    Author:    Robert J. Alexander
  8. #
  9. #    Date:    June 10, 1988
  10. #
  11. ############################################################################
  12. #  
  13. #     This procedure deals with with the problem of printing tabular
  14. #  data where the total width of items to be printed is wider than
  15. #  the page.  Simply allowing the data to wrap to additional lines
  16. #  often produces marginally readable output.  This procedure facil-
  17. #  itates printing such groups of data as vertical columns down the
  18. #  page length, instead of as horizontal rows across the page.  That
  19. #  way many, many fields can be printed neatly.  The programming of
  20. #  such a transformation can be a nuisance.  This procedure does
  21. #  much of the work for you, like deciding how many items can fit
  22. #  across the page width and ensuring that entire items will be
  23. #  printed on the same page without page breaks (if that service is
  24. #  requested).
  25. #  
  26. #     For example, suppose we have a list of records we would like
  27. #  to print.  The record is defined as:
  28. #  
  29. #          record rec(item1,item2,item3,...)
  30. #  
  31. #  Also suppose that lines such as
  32. #  
  33. #          Field 1   Field 2   Field 3     ...
  34. #          -------   -------   -------     ---
  35. #          Record 1    item1     item2     item3      ...
  36. #          Record 2    item1     item2     item3      ...
  37. #  
  38. #  are too long to print across the page.  This procedure will print
  39. #  them as:
  40. #  
  41. #          TITLE
  42. #          =====
  43. #          Record 1   Record 2     ...
  44. #          --------   --------     ---
  45. #          Field 1   item1      item1       ...
  46. #          Field 2   item2      item2       ...
  47. #          Field 3   item3      item3       ...
  48. #  
  49. #  The arguments are:
  50. #  
  51. #       items:       a co-expression that produces a sequence of
  52. #                    items (usually structured data objects, but not
  53. #                    necessarily) for which data is to be printed.
  54. #  
  55. #       fields:      a list of procedures to produce the field's
  56. #                    data.  Each procedure takes two arguments.  The
  57. #                    procedure's action depends upon what is passed
  58. #                    in the first argument:
  59. #  
  60. #            header      Produces the row heading string to be used
  61. #                        for that field (the field name).
  62. #  
  63. #            width       Produces the maximum field width that can
  64. #                        be produced (including the column header).
  65. #  
  66. #            Other      Produces the field value string for the
  67. #                        item passed as the argument.
  68. #  
  69. #          The second argument is arbitrary data from the procedures
  70. #       with each invocation.  The data returned by the first func-
  71. #       tion on the list is used as a column heading string (the
  72. #       item name).
  73. #  
  74. #       title:       optional.
  75. #  
  76. #  
  77. #       pagelength:  if null (omitted) page breaks are ignored.
  78. #  
  79. #       linelength:  default 80.
  80. #  
  81. #       auxdata:     auxiliary arbitrary data to be passed to the field
  82. #                    procedures -- see `fields', above.
  83. #  
  84. ############################################################################
  85.  
  86. procedure printcol(items,fields,title,pagelength,linelength,auxdata)
  87.   local maxwidth,maxhead,groups,columns,itemlist,cont,f,p,underline,
  88.     hfield
  89.   /linelength := 80
  90.   /pagelength := 30000
  91.   /title := ""
  92. #
  93. #  Compute the maximum field width (so we know the column spacing) and
  94. #  the maximum header width (so we know how much space to leave on the
  95. #  left for headings.
  96. #
  97.   maxwidth := maxhead := -1 
  98.   cont := ""
  99.   every maxwidth <:= (!fields)("width",auxdata)
  100.   hfield := get(fields)
  101.   every maxhead <:= *(!fields)("header",auxdata)
  102.   columns := (linelength - maxhead) / (maxwidth + 1)
  103.   groups := pagelength / (6 + *fields)
  104. #
  105. #  Loop to print groups of data.
  106. #
  107.   repeat {
  108.     if pagelength < 30000 then writes("\f")
  109. #
  110. #  Loop to print data of a group (a page's worth).
  111. #
  112.     every 1 to groups do {
  113. #
  114. #  Collect the items to be output in this group.  A group is the number
  115. #  of columns that can fit across the page.
  116. #
  117.       itemlist := []
  118.       every 1 to columns do put(itemlist,@items) | break
  119.       if *itemlist = 0 then break break
  120. #
  121. #  Print a title and the column headings.
  122. #
  123.       write(repl("=",*write("\n",title || cont)))
  124.       cont := " (continued)"
  125.       writes(underline := left("",maxhead))
  126.       every f := hfield(!itemlist,auxdata) do {
  127.     p := if *f < maxwidth then center else left
  128.     writes(" ",p(f,maxwidth))
  129.     underline ||:= " " || p(repl("-",*f),maxwidth)
  130.       }
  131.       write("\n",underline)
  132. #
  133. #  Print the fields.
  134. #
  135.       every f := !fields do {
  136.     writes(right(f("header",auxdata),maxhead))
  137.     every writes(" ",center(f(!itemlist,auxdata),maxwidth))
  138.     write()
  139.       }
  140.     }    # End of loop to print groups.
  141.   }    # End of loop to print all items.
  142.   return
  143. end
  144.