Coruscate has asked for the wisdom of the Perl Monks concerning the following question:
I was asking about this yesterday in the chatterbox, but that got almost nowhere, so I shall post here in hopes that I might get some advice on how to accomplish this task.
I am sending output to a telnet client and wish to wrap the text sent on the 80th column, wrapping, of course, only on word boundaries. In my case, wrapping on spaces is good enough, since tab characters will not count. And newlines should be sent as is, not removed. The following code does just this, but there is one complication I have invented to challenge this snippet:
use Text::Wrap; $Text::Wrap::columns = 80; # Modified print() sub output { my $text = shift; my $wrapped = wrap('', '', $text); $wrapped =~ s/\n/\r\n/g; print $wrapped; }
Okay, so that works fine. Text is boundary-wrapped at a max of 80 columns. Perfect. What's the problem? I am now adding special tty escape sequences that clear the screen, change ansi colors, etc etc. These sequences look like \e[2K, \e[37;40m, \e[0m, etc etc. The ones I use all match the regex m#\e\[[\d;]+[mK]#, if that helps whoever may find a way to aid me in my quest :)
So what I need to do is still wrap at 80 columns, while ignoring these special characters in the call to Text::Wrap::wrap(). The problem is that if I pass a string such as \e[33mCoruscate\e[0m;\e[37;40m is having trouble with a script he is working on and therefore makes a trip to \e[32mwww.perlmonks.org\e[0m;\e[37;40m to seek help. to the output() function, it will wrap in the terminal much sooner than I want. It will wrap at a visual 44 columns instead of the wanted 80 (these escape sequences are not physically shown in the terminal. The telnet client takes these out and does special magic behind the scenes). That's because those escape sequences take up 36 columns in my example. Text::Wrap::wrap() doesn't know that it is suppose to exclude these sequences, so it counts them as being there (which you would expect).
My question: Is there a Text::Wrapish type module that allows you to pass along certain strings or patterns which are to be ignored when calculating where to wrap the text? Or perhaps someone smarter more knowledgeable than me in this sort of thing can give me direction or code samples as how to overcome this terrible monster!
If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Wrap while ignoring certain sequences (from CB)
by tye (Sage) on Mar 12, 2003 at 21:47 UTC | |
by Coruscate (Sexton) on Mar 13, 2003 at 05:27 UTC | |
|
Re: Wrap while ignoring certain sequences
by Enlil (Parson) on Mar 12, 2003 at 22:08 UTC | |
by hv (Prior) on Mar 12, 2003 at 22:32 UTC |