Word wrap: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments, added whitespace. -- ~~~~)
Line 549: Line 549:
<br>No hyphenation (or de-hyphenation) is attempted.
<br>No hyphenation (or de-hyphenation) is attempted.
<br>Words longer than the width of the output are acceptable and are shown, a simple change could be made to issue a notification.
<br>Words longer than the width of the output are acceptable and are shown, a simple change could be made to issue a notification.
<lang rexx>/*REXX program justifies (by words) a string of words ───► screen. */
<lang rexx>
arg justify width . /*───────────JUSTIFY─────────────*/
/*REXX (Classic) pgm justifies (by words) a string of words──>screen*/
/*Center: ◄centered► */
/* Both: ◄──both margins──► */
/* Right: ────────►right margin */
/* Left: left margin◄──────── */
/*═════pick one of the above.════*/


arg justify width . /*───────JUSTIFY─────────────*/
just=left(justify,1) /*only use first capital letter. */
/*Center: <centered> */
/*Both: <──both margins──> */
/*Right: ─────>right margin */
/*Left: left margin<─────- */
/*─────pick one of the above.*/


if width=='' then width=linesize()%2 /*It's null? Then pick a default*/
just=left(justify,1) /*only use 1st capital letter*/
if width==0 then width=40 /*Not determinable? Then use 40.*/

if width=='' then width=linesize()%2 /*null? Then pick a default.*/
if width==0 then width=40 /*not determinable? Use 40.*/


txt="Diplomacy is the art of saying 'Nice Doggy' until",
txt="Diplomacy is the art of saying 'Nice Doggy' until",
"you can find a rock. ─── Will Rodgers"
"you can find a rock. ─── Will Rodgers"


$='' /*this is where the money is. */
/*─────────────────────────────────────where da rubber meets da road*/
$='' /*this is where the money is.*/


do k=1 for words(txt) /*parse 'til we exhaust TXT. */
do k=1 for words(txt); x=word(txt,k) /*parse 'til we exhaust the TXT. */
x=word(txt,k) /*get a word from TXT string.*/
_=$ x /*append it to da money and see. */
if length(_)►width then call tell /*word(s) exceeded the width? */
_=$ x /*append it to da money & see*/
$=_ /*the new words are OK so far. */
if length(_)>width then call tell /*word(s) exceeded the width?*/
$=_ /*the new words are OK so far*/
end
end


call tell /*handle any residual words. */
call tell /*handle any residual words. */
exit /*let's go home & celebrate. */
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────TELL subroutine─────────────────────*/

tell: if $=='' then return /*first word may be too long. */
/*─────────────────────────────────────TELL subroutine──────────────*/
tell: if $=='' then return /*first word may be too long.*/
select
select
when just=='B' then $=justify($,width) /*<───both───>*/
when just=='B' then $=justify($,width) /*◄────both────►*/
when just=='C' then $= center($,width) /* <centered> */
when just=='C' then $= center($,width) /* ◄centered► */
when just=='R' then $= right($,width) /*──────>right*/
when just=='R' then $= right($,width) /*──────► right */
otherwise $= strip($) /*left<───────*/
otherwise $= strip($) /*left ◄────────*/
end
end /*select*/
say $ /*show&tell, or write──>file?*/
say $ /*show and tell, or write──►file?*/
_=x /*handle any word overflow. */
_=x /*handle any word overflow. */
return /*go back and keep truckin'. */
return /*go back and keep truckin'. */</lang>
</lang>
The input file:
The input file:
<pre style="height:15ex;overflow:scroll">
<pre style="height:15ex;overflow:scroll">