Attempting to parse free-form ANSI text.
Frederic Rentsch
anthra.norell at vtxmail.ch
Sun Oct 22 05:00:59 EDT 2006
Michael B. Trausch wrote:
> Alright... I am attempting to find a way to parse ANSI text from a
> telnet application. However, I am experiencing a bit of trouble.
>
> What I want to do is have all ANSI sequences _removed_ from the output,
> save for those that manage color codes or text presentation (in short,
> the ones that are ESC[#m (with additional #s separated by ; characters).
> The ones that are left, the ones that are the color codes, I want to
> act on, and remove from the text stream, and display the text.
>
> I am using wxPython's TextCtrl as output, so when I "get" an ANSI color
> control sequence, I want to basically turn it into a call to wxWidgets'
> TextCtrl.SetDefaultStyle method for the control, adding the appropriate
> color/brightness/italic/bold/etc. settings to the TextCtrl until the
> next ANSI code comes in to alter it.
>
> It would *seem* easy, but I cannot seem to wrap my mind around the idea.
> :-/
>
> I have a source tarball up at http://fd0man.theunixplace.com/Tmud.tar
> which contains the code in question. In short, the information is
> coming in over a TCP/IP socket that is traditionally connected to with a
> telnet client, so things can be broken mid-line (or even mid-control
> sequence). If anyone has any ideas as to what I am doing, expecting, or
> assuming that is wrong, I would be delighted to hear it. The code that
> is not behaving as I would expect it to is in src/AnsiTextCtrl.py, but I
> have included the entire project as it stands for completeness.
>
> Any help would be appreciated! Thanks!
>
> -- Mike
>
*I have no experience with reading from TCP/IP. But looking at your
program with a candid mind I'd say that it is written to process a chunk
of data in memory. If, as you say, the chunks you get from TCP/IP may
start and end anywhere and, presumably you pass each chunk through
AppendText, then you have a synchronization problem, as each call resets
your escape flag, even if the new chunk starts in the middle of an
escape sequence. Perhaps you should cut off incomplete escapes at the
end and prepend them to the next chunk.
And:
if(len(buffer) > 0):
wx.TextCtrl.AppendText(self, buffer) <<< Are you sure text goes
into the same place as the controls?
if(len(AnsiBuffer) > 0):
wx.TextCtrl.AppendText(self, AnsiBuffer) <<< You say you want to
strip the control sequences
Frederic
*
More information about the Python-list
mailing list