Jump to content

msgbox line break in a variable or wrap


Recommended Posts

Basically the text portion in the msgbox is being placed there by a $variable. I need to limit the length of the msgbox, so I need to be able to either break the lines after a certain length or preferably wrap the text(because breaking the text in the variable with @crlf after a certain amount of text could cut words off). I prefer not to use a gui pop up style for this and stick with a msgbox. Is this possible?

Edited by Champak
Link to comment
Share on other sites

I thought about stringlen, but that counts characters and would/could cause words to be split up. But now I'm thinking maybe I can do _StringInsert @crlf if it is a space. If it is not a space at that point, search for the first space after the _StringInsert until the end of the string. Now I just have to figure out how the hell to do that.

Link to comment
Share on other sites

#include <array.au3>
$Text = "THIS IS A REALLY LONG TEXT LINE THAT WOULD MAKE A REALLY LONG MESSAGE BOX" & _
" AND SO WE WANT TO WORD WRAP IT BUT NOT CUT OFF THE WORDS! IT IS 155 CHARS LONG!!!"
$CharsPerLine = 15
MsgBox(0, "", StringBreakAtWords($Text, $CharsPerLine,1,@CRLF))

Func StringBreakAtWords($String, $Maxchars, $ReturnString = 1, $Delimiter = @CRLF)
$Pattern = ".{"&$Maxchars&"}\b|.{1,"&$Maxchars&"}\b"
$Lines = StringRegExp($String, $Pattern, 3)
If Not $ReturnString then Return $Lines
Dim $Result=""
For $i = 0 to UBound($Lines)-1
    $Result &= $Lines[$i]&$Delimiter
Next
Return StringTrimRight($Result,1)
EndFunc

Edited by Paulie
Link to comment
Share on other sites

That's great. Thanks....I know I was about to take at least an hour to figure this one out. What is the "$ReturnString = 1" for?

EDIT: I think this should definately be a part of autoit's UDF....maybe under misc.., or make a new UDF section for msgbox because I saw a nice one by Smoke where he changes the button text...I'm sure there are a few more useful msgbox functions floating around.

Edited by Champak
Link to comment
Share on other sites

That's great. Thanks....I know I was about to take at least an hour to figure this one out. What is the "$ReturnString = 1" for?

EDIT: I think this should definately be a part of autoit's UDF....maybe under misc.., or make a new UDF section for msgbox because I saw a nice one by Smoke where he changes the button text...I'm sure there are a few more useful msgbox functions floating around.

If $return string = 0 then it would just return an array of the lines. The string is post delimited
Link to comment
Share on other sites

I added the ability to be able to insert carriage returns.

add "\r" after that first "\b"

$Pattern = ".{"&$Maxchars&"}\b|.{1,"&$Maxchars&"}\b"

But there are three problems.

1/ The script seems to be inserting carriage returns/line breaks in some odd places sometimes.

Here is an example:

#include <array.au3>
$Text0 = "The start of everything"
$Text1 = "Basically the text portion in the msgbox is being placed there by a $variable. I need to limit the length of the msgbox, so I need to be able to either break the lines after a certain length or preferably wrap the text(because breaking the text in the variable with @crlf after a certain amount of text could cut words off). I prefer not to use a gui pop up style for this and stick with a msgbox. Is this possible?"
$Text2 = "The last line not really saying anything. THIS IS A REALLY LONG TEXT LINE THAT WOULD MAKE A REALLY LONG MESSAGE BOX" & _
" AND SO WE WANT TO WORD WRAP IT BUT NOT CUT OFF THE WORDS! IT IS 155 CHARS LONG!!!"


$CharsPerLine = 70
MsgBox(48, "ba", StringBreakAtWords($Text0 & @CR & _
                                    $Text1 & @CR & _
                                    $Text2, $CharsPerLine,1,@CRLF))

Func StringBreakAtWords($String, $Maxchars, $ReturnString = 1, $Delimiter = @CRLF)
$Pattern = ".{"&$Maxchars&"}\b\r|.{1,"&$Maxchars&"}\b"
$Lines = StringRegExp($String, $Pattern, 3)
If Not $ReturnString then Return $Lines
Dim $Result=""
For $i = 0 to UBound($Lines)-1
    $Result &= $Lines[$i]&$Delimiter
Next
Return StringTrimRight($Result,1)
EndFunc

The line unnecessarily gets split right after: "Basically the text portion in the msgbox is"

and: The last line not really saying anything.

2/ The followin line sometimes begins with a space which makes things sometimes uneven(I could probobly fix this)

3/ Punctuations right at the end of a word sometimes gets put to the next line...doesn't really make sense. It would make more sense if that last word with the punctuation ". , ! ? )" gets put to the next line.

Edited by Champak
Link to comment
Share on other sites

If $return string = 0 then it would just return an array of the lines. The string is post delimited

I'm sorry, I still don't get it. I don't see what that has to do with regexp array. When I remove it, it still returns everything It should in the array. It seems like an arbitrary variable with some number assigned to it. Almost like saying "What does it have to do with the price of tea in China?"
Link to comment
Share on other sites

Well, I can help you with number 1.

1) you were only calling the function once for all three separate portions of text. One function call should be used for each 'portion' of the text:

#include <array.au3>
$Text0 = "The start of everything"
$Text1 = "Basically the text portion in the msgbox is being placed there by a $variable. I need to limit the length of the msgbox, so I need to be able to either break the lines after a certain length or preferably wrap the text(because breaking the text in the variable with @crlf after a certain amount of text could cut words off). I prefer not to use a gui pop up style for this and stick with a msgbox. Is this possible?"
$Text2 = "The last line not really saying anything. THIS IS A REALLY LONG TEXT LINE THAT WOULD MAKE A REALLY LONG MESSAGE BOX" & _
" AND SO WE WANT TO WORD WRAP IT BUT NOT CUT OFF THE WORDS! IT IS 155 CHARS LONG!!!"


$CharsPerLine = 70
MsgBox(48, "ba", StringBreakAtWords($Text0, $CharsPerLine,1,@CRLF) & @CR & _
                                    StringBreakAtWords($Text1, $CharsPerLine,1,@CRLF) & @CR & _
                                    StringBreakAtWords($Text2, $CharsPerLine,1,@CRLF))

Func StringBreakAtWords($String, $Maxchars, $ReturnString = 1, $Delimiter = @CRLF)
$Pattern = ".{"&$Maxchars&"}\b\r|.{1,"&$Maxchars&"}\b"
$Lines = StringRegExp($String, $Pattern, 3)
If Not $ReturnString then Return $Lines
Dim $Result=""
For $i = 0 to UBound($Lines)-1
    $Result &= $Lines[$i]&$Delimiter
Next
Return StringTrimRight($Result,1)
EndFunc
Link to comment
Share on other sites

I'm sorry, I still don't get it. I don't see what that has to do with regexp array. When I remove it, it still returns everything It should in the array. It seems like an arbitrary variable with some number assigned to it. Almost like saying "What does it have to do with the price of tea in China?"

It is simply a formatting thing... Perhaps you wanted to split the string, and then only use the first element in the array (or the first line)

you would set $ReturnString = 0 so that it would return an array instead of a string, then just take the [0] element of that array.

If you leave $ReturnString = 1 then you will get a delimited string, and the delimiter parameter would be so you could choose the delimiter.

Basically that parameter is asking "Do you want an array or a string?"

Link to comment
Share on other sites

Fixed 2 with replacing the loop:

For $i = 0 to UBound($Lines)-1
    If StringLeft($Lines[$i], 1) = " " Then
        $Lines2 = StringTrimLeft($Lines[$i], 1)
    Else
        $Lines2 = $Lines[$i]
    EndIf
    $Result &= $Lines2&$Delimiter
NextoÝ÷ Ù*-¬²ç¬çíþ«¨µà^¶×«²n¶*'jëh×6Func StringBreakAtWords($String, $Maxchars, $ReturnString = 1, $Delimiter = @CRLF)
$Pattern = ".{"&$Maxchars&"}\b|.{1,"&$Maxchars&"}\b"
$Lines = StringRegExp($String, $Pattern, 3)
If Not $ReturnString then Return $Lines
Dim $Result=""
For $i = 0 to UBound($Lines)-1
    $Lines[$i] = StringStripWS($Lines[$i],3)
    $Result &= $Lines[$i]&$Delimiter
Next
Return StringTrimRight($Result,1)
EndFunc
Link to comment
Share on other sites

@danwilli

great, simple solution.

@Paulie

Oohhhhhh, I totally missed the "Return" part in the

If Not $ReturnString then Return $Lines

I was blanking out and thinking the fucntion was continuing. I got it now.

Better solution:

Absolutely. I forgot that StringStripWS has parameters.

EDIT: So just need to figure the punctuation issue and all is Good.

Edited by Champak
Link to comment
Share on other sites

One more thing, I'm not seeing what exactly your "\r" is doing for the pattern. I get the same results for the following line both with and without it

#include <array.au3>
$Text = "THIS IS A REALLY "&@CR&" LONG TEXT LINE THAT WOULD MAKE A REALLY LONG MESSAGE BOX" & _
" AND SO WE WANT TO WORD WRAP IT BUT NOT "&@CR&" CUT OFF THE WORDS! IT IS 155 CHARS LONG!!!"
$CharsPerLine = 25
MsgBox(0, "", StringBreakAtWords($Text, $CharsPerLine,1,@CRLF))

Func StringBreakAtWords($String, $Maxchars, $ReturnString = 1, $Delimiter = @CRLF)
$Pattern = ".{"&$Maxchars&"}\b|.{1,"&$Maxchars&"}\b" ; ".{"&$Maxchars&"}\b\r|.{1,"&$Maxchars&"}\b"
$Lines = StringRegExp($String, $Pattern, 3)
If Not $ReturnString then Return $Lines
Dim $Result=""
For $i = 0 to UBound($Lines)-1
    $Lines[$i] = StringStripWS($Lines[$i],3)
    $Result &= $Lines[$i]&$Delimiter
Next
Return StringTrimRight($Result,1)
EndFunc
Edited by Paulie
Link to comment
Share on other sites

One more thing, I'm not seeing what exactly your "\r" is doing for the pattern. I get the same results for the following line both with and without it

Evidently I probably added an extra unnecessary character when I first tried the carriage returns which caused errors. Then I guess when I tried the "fix", I probably then put the carriage return in properly and thought I found a "solution" for a "problem" that wasn't really there.
Link to comment
Share on other sites

So now just need to ensure that punctuation is not on a line split.

Paulie, is there any way this can be in you regexp?

I cant figure out how to do it. nothing i try works :\ maybe one of the regexp geniuses (Smoke_N muttley ) will shine some light on this situation Edited by Paulie
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...