Word wrap: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 2 users not shown)
Line 1,954:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,978:
^ TokenEnumerator
.new(self)
.selectBy::(word)
{
currentWidth += word.Length;
Line 1,985:
currentWidth := word.Length + 1;
^ newLinenewLineConstant + word + " "
}
else
Line 6,034:
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.</pre>
 
=={{header|Tailspin}}==
A simple greedy algorithm that will always put one word on a line even if the word is longer than the desired width.
<syntaxhighlight lang="tailspin">
templates break&{width:}
composer words
<word>* (<WS>*)
rule word: (<WS>*) [<'\S'>+]
end words
def chars: [$ -> words];
@: $chars(first);
[$chars(first~..last)... -> #] -> '$... -> '$;$#10;';$@...;' !
 
when <[](..~($width-$@::length))> do ..|@: ' '; $... -> ..|@: $;
otherwise '$@...;' ! @: $;
end break
 
'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.' -> break&{width: 80} -> !OUT::write</syntaxhighlight>
{{out}}
<pre>
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.
</pre>
 
=={{header|Tcl}}==
Line 6,249 ⟶ 6,286:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">var greedyWordWrap = Fn.new { |text, lineWidth|
var words = text.split(" ")
var sb = words[0]
9,485

edits