Hello,
I just finished these functions for a project I'm working on at the moment and thought they may be of some use to someone else too.
So here they are, a set of functions for text wrapping a long string of text:
Commands:
TextWrapInit()
TextWrapClear()
TextWrap(TextWidth as integer, TextString as string)
TextWrapEnd()
Functions:
//===FUNCTIONS===
//This function initates the text wrapper
Function TextWrapInit()
Global Dim TextWrapOutput(1) as string
EndFunction
//This function wraps the text to a given width and handles formatting
Function TextWrap(TextWidth as integer, TextString as string)
Local EndOfLastString as integer =0
Local ArrayLength as integer =1
Local temp$ as string =""
//Clear the TextWrapOutput() array from last time it was used
TextWrapClear()
//Cycle through each character in the string
for t=1 to len(TextString)
//If the length of the next line-string is greater than TextWidth
if len(left$(TextString,t))-EndOfLastString>TextWidth
//Search back until a space is found
for c=t to 1 step -1
//Space found
if mid$(TextString,c)=" "
//Add each character from the end of the last line-string to the space
for s=EndOfLastString+1 to c-1
TextWrapOutput(ArrayLength)=TextWrapOutput(ArrayLength)+mid$(TextString,s)
next s
//Set the end of the last line-string
EndOfLastString=c
//Increment the length of the array
inc ArrayLength,1
//Redim the array
dim TextWrapOutput(ArrayLength)
//Exit the for-loop
c=-1
endif
next c
else
//Text formatting found
if mid$(TextString,t)="["
//Read foward until the next square bracket is found
for c=t to len(TextString)
if mid$(TextString,c)="]"
//Reset the temp$ variable
temp$=""
//Grab all text inbetween
for s=t+1 to c-1
temp$ = temp$+mid$(TextString,s)
next s
//Analyze formatting
select temp$
case "CR" //Carrige return
//Save line string until that point
for s=EndOfLastString+1 to t-1
TextWrapOutput(ArrayLength)=TextWrapOutput(ArrayLength)+mid$(TextString,s)
next s
//Increment the length of the array
inc ArrayLength,1
//Redim the array
dim TextWrapOutput(ArrayLength)
//Continue from other side of formatting
t=EndOfLastString
//New end of string position
EndOfLastString=c+1
endcase
// REMSTART
// Other cases can be added here for other kinds
// of formatting and for adding text from variables
// to the text; here's an example:
// REMEND
case "V1" //Value of variable 1 in place of the original "[V1]"
//Replace text with variable value
TextString=left$(TextString,t-1)+str$(Variable01)+right$(TextString,len(TextString)-(c+1))
endcase
// REMSTART
// With this case you can only use global variables,
// otherwise they will be printed as zero. This formatting
// system also has many other applications besdies variables
// such as tabbing and pretty much anything you can think of.
// REMEND
endselect
//Exit for-loop
c=len(TextString)+1
endif
next c
endif
endif
next t
//Add end of text-string to print que manually because it will be shorter than TextWidth and so will not be caught by the if function.
if len(TextString)>EndOfLastString
for s=EndOfLastString+1 to len(TextString)
TextWrapOutput(ArrayLength)=TextWrapOutput(ArrayLength)+mid$(TextString,s)
next s
endif
EndFunction
//This function clears the TextWrapOutput variable ready for use
Function TextWrapClear()
Global Dim TextWrapOutput(1) as string
TextWrapOutput(1)=""
EndFunction
//This function releases resources used by the text wrapper
Function TextWrapEnd()
UnDim TextWrapOutput(1)
EndFunction
Example (no media, fully commented):
//Take control of the screen
sync on
//Make global variable to print later
Global Variable01 as integer =45
//Initate the functions
TextWrapInit()
//Write a string to be text wrapped
ExampleString$ = "This function can wrap text and handle manual addition of carraige returns. This is a double carraige return: [CR] [CR] Adding functionality for other types of formatting and variable inclusion is easy with this system. For example: this is variable01: [V1] . [CR] [CR] Easy Text Wrapper by Broken_Code [CR] [CR] Press any key to continue."
//Wrap text
//TextWrap(Number of characters per line, String to text wrap)
TextWrap(50,ExampleString$)
//Print to screen
print ""
for t=1 to array count(TextWrapOutput(0))
print TextWrapOutput(t)
next t
//Sync the screen
sync
//Sync the screen again so that someting shows on the first loop
sync
//Wait for key press
wait key
//Let the user try their own text
do
//Clear the screen
cls
//User input
input "Enter your own text to be wrapped> ",ExampleString$
//Wrap text
//TextWrap(Number of characters per line, String to text wrap)
TextWrap(50,ExampleString$)
//Print to screen
print ""
for t=1 to array count(TextWrapOutput(0))
print TextWrapOutput(t)
next t
//Sync the screen
sync
//Wait for key press
wait key
loop
//End text wrap and release resources
TextWrapEnd()
//End program
end
//===FUNCTIONS===
//This function initates the text wrapper
Function TextWrapInit()
Global Dim TextWrapOutput(1) as string
EndFunction
//This function wraps the text to a given width and handles formatting
Function TextWrap(TextWidth as integer, TextString as string)
Local EndOfLastString as integer =0
Local ArrayLength as integer =1
Local temp$ as string =""
//Clear the TextWrapOutput() array from last time it was used
TextWrapClear()
//Cycle through each character in the string
for t=1 to len(TextString)
//If the length of the next line-string is greater than TextWidth
if len(left$(TextString,t))-EndOfLastString>TextWidth
//Search back until a space is found
for c=t to 1 step -1
//Space found
if mid$(TextString,c)=" "
//Add each character from the end of the last line-string to the space
for s=EndOfLastString+1 to c-1
TextWrapOutput(ArrayLength)=TextWrapOutput(ArrayLength)+mid$(TextString,s)
next s
//Set the end of the last line-string
EndOfLastString=c
//Increment the length of the array
inc ArrayLength,1
//Redim the array
dim TextWrapOutput(ArrayLength)
//Exit the for-loop
c=-1
endif
next c
else
//Text formatting found
if mid$(TextString,t)="["
//Read foward until the next square bracket is found
for c=t to len(TextString)
if mid$(TextString,c)="]"
//Reset the temp$ variable
temp$=""
//Grab all text inbetween
for s=t+1 to c-1
temp$ = temp$+mid$(TextString,s)
next s
//Analyze formatting
select temp$
case "CR" //Carrige return
//Save line string until that point
for s=EndOfLastString+1 to t-1
TextWrapOutput(ArrayLength)=TextWrapOutput(ArrayLength)+mid$(TextString,s)
next s
//Increment the length of the array
inc ArrayLength,1
//Redim the array
dim TextWrapOutput(ArrayLength)
//Continue from other side of formatting
t=EndOfLastString
//New end of string position
EndOfLastString=c+1
endcase
// REMSTART
// Other cases can be added here for other kinds
// of formatting and for adding text from variables
// to the text; here's an example:
// REMEND
case "V1" //Value of variable 1 in place of the original "[V1]"
//Replace text with variable value
TextString=left$(TextString,t-1)+str$(Variable01)+right$(TextString,len(TextString)-(c+1))
endcase
// REMSTART
// With this case you can only use global variables,
// otherwise they will be printed as zero. This formatting
// system also has many other applications besdies variables
// such as tabbing and pretty much anything you can think of.
// REMEND
endselect
//Exit for-loop
c=len(TextString)+1
endif
next c
endif
endif
next t
//Add end of text-string to print que manually because it will be shorter than TextWidth and so will not be caught by the if function.
if len(TextString)>EndOfLastString
for s=EndOfLastString+1 to len(TextString)
TextWrapOutput(ArrayLength)=TextWrapOutput(ArrayLength)+mid$(TextString,s)
next s
endif
EndFunction
//This function clears the TextWrapOutput variable ready for use
Function TextWrapClear()
Global Dim TextWrapOutput(1) as string
TextWrapOutput(1)=""
EndFunction
//This function releases resources used by the text wrapper
Function TextWrapEnd()
UnDim TextWrapOutput(1)
EndFunction
This wrapper is also capable of carraige returns and variable recognition (although this has to be variable-specificly coded- see example for details) and has the ability to add any formatting you like.
I hope this is of some use to somebody,
Broken_Code