Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: how to strip VT100 escape sequences?

by Fastolfe (Vicar)
on Dec 19, 2001 at 07:44 UTC ( [id://133012]=note: print w/replies, xml ) Need Help??


in reply to how to strip VT100 escape sequences?

You can always try it and see if any escape sequences get left behind. I agree that just "assuming" that this is how a VT100 escape sequence is always represented is a bad idea, but until you find authoritative information to tell you otherwise, it's pretty easy to write some code to strip what you do have out. You've very nearly stated your requirements:

  1. A literal \033 (or \e in Perl)
  2. A literal bracket [ (I added this)
  3. One or more (digits, commas, spaces)
  4. A letter

Pitting this against perlre, we can construct this:

/\e # ESC \[ # [ [\d,\s] # one of: digit, comma, whitespace + # at least 1 [A-Z] # letters from A-Z /ix # case-insensitive # or: /\e\[[\d,\s]+[A-Z]/i

Put that in the left-hand-side of a substitution operator and you have yourself a functional escape code stripper.

Replies are listed 'Best First'.
Re: Re: how to strip VT100 escape sequences?
by chip (Curate) on Dec 19, 2001 at 12:19 UTC
    Well, Fastolfe, VT100 escape sequences need not have any parameter value at all, in which case the terminating letter immediately follows the left square bracket. (In this case, any numeric value is taken to be zero.) Thus you need a "*" instead of a "+".

    And I'd be really surprised if lower case letters can't end a sequence.

    And I'm pretty sure there are some sequences that end with punctuation marks.

    And I'm fairly sure that multiple parameters are separated with semicolons, not commas.

    Ah, forget it! I'm gonna fire up my old H19 terminal and leave all this DEC hair far behind....

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      You appear to have remembered correctly, chip, except that apparently no escape sequence ends in a punctuation mark.

      Interestly enough, an examination of CPAN shows that there is no module that currently does this. Writers, to your keyboards!

      --
      g r i n d e r
      just another bofh

      print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';
      I'm sure the original poster appreciates the tips.. Though my post wasn't so much on how to do what he was doing (strip VT100 escape sequences), but how he might figure it out for himself. I didn't do anything beyond his requirements deliberately (because I can't speak for all escape sequences).
        I appreciate it; it is true that your input answers exactly my specifications.
        However I wasn't sure about the VT100 sequences, and I appreciate the other inputs.
        Meaningwhile a friend pointed me to a linux man page specifying the VT102 sequences : man console_codes, and it appears that other formats exist; for example: ESC > for selecting numeric keypad mode, ESC % for selecting character set, etc. However it seems that the control sequences always have the same format, I quote from man console_codes:
        • CSI (or ESC [) is followed by a sequence of parameters, at most NPAR (16), that are decimal numbers separated by semicolons. An empty or absent parameter is taken to be 0. The sequence of parameters may be preceded by a single question mark.
        • However, after CSI [ (or ESC [ [) a single character is read and this entire sequence is ignored. (The idea is to ignore an echoed function key.)
        • The action of a CSI sequence is determined by its final character. "
        This final character may be @, A..Z, a..z, and ` (backquote)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://133012]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 04:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found