I made a script that sends text to a AS400 Computer over a Terminalprogram. Therefor I had to wrap text (and put this text in special form: Ctrl as Enter,linenumbers etc ..)
So I searched for a wrap algorithm. I found Text wrapper.ahk but it was a little bit to complicated for me. Now I have made my own wrap function.
Code:
wrap(String,maxWrapLength,separator = "",showSeparator = "") {
; global endChar
endChar := "`n"
beforewrap := ""
str1 := ""
StringReplace, String1, String, `r`n, %A_Space%, All
StringReplace, String1, String1, `n,%A_Space% , All
If (separator != "")
StringReplace, String1, String1, %separator%,%endChar%%separator%, All
Loop Parse, String1, `n
{
str :=""
length := 0
line := A_loopfield
Loop Parse, line, %A_Space%
{
beforewrap := str
str := str . A_loopfield . A_Space
lengthLoopfield := StrLen(A_loopfield)
length := length + lengthLoopfield +1
If length > %maxWrapLength%
{
str := beforewrap . endChar . A_loopfield . A_Space
length := StrLen(A_loopfield) +1
}
}
If (str1 = "")
str1 := str1 . str
Else
str1 := str1 . endChar . str
}
If (showSeparator = "0")
StringReplace, str1, str1,%separator%,, All
Return str1
}
Code:
TexttoWrap = hallo1 ha+l++lo2 hallo3 hallo4 hallo5
test := wrap(TexttoWrap,20,"+",0 )
MsgBox, ::::wrap:::: `n`n%test%
MsgBox, % "::::wrap::::`n`n" wrap(TexttoWrap,20,"+",1 )
MsgBox, % "::::wrap::::`n`n" wrap(TexttoWrap,20,"",1 )
I hope someone will find this helpful.
If you find Errors or a better way to do this, please comment it.