Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Interactive post-coloring of readline input?

by LanX (Canon)
on Feb 08, 2012 at 17:48 UTC ( #952537=perlquestion: print w/ replies, xml ) Need Help??
LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

Please give me a hint how to manipulate Term::Readline output after it was already printed.

I suppose the trick is to send a control sequence which erases the line and reprints it.

My aim is to hack a proof of concept for syntax highlighting in the perl debugger when used interactively as a shell

From digging into perl5db.pl I can see that Term::Readline is used for reading and a hashref $rl_attribs is used to provide call-back functions like db_complete()

        $rl_attribs->{completion_function} = \&db_complete;

So is it possible to provide a similar hook which reacts on word_break_characters and then surrounds the already printed word with control-sequences for coloring?

Examples typing "print" does nothing (printed black) but typing: "print " or "print(" activates a call back which modifies "print" to light blue.

Cheers Rolf

Comment on Interactive post-coloring of readline input?
Select or Download Code
Re: Interactive post-coloring of readline input?
by Khen1950fx (Canon) on Feb 09, 2012 at 01:46 UTC
    Use this and step through it.
    #!/usr/bin/perl -d use strict; use warnings; use Term::ReadLine; use Term::ANSIColor; my $attr = "This is a test"; print "$attr", "\n", colored("$attr", "red"), "\n", colored("$attr", "blue"), "\n", colored("$attr", "green"), "\n", colored("$attr", "black"), "\n";
      Thanks, but maybe a misunderstanding.

      I'm using the debugger as a shell/REPL and want to highlight perl keywords while I'm typing them.

      DB<100> print $a;

      DB<101> prin█

      So my question is rather how to hook into Term::ReadLine::Gnu to intercept finished words and surround them with the help of Term::ANSIColor.

      I'm not sure what for you use Term::ReadLine in your example...

      Cheers Rolf

        Actually, you should spend your time reading the manuals for Readline library. For example here .

        Once you understand, which facilities the library provides for interacting with the users input, you should be able to find appropriate front-end functions in Perl. I see there functions like rl_insert_text, rl_delete_text which lead me to belief, that it is possible to do what you want. But personally I've never tried to do it.

Re: Interactive post-coloring of readline input?
by chrestomanci (Priest) on Feb 09, 2012 at 10:41 UTC

    Are you aware of existing work to add syntax highlighting to the perl debugger?

    Ovid has put together a prof of concept alpha quality module DB::Color. It is also on GitHub and he has Blogged about it.

    It sounds like what you are doing does not closely overlap, as you are trying to syntax highlight the user's command line rather code listings, but I think it would be a good idea if you get in contact with Ovid, and discuss how you can share effort to create a single unified tool. For example you don't want to duplicate effort in writing a parser to identify keywords, and you want the same colour scheme to be used in both places.

      > but I think it would be a good idea if you get in contact with Ovid, and discuss how you can share effort to create a single unified tool.

      Thanks for the link... I didn't know that one!. :)

      But are you aware how many Perl-REPL and Perl-Shell projects already exist, where almost nobody teams up? (This post thread is one of many)

      Writing propiatory read-eval-loops is almost as popular as propiatory template engines.

      >It sounds like what you are doing does not closely overlap,

      No, not really. Ovid uses a Kate library for highlighting introspections and single steps by manipulating &DB::DB.

      Cheers Rolf

      BTW: When purely debugging (breakpoint, stepping) I use the emacs frontend. So inspected code is already highlighted out of the box in my color scheme. So Ovid's use case doesn't apply to me.

Re: Interactive post-coloring of readline input? (SOLVED)
by LanX (Canon) on Feb 09, 2012 at 23:08 UTC
    This snippet shows how to define a callback using Term::ReadLine::Gnu which uppercases all "print" words :

    { my $old_line_buffer; $attribs->{event_hook} = sub { my $line_buffer = $attribs->{line_buffer}; return if $line_buffer eq $old_line_buffer; $term->replace_line($line_buffer), $term->redisplay() if ($line_buffer =~s/print\W/\U$&/g); }; }

    HTH!

    Cheers Rolf

    UPDATE: Unfortunately readline doesn't support the interpretation of ANSI escape codes...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (14)
As of 2014-08-21 19:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (143 votes), past polls