15 Jun, 2011, duwnel wrote in the 1st comment:
Votes: 0
First off, RegEx has never been a strong suit of mine, so I'm sorry if this is a bit idiotic.

I've got a little function that looks a bit like this:
def wordWrap(text)
return text.gsub(/(.{1,80})(\s|\Z)/, "\\1\n")
end


This is all part of a set of dynamic help file functions that wraps text in the file after it's been parsed. So if a block is encountered in the help file that says {ansi.color.cyan} for example, it'd insert the escape sequence in that spot. Seeing as how the escape sequence is a number of characters long, each character is counting toward that max of 80.

Any thoughts on how I could exclude an escape sequence match?

Thank you!
16 Jun, 2011, David Haley wrote in the 2nd comment:
Votes: 0
I wouldn't do this with a regular expression; do it by scanning the string left to right counting logical characters (i.e., exclude color codes) until you reach 80 characters.
16 Jun, 2011, Sharmair wrote in the 3rd comment:
Votes: 0
I would also do this by scanning and ignoring non printable chars. However, if
you are fitting this into an 80 char wide screen, output only to 79 char before
the line end. Many clients will put a line end after 80 char and you end up getting
double spaced lines.
16 Jun, 2011, Runter wrote in the 4th comment:
Votes: 0
http://codepad.org/bbVQQZfq
Off the top of my head something like this would work.
16 Jun, 2011, duwnel wrote in the 5th comment:
Votes: 0
Thanks. Problem was with properly finding the non-printable characters. In a string, ANSI codes still maintain characters (The escape character, the brace, one or more characters) all of which are being counted. The solution wasn't difficult, but meant getting the way I wanted to do it out of my head so I could see the way I could do it.

Runter said:
http://codepad.org/bbVQQZfq
Off the top of my head something like this would work.

Runter: Thanks for the reference. It broke me out of my way of thinking.

I don't claim it's good or right, but it's posted here if anyone's curious:

def wordWrap(szText)
lines = []
szText.each_line("\n").each do |l|
if l.gsub(/\e\
szText.each_line("\n").each do |l|
if l.gsub(/\e\[[^m]*m/,"").length > 80
l = l.gsub(/(.{1,80})(\s|\Z)/, "\\1\n")
end
lines << l
end
return lines.join+"\n"
end
[/code]

This wraps on the last space before the word that crosses over the boundary (marked with a constant 80 here) as an arbitrary example. A slight problem is encountered if the ANSI code is attached to the end or in the middle of a word, a wrap may occur one whitespace prior. Not a huge thing to me, since it's still wrapping before the end of the line, but something to still be worked out in it.

Thanks for all the help!
0.0/5