Author Topic: Aligned Text  (Read 2724 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Aligned Text
« on: June 20, 2012, 01:01:41 AM »
This is for the Rosetta Code Task "AlignedText". Since it is potentially useful, I am posting it here

It enables packed text records to be laid out in rows and columns, left, centred or right justified. The spacing is adjusted to hold the longest fields of text.

Code: OxygenBasic
  1.  
  2.  
  3.  
  4. '================
  5. Class AlignedText
  6. '================
  7.  
  8. indexbase 1
  9.  
  10. string  buf, bufo, cr, tab, jus, dlm
  11. sys     Cols, Rows, ColWidth[200], TotWidth, ColPad, ld
  12.  
  13. method SetText(string s)
  14. ========================
  15. cr=chr(13)+chr(10)
  16. tab=chr(9)
  17. jus=string 200,"L"
  18. buf=s
  19. measure
  20. end method
  21.  
  22.  
  23. method measure()
  24. ================
  25. sys a, b, wa, wb, cm, c, cw
  26. a=1 : b=1
  27. Cols=0 : Rows=0 : ColPad=3
  28. ld=len dlm
  29. if not ld then dlm="," : ld=1 'default to comma
  30. do
  31.   wb=b
  32.   a=instr b,buf,cr
  33.   if a=0 then exit do
  34.   cm=0
  35.   c++
  36.   do
  37.     wa=instr wb,buf,dlm
  38.     if wa=0 or wa>a then exit do
  39.     cm++
  40.     if cm>cols then cols=cm
  41.     cw=wa-wb
  42.     if cw > ColWidth[cm] then ColWidth[cm]=cw
  43.     wb=wa+ld
  44.   end do
  45.   b=a+len cr
  46. end do
  47. rows=c
  48. '
  49. c=0
  50. for i=1 to cols
  51.   ColWidth[ i ]+=ColPad
  52.   c+=ColWidth[ i ]
  53. next
  54. TotWidth=c+len cr
  55. 'print ShowMetrics
  56. end method
  57.  
  58.  
  59. method ShowMetrics() as string
  60. ==============================
  61. pr="METRICS:" cr cr
  62. pr+=rows tab cols tab totwidth cr cr
  63. pr+="column" tab "spacing" cr
  64. for i=1 to cols
  65.   pr+=i tab ColWidth[ i ] cr
  66. next
  67. return pr
  68. end method
  69.  
  70.  
  71. method justify(string j)
  72. ========================
  73. mid jus,1,j
  74. end method
  75.  
  76.  
  77. method layout() as string
  78. =========================
  79. sys a, b, wa, wb, wl, cm, lpos, cpos
  80. bufo=space Rows*TotWidth
  81. a=1 : b=1
  82. do
  83.   wb=b
  84.   a=instr(b,buf,cr)
  85.   if a=0 then exit do
  86.   cm=0
  87.   cpos=1
  88.   do
  89.     wa=instr(wb,buf,dlm)
  90.     if wa=0 or wa>a then exit do
  91.     '
  92.    cm++
  93.     '
  94.    'JUSTIFICATION
  95.    '
  96.    wl=wa-wb
  97.     p=lpos+cpos 'default "L" LEFT ALIGN
  98.    '
  99.    select case asc(jus,cm)
  100.       case "R" : p=lpos+cpos+ColWidth[cm]-wl-Colpad
  101.       case "C" : p=lpos+cpos+( ColWidth[cm]-wl-Colpad )*.5
  102.     end select
  103.     '
  104.    mid bufo,p, mid buf,wb,wl
  105.     cpos+=colwidth[cm]
  106.     wb=wa+ld
  107.   end do
  108.   b=a+len cr
  109.   lpos+=TotWidth
  110.   if lpos<len(bufo) then mid bufo,lpos-1,cr
  111. end do
  112. return bufo
  113. end method
  114.  
  115. end class
  116.  
  117. '#recordof AlignedText
  118.  
  119. '====
  120. 'TEST
  121. '====
  122.  
  123. AlignedText tt
  124. tt.dlm=";;"
  125. tt.SetText quote
  126. """
  127. Given;;a;;text;;file;;of;;many;;lines,;;where;;fields;;within;;a;;line;;
  128. are;;delineated;;by;;a;;single;;'dollar';;character,;;write;;a;;program
  129. that;;aligns;;each;;column;;of;;fields;;by;;ensuring;;that;;words;;in;;each;;
  130. column;;are;;separated;;by;;at;;least;;one;;space.
  131. Further,;;allow;;for;;each;;word;;in;;a;;column;;to;;be;;either;;left;;
  132. justified,;;right;;justified,;;or;;center;;justified;;within;;its;;column.
  133. """
  134. 'print tt.ShowMetrics
  135. tt.justify "LLLLCCCRRRRR"
  136. putfile "t.txt", tt.layout
  137.  
  138.  

Update required:

http://www.oxygenbasic.org/o2zips/Oxygen.zip
« Last Edit: November 04, 2014, 08:55:21 AM by Charles Pegge »