RosettaCodeData/Task/Word-wrap/PureBasic/word-wrap.basic

47 lines
1.7 KiB
Plaintext

DataSection
Data.s "In olden times when wishing still helped one, there lived a king "+
"whose daughters were all beautiful, but the youngest was so beautiful "+
"that the sun itself, which has seen so much, was astonished whenever "+
"it shone-in-her-face. Close-by-the-king's castle lay a great dark "+
"forest, and under an old lime-tree in the forest was a well, and when "+
"the day was very warm, the king's child went out into the forest and "+
"sat down by the side of the cool-fountain, and when she was bored she "+
"took a golden ball, and threw it up on high and caught it, and this "+
"ball was her favorite plaything."
EndDataSection
Procedure.i ww_pos(txt$,l.i)
While Mid(txt$,l,1)<>Chr(32) And l>0 And Len(txt$)>l : l-1 : Wend
If l>0 : ProcedureReturn l : Else : ProcedureReturn Len(Trim(txt$)) : EndIf
EndProcedure
Procedure WriteLine(txt$,ls.i)
Shared d$,lw
Select LCase(d$)
Case "l" : PrintN(Mid(txt$,1,ls))
Case "r" : PrintN(RSet(Trim(Mid(txt$,1,ls)),lw,Chr(32)))
EndSelect
EndProcedure
Procedure main(txt$,lw.i)
If Len(txt$)
p=ww_pos(txt$,lw) : WriteLine(txt$,p) : ProcedureReturn main(LTrim(Right(txt$,Len(txt$)-p)),lw)
EndIf
EndProcedure
Procedure.i MaxWordLen(txt$)
For i=1 To CountString(txt$,Chr(32))+1
wrd$=LTrim(StringField(txt$,i,Chr(32)))
wrdl=Len(wrd$)+1 : If wrdl>l : l=wrdl : EndIf
Next
ProcedureReturn l
EndProcedure
OpenConsole()
Read.s t$
Print("Input line width: ") : lw=Val(Input()) : minL=MaxWordLen(t$)
If lw<minL : lw=minL : PrintN("Min. line width "+Str(lw-1)) : EndIf
Print("Input direction l:left r:rigth ")
Repeat : d$=Inkey() : Delay(50) : Until FindString("lr",d$,1,#PB_String_NoCase) : PrintN(d$+#CRLF$)
main(t$,lw) : Input()