'================
Class AlignedText
'================
indexbase 1
string buf, bufo, cr, tab, jus, dlm
sys Cols, Rows, ColWidth[200], TotWidth, ColPad, ld
method SetText(string s)
========================
cr=chr(13)+chr(10)
tab=chr(9)
jus=string 200,"L"
buf=s
measure
end method
method measure()
================
sys a, b, wa, wb, cm, c, cw
a=1 : b=1
Cols=0 : Rows=0 : ColPad=3
ld=len dlm
if not ld then dlm="," : ld=1 'default to comma
do
wb=b
a=instr b,buf,cr
if a=0 then exit do
cm=0
c++
do
wa=instr wb,buf,dlm
if wa=0 or wa>a then exit do
cm++
if cm>cols then cols=cm
cw=wa-wb
if cw > ColWidth[cm] then ColWidth[cm]=cw
wb=wa+ld
end do
b=a+len cr
end do
rows=c
'
c=0
for i=1 to cols
ColWidth[ i ]+=ColPad
c+=ColWidth[ i ]
next
TotWidth=c+len cr
'print ShowMetrics
end method
method ShowMetrics() as string
==============================
pr="METRICS:" cr cr
pr+=rows tab cols tab totwidth cr cr
pr+="column" tab "spacing" cr
for i=1 to cols
pr+=i tab ColWidth[ i ] cr
next
return pr
end method
method justify(string j)
========================
mid jus,1,j
end method
method layout() as string
=========================
sys a, b, wa, wb, wl, cm, lpos, cpos
bufo=space Rows*TotWidth
a=1 : b=1
do
wb=b
a=instr(b,buf,cr)
if a=0 then exit do
cm=0
cpos=1
do
wa=instr(wb,buf,dlm)
if wa=0 or wa>a then exit do
'
cm++
'
'JUSTIFICATION
'
wl=wa-wb
p=lpos+cpos 'default "L" LEFT ALIGN
'
select case asc(jus,cm)
case "R" : p=lpos+cpos+ColWidth[cm]-wl-Colpad
case "C" : p=lpos+cpos+( ColWidth[cm]-wl-Colpad )*.5
end select
'
mid bufo,p, mid buf,wb,wl
cpos+=colwidth[cm]
wb=wa+ld
end do
b=a+len cr
lpos+=TotWidth
if lpos<len(bufo) then mid bufo,lpos-1,cr
end do
return bufo
end method
end class
'#recordof AlignedText
'====
'TEST
'====
AlignedText tt
tt.dlm=";;"
tt.SetText quote
"""
Given;;a;;text;;file;;of;;many;;lines,;;where;;fields;;within;;a;;line;;
are;;delineated;;by;;a;;single;;'dollar';;character,;;write;;a;;program
that;;aligns;;each;;column;;of;;fields;;by;;ensuring;;that;;words;;in;;each;;
column;;are;;separated;;by;;at;;least;;one;;space.
Further,;;allow;;for;;each;;word;;in;;a;;column;;to;;be;;either;;left;;
justified,;;right;;justified,;;or;;center;;justified;;within;;its;;column.
"""
'print tt.ShowMetrics
tt.justify "LLLLCCCRRRRR"
putfile "t.txt", tt.layout