Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Term::ReadLine and Term::ANSIColor don't play well on Windows

by kubrat (Scribe)
on Mar 26, 2013 at 15:56 UTC ( [id://1025547]=perlquestion: print w/replies, xml ) Need Help??

kubrat has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

The following example code works as expected on Linux:

use Term::ANSIColor; use Term::ReadLine; my $term = new Term::ReadLine 'Simple Perl calc'; my $prompt = "Enter your arithmetic expression: "; my $OUT = $term->OUT || STDOUT; while ( defined ($_ = $term->readline($prompt)) ) { $res = eval($_); warn $@ if $@; print $OUT color('green'); print $OUT $res, "\n" unless $@; print $OUT color('reset'); $term->addhistory($_) if /\S/; }

However, on Windows (win7 to be precise) instead of a green coloured $res I get the ANSI escape characters, e.g. <-[32m4<-[0m

The strange thing is that otherwise ANSIColor works on Windows if you print directly to the console.

Peace.

Replies are listed 'Best First'.
Re: Term::ReadLine and Term::ANSIColor don't play well on Windows
by hdb (Monsignor) on Mar 26, 2013 at 16:16 UTC

    From the Term::ANSIColor documentation: "Note that not all displays are ISO 6429-compliant, or even X3.64-compliant (or are even attempting to be so). This module will not work as expected on displays that do not honor these escape sequences, such as cmd.exe, 4nt.exe, and command.com under either Windows NT or Windows 2000. They may just be ignored, or they may display as an ESC character followed by some apparent garbage."

      The problem is that Term::ANSIColor does work on its own and on cmd.exe.

      But it won't work when you try to output colors through Term::ReadLine's OUT handle on cmd.exe. And it is only this combination that does not work. If you try the same thing on Linux then all is well.

      Also, as far as I can tell there are other modules that expected it to work like Devel::REPL for example.
        Even on Linux one is confronted with coloring problems e.g. when trying to have syntax highlighting within the debugger.

        Term::ReadLine is only a wrapper using which ever underlying module is installed. The full list of features is only supplied by Term::ReadLine::Gnu , which builds on a Gnu-Readline programmed in C.

        So what are you using on Win???

        Since finding support for these issues in GNU-Readline isn't easy, I suppose that only reimplementing the full library in Perl can help here.

        Are you volunteering?

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Term::ReadLine and Term::ANSIColor don't play well on Windows
by Anonymous Monk on Mar 26, 2013 at 16:35 UTC
      Even with the win32 console ansi module I get exactly the same effect, .e.g. ←[1;34mla←[0m .
Re: Term::ReadLine and Term::ANSIColor don't play well on Windows
by kubrat (Scribe) on Mar 27, 2013 at 12:28 UTC

    Thanks for that Rolf. I think I understand the problem a bit better now.

    On windows Term::ReadLine::Perl gets loaded. I can't see a way of fixing it in the module itself but working around it is pretty straightforward. I simply print directly to STDOUT instead of the Term OUTs handle which maps to CONOUT$ on Win. You just need to pad your output with a newline to avoid your output appearing on your prompt and it all works.

    Cheers

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-29 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found