home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / colm.icn < prev    next >
Text File  |  2000-07-29  |  5KB  |  132 lines

  1. ############################################################################
  2. #
  3. #    File:     colm.icn
  4. #
  5. #    Subject:  Program to arrange data into columns
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     December 5, 1989
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. # Program to arrange a number of data items, one per line, into
  18. # multiple columns. Items are arranged in column-wise order, that is,
  19. # the sequence runs down the first column, then down the second, etc.
  20. #
  21. # If a null line appears in the input stream, it signifies a break in
  22. # the list, and the following line is taken as a title for the
  23. # following data items. No title precedes the initial sequence of
  24. # items.
  25. #
  26. # Usage:
  27. #
  28. #       colm [-w line_width] [-s space_between] [-m min_width]
  29. #               [-t tab_width] [-x] [-d] [file ...]
  30. #
  31. #  The parameters are:
  32. #
  33. #       line_width:     the maximum width allowed for output lines
  34. #                       (default: 80).
  35. #       space_between:  minimum number of spaces between items
  36. #                       (default: 2).
  37. #       min_width:      minimum width to be printed for each entry
  38. #                       (default: no minimum).
  39. #       tab_width:      tab width used to entab output lines.
  40. #                       (default: no tabs).
  41. #       -x              print items in row-wise order rather than
  42. #                       column-wise.
  43. #       -d (distribute) distribute columns throughout available width.
  44. #
  45. #  The command "colm -h" generates "help" text.
  46. #
  47. #  This is a  general utility,  but  it  was  written and tailored for a
  48. #  specific purpose:
  49. #
  50. #  This  utility  was written  to rearrange the file name  list from the
  51. #  Macintosh  Programmer's   Workshop  "Files"  command  into   a   more
  52. #  convenient  format.  "Files" lists  file  names in a  single  column.
  53. #  This program  takes  the  list  produced by  "Files"  and  outputs  a
  54. #  multi-column  list.  The  names  are  listed  vertically within  each
  55. #  column, and  the column width is computed dynamically  depending upon
  56. #  the sizes  of the  names listed.  A  recommendation  is  to create  a
  57. #  command file "lc" (List in Columns) as follows:
  58. #
  59. #       Files {"Parameters"} | colm
  60. #
  61. #  The output from  the  Files command  is "piped" to the "colm" program
  62. #  (this program), which prints its list in the current window.
  63. #
  64. #  By  putting both  the "lc"  command  file and the "colm" program into
  65. #  your {MPW}Tools folder, "lc" can be conveniently issued  as a command
  66. #  at any time, using the same parameters as the "Files" command.
  67.  
  68. link options, colmize
  69.  
  70. procedure main(arg)
  71.    local usage, help, opt, rowwise, distribute, maxcols, space, minwidth
  72.    local tabwidth, f, entries, entry
  73.    #
  74.    #  Define usage and help strings.
  75.    #
  76.    usage := "_
  77.    Usage:\tcolm [-w line_width] [-s space_between] [-m min_width]\n_
  78.         \t\t[-t tab_width] [-x] [file ...]\n_
  79.         \tcolm -h  for help"
  80.    help := "_
  81.         \tline_width:\tthe maximum width allowed for output lines\n_
  82.                     \t\t\t(default: 80).\n_
  83.         \tspace_between:\tminimum number of spaces between items\n_
  84.                     \t\t\t(default: 2).\n_
  85.         \tmin_width:\tminimum width to be printed for each entry\n_
  86.                     \t\t\t(default: no minimum).\n_
  87.         \ttab_width:\ttab width used to print output lines.\n_
  88.                     \t\t\t(default: no tabs).\n_
  89.         \t-x\t\tprint items in row-wise order rather than\n_
  90.                     \t\t\tcolumn-wise.\n_
  91.         \t-d (distribute)\tdistribute columns throughout available width."
  92.    #
  93.    #  Process command line options.
  94.    #
  95.    opt := options(arg,"hxdw+s+m+t+")
  96.    if \opt["h"] then write(usage,"\n\n",help) & exit()
  97.    rowwise := opt["x"]
  98.    distribute := opt["d"]
  99.    maxcols := \opt["w"] | 80
  100.    space := \opt["s"] | 2
  101.    minwidth := \opt["m"] | 0
  102.    tabwidth := (\opt["t"] | 0) + 1
  103.    if tabwidth = 1 then entab := 1
  104.    if *arg = 0 then arg := [&input]
  105.    #
  106.    #  Loop to process input files.
  107.    #
  108.    while f := get(arg) do {
  109.       f := (&input === f) | open(f) | stop("Can't open ",f)
  110.       #
  111.       #  Loop to process input groups (separated by empty lines).
  112.       #
  113.       repeat {
  114.      entries := []
  115.      #
  116.      #  Loop to build a list of non-empty lines of an input file.
  117.      #
  118.      while entry := "" ~== read(f) do {
  119.         put(entries,entry)
  120.         }
  121.      #
  122.      #  Now write the data in columns.
  123.      #
  124.      every write(entab(colmize(entries,maxcols,space,minwidth,
  125.            rowwise,distribute),tabwidth))
  126.      write("\n",read(f)) | break       # print the title line, if any
  127.      }
  128.       close(f)
  129.       write()
  130.       }
  131. end
  132.