-3

I just finished working on menu generator, but I need to add warning message or popup like this one :

Warning Message

the warning message, like the other elements of menu, should have same width of 80 (inner width is 78, because border takes two). I built a function with one parameter which can easy generate that message for the text that will be displayed. The problem is when I put text longer than 78 characters, I get errors. I want to split it into two (or more depending on how many lines we would get) parameters cause no one will count to 78 on each parameter/line. I'm looking for a possibility to split text into two or more lines to fit the inner width of 78.

Since this time I decided to split text with " "(space) separator

$textsplit = $text.Split(" ")

Then I decided to add each element of $textsplit to an array using a Do-Until loop

$warningmsgline1.Add($textsplit[$i])
$warningmsgline1.Add(" ")

to make a new variable that will contain a sentence (words) that contains less then 78 characters.

I hope you are keeping up :)

How can I create such a condition? Nested loops? What kind of loops? For? Do-Until?

Feel free to ask if something is unclear.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Kenji Zet
  • 1
  • 3
  • Hi Kenji, I can help you on this provided you ask exact question. Could not understand your requirement completely – Vijay May 04 '17 at 20:10
  • Something like [this](http://stackoverflow.com/questions/11348506/split-string-with-powershell-and-do-something-with-each-token)? – Jeroen Heier May 04 '17 at 20:12
  • As i thought :) Hard to understand :) Let me explain : I need to split whole warning msg into lines. Each of lines have 78signs and i want to split it thru words. F.e : "During this connection, the OnCloud commands must be used with a "Cloud" prefix such as Get-CloudDistributionGroup." Must be splitted into two lines : 1 : During this connection, the OnCloud commands must be used with a "Cloud" 2 : prefix such as Get-CloudDistributionGroup. What You think ? – Kenji Zet May 05 '17 at 13:09

1 Answers1

0

I am a little fuzzy on understanding your question. But if I understand you correctly, you want to handle line wrapping at a max of 78 characters? This is not complete, but should set you in the right direction.

$message = "This is a really long message that is longer than 80 characters. It will need to be wrapped onto a second line."

$wordArray = $message.Split(' ')

$output = @()
$currentLine = ""

$wordArray | ForEach-Object {
    if ($currentLine.Length + 1 + $_.Length -le 78) {
        $currentLine = "$currentLine $_"
    }   
    else {
        $output += $currentLine
        $currentLine = $_   
    }
}

## Add remainder to output
$output += $currentLine 

$output
Cory Knutson
  • 164
  • 8