WRAP

If all you want is a simple routine to wrap som text in a given window, then this little number - more or less as it was typed in from some magazine back in the day - will, for most practical purposes, do the trick:

100 DEFine PROCedure Wrap(ch,str$)
110 LOCal first,second,loop,screen$(40)
120 IF LEN(str$)=0:RETurn
130 first=0
140 REPeat loop
150  first=first+1
160  FOR second=first TO LEN(str$)
170   IF str$(second)=' ' THEN
180    screen$=str$(first TO second-1)
190    PRINT#ch;!screen$;
200    first=second
210    EXIT second
220   END IF
230   IF second=LEN(str$) THEN
240    screen$=str$(first TO LEN(str$))
250    PRINT#ch;!screen$;
260    EXIT loop
270   END IF
280  END FOR second
290  IF first=LEN(str$):EXIT loop
300 END REPeat loop
310 END DEFine Wrap
320 :
  

Its not perfect, but it works. It uses S*BASIC's line handling rules to do its magic. Great if you know exactly what text is to be displayed, and test it beforehand so you know it looks ok.

This next routine, the procedure Wrap, has a few extras thrown in. It also breaks lines at the last possible space character, but the line length can be shorter than the width of the window. Also it allows for linefeeds, which you indicate by including actual linefeed characters, CHR$(10), in your string.

The final routine, the function Wrap%, doesnt actually display anything. It merely emulates the function of Wrap, above, and returns the number of lines required to display a given string. That can be useful for popup help text windows, for example, so they can be accurately dimensioned to the size of the text. Additionally, it can translate alternative characters into the linefeed character. In the example given, the hash character, #, is used.

None of these three routines cope well with words longer than the length of a line, so if that is a requirement, then youll need to make the necessary modifications.. Another useful modification might be to add a left margin. SuperBASIC users will need to change the FOR variable i% to a float.

10 rem Wrap - Routines to wrap a string:
11 rem Estimate size of wrapped output,
12 rem then print lines wrapped at space/char
13 :
14 rem V0.03, pjw, February 19th 2017
15 rem V0.04, pjw, June 21st 2019, bugfix and simplification
16 :
17 rem     Simple harness.  EX or LRUN to test.
18 widthchr% = 30:         rem Desired with in char
19 str$ = 'This is a test of a long<LF>' & CHR$(10) & 'string with a '
20 str$ = str$ & 'linefeed<LF>#or two<LF>#'
21 str$ = str$ & 'thrown in for good measure. Here comes the rest '
22 str$ = str$ & 'of this string.'
23 :
24 hichr% = Wrap%(widthchr%, '#', str$): rem Number of lines in char
25 wsx%   = widthchr% *  6: rem Required window dimensions in pix
26 wsy%   = hichr%    * 10
27 :
28 ch = FOPEN("con_")
29 OUTLN#ch; wsx%, wsy%, 20, 20: CLS#ch
30 Wrap#ch; widthchr%, str$
31 PAUSE#ch: CLOSE#ch
32 :
33 wsx%   = widthchr% *  8: rem Required window dimensions in pix
34 wsy%   = hichr%    * 20
35 :
36 ch = FOPEN("con_")
37 OUTLN#ch; wsx%, wsy%, 20, 20: CLS#ch: CSIZE#ch; 1, 1
38 Wrap#ch; widthchr%, str$
39 PAUSE#ch: CLOSE#ch
40 :
41 :
100 rem + ------------------------------------------------------------------------ +
102 rem |<                                  Wrap                                  >|
104 rem + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
106 rem |           Wrap long lines at given width, breaking at <SPACE>            |
108 rem |                                                                          |
110 rem | This version works on a character-by-character basis, and caters for LF  |
112 rem | Use Wrap% to calculate line height and replace alternative split         |
114 rem | characters with LF. If Wrap% not used, only LF counts as split character |
116 rem + ------------------------------------------------------------------------ +
118 rem | V0.03, pjw, February 19th 2017, Complete re-write                        |
120 rem | V0.04, pjw, June 21st 2019, bug fix                                      |
122 rem + ------------------------------------------------------------------------ +
124 :
126 DEFine PROCedure Wrap(ch, width%, text$)
128 LOCal i%, p%, e%, l$
130 :
132 l$ = '': p% = 0: e% = LEN(text$)
134 FOR i% = 1 TO e%
136  IF p% > width% THEN
138   BPUT#ch; l$(1 TO e% - 1), 10
140   IF e% = LEN(l$) THEN
142    l$ = text$(i%)
144   ELSE
146    l$ = l$(e% + 1 TO LEN(l$)) & text$(i%)
148   END IF
150   e% = LEN(l$)
152   p% = e% + 1
154  ELSE
156   p% = p% + 1
158   l$ = l$ & text$(i%)
160  END IF
162  IF text$(i%) = ' ' THEN
164   e% = LEN(l$)
166  ELSE
168   IF text$(i%) = CHR$(10): p% = 1
170  END IF
172 END FOR i%
174 BPUT#ch; l$
176 END DEFine Wrap
178 :
180 :
182 rem + ------------------------------------------------------------------------ +
184 rem |<                               Wrap Count                               >|
186 rem + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
188 rem |          Calculate number of lines a wrapped string will take            |
190 rem |                                                                          |
192 rem | Complimentary routine to Wrap                                            |
194 rem | NB! May modify text$ string, as LF or other split characters are         |
196 rem | replaced with LF!                                                        |
198 rem + ------------------------------------------------------------------------ +
200 rem | V0.01, pjw, June 30th 2017                                               |
202 rem | V0.02, pjw, June 21st 2019, exchanging split char for LF simplifies      |
204 rem + ------------------------------------------------------------------------ +
206 :
208 DEFine FuNction Wrap%(width%, split$, text$)
210 LOCal i%, p%, e%, h%, l$, s$
212 :
214 h% = 1: s$ = CHR$(10) & split$
216 l$ = '': p% = 0: e% = LEN(text$)
218 FOR i% = 1 TO e%
220  IF p% > width% THEN
222   h% = h% + 1
224   IF e% = LEN(l$) THEN
226    l$ = text$(i%)
228   ELSE
230    l$ = l$(e% + 1 TO LEN(l$)) & text$(i%)
232   END IF
234   e% = LEN(l$)
236   p% = e% + 1
238  ELSE
240   p% = p% + 1
242   l$ = l$ & text$(i%)
244  END IF
246  IF text$(i%) = ' ' THEN
248   e% = LEN(l$)
250  ELSE
252   IF text$(i%) INSTR s$ THEN
254    text$(i%) = CHR$(10):        rem Exchange split char
256    h% = h% + 1: p% = 1
258   END IF
260  END IF
262 END FOR i%
264 RETurn h%
266 END DEFine Wrap%
268 :
270 :
Generated with sb2htm on 2019 Jun 21
©pjwitte March 2oi9