RosettaCodeData/Task/Word-wrap/NetRexx/word-wrap-1.netrexx
Ingy döt Net 776bba907c Sync
2013-10-27 22:24:23 +00:00

76 lines
2.3 KiB
Text

/* NetRexx */
options replace format comments java crossref symbols
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
@see http://en.wikipedia.org/wiki/Word_wrap#Minimum_length
SpaceLeft := LineWidth
for each Word in Text
if (Width(Word) + SpaceWidth) > SpaceLeft
insert line break before Word in Text
SpaceLeft := LineWidth - Width(Word)
else
SpaceLeft := SpaceLeft - (Width(Word) + SpaceWidth)
*/
method wordWrap(text, lineWidth = 80) public static
if lineWidth > 0 then do
NL = '\n'
SP = ' '
wrapped = ''
spaceWidth = SP.length()
spaceLeft = lineWidth
loop w_ = 1 to text.words()
nextWord = text.word(w_)
if (nextWord.length() + spaceWidth) > spaceLeft then do
wrapped = wrapped || NL || nextWord
spaceLeft = lineWidth - nextWord.length()
end
else do
wrapped = wrapped || SP || nextWord
spaceLeft = spaceLeft - (nextWord.length() + spaceWidth)
end
end w_
end
else do
wrapped = text
end
return wrapped.strip() -- clean w/s from front & back
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) public static
parse arg lineLen .
if lineLen = '' then lineLen = 80
text = getText()
wrappedLines = wordWrap(text, lineLen)
say 'Wrapping text at' lineLen 'characters'
say ('....+....|'.copies((lineLen + 9) % 10)).left(lineLen)
say wrappedLines
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getText() public static
-- ....+....|....+....|....+....|....+....|....+....|....+....|
speech01 = -
'She should have died hereafter;' -
'There would have been a time for such a word.' -
'Tomorrow, and tomorrow, and tomorrow,' -
'Creeps in this petty pace from day to day,' -
'To the last syllable of recorded time;' -
'And all our yesterdays have lighted fools' -
'The way to dusty death. Out, out, brief candle!' -
'Life''s but a walking shadow, a poor player' -
'That struts and frets his hour upon the stage' -
'And then is heard no more. It is a tale' -
'Told by an idiot, full of sound and fury' -
'Signifying nothing.' -
'' -
'—-Macbeth (Act 5, Scene 5, lines 17-28)' -
''
return speech01