Word wrap: Difference between revisions
Content added Content deleted
(Added solution for Action!) |
|||
Line 329: | Line 329: | ||
caught it, and this ball was her |
caught it, and this ball was her |
||
favorite plaything. |
favorite plaything. |
||
</pre> |
|||
=={{header|Action!}}== |
|||
<lang Action!>CHAR ARRAY text(1000) |
|||
CARD length |
|||
PROC AppendText(CHAR ARRAY part) |
|||
BYTE i |
|||
FOR i=1 TO part(0) |
|||
DO |
|||
text(length)=part(i) |
|||
length==+1 |
|||
OD |
|||
RETURN |
|||
INT FUNC GetPosForWrap(BYTE lineLen INT start) |
|||
INT pos |
|||
pos=start+lineLen |
|||
IF pos>=length THEN |
|||
RETURN (length-1) |
|||
FI |
|||
WHILE pos>start AND text(pos)#32 |
|||
DO |
|||
pos==-1 |
|||
OD |
|||
IF pos=start THEN |
|||
pos=start+lineLen |
|||
ELSE |
|||
pos==-1 |
|||
FI |
|||
RETURN (pos) |
|||
PROC PrintTextWrapped(BYTE lineLen) |
|||
INT i,pos |
|||
BYTE wrap,screenWidth=[40] |
|||
i=0 |
|||
WHILE i<length |
|||
DO |
|||
pos=GetPosForWrap(lineLen,i) |
|||
IF pos-i=screenWidth-1 OR pos=length-1 THEN |
|||
wrap=0 |
|||
ELSE |
|||
wrap=1 |
|||
FI |
|||
WHILE i<=pos |
|||
DO |
|||
Put(text(i)) |
|||
i==+1 |
|||
OD |
|||
WHILE i<length AND text(i)=32 |
|||
DO |
|||
i==+1 |
|||
OD |
|||
IF wrap THEN |
|||
PutE() |
|||
FI |
|||
OD |
|||
RETURN |
|||
PROC Test(BYTE lineLen) |
|||
BYTE CH=$02FC |
|||
Put(125) ;clear screen |
|||
PrintF("Line length=%B%E%E",lineLen) |
|||
PrintTextWrapped(lineLen) |
|||
PrintF("%E%EPress any key to continue...") |
|||
DO UNTIL CH#$FF OD |
|||
CH=$FF |
|||
RETURN |
|||
PROC Main() |
|||
BYTE LMARGIN=$52,old |
|||
length=0 |
|||
AppendText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. ") |
|||
AppendText("Maecenas varius sapien vel purus hendrerit vehicula. ") |
|||
AppendText("Integer hendrerit viverra turpis, ac sagittis arcu pharetra id. ") |
|||
AppendText("Sed dapibus enim non dui posuere sit amet rhoncus tellus consectetur. ") |
|||
AppendText("Proin blandit lacus vitae nibh tincidunt cursus. ") |
|||
AppendText("Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. ") |
|||
AppendText("Nam tincidunt purus at tortor tincidunt et aliquam dui gravida. ") |
|||
AppendText("Nulla consectetur sem vel felis vulputate et imperdiet orci pharetra. ") |
|||
AppendText("Nam vel tortor nisi. Sed eget porta tortor. ") |
|||
AppendText("Aliquam suscipit lacus vel odio faucibus tempor. ") |
|||
AppendText("Sed ipsum est, condimentum eget eleifend ac, ultricies non dui.") |
|||
old=LMARGIN |
|||
LMARGIN=0 ;remove left margin on the screen |
|||
Test(40) |
|||
Test(30) |
|||
Test(20) |
|||
LMARGIN=old ;restore left margin on the screen |
|||
RETURN |
|||
</lang> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Word_wrap.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
Line length=40 |
|||
Lorem ipsum dolor sit amet, consectetur |
|||
adipiscing elit. Maecenas varius sapien |
|||
... |
|||
Sed ipsum est, condimentum eget eleifend |
|||
ac, ultricies non dui. |
|||
Press any key to continue... |
|||
Line length=30 |
|||
Lorem ipsum dolor sit amet, |
|||
consectetur adipiscing elit. |
|||
... |
|||
condimentum eget eleifend ac, |
|||
ultricies non dui. |
|||
Press any key to continue... |
|||
Line length=20 |
|||
Lorem ipsum dolor |
|||
sit amet, |
|||
... |
|||
eleifend ac, |
|||
ultricies non dui. |
|||
</pre> |
</pre> |
||