Re: Coloring text using TK?
by Grygonos (Chaplain) on Dec 01, 2003 at 17:41 UTC
|
Without understanding anything about your code, I can only direct you to this Tk-TextANSIColor-0.15 module.
This may well get you on the right track to accomplishing your goal. From the looks of it you would have to parse the contents of the file and insert the ANSI color codes around the words of interest....I don't know for sure, just throwing out an idea from what I read. Also, it says that the module is mainly used in conjuction w/ a tied filehandle
Hope some of that helps,
| [reply] |
|
|
Yes that appears to be what I am looking for. Here is some code I will be applying this to:
sub open {
my $browse = $t->getOpenFile(-title => "Browse For A File!");
if ($browse ne "") {
$t->delete("1.0", "end");
if (!open(TARGET, "$browse")) {
$info = "Error!";
$t->insert("end", "ERROR: Could not open $browse\n");
return;
}
#$line_count = 1;
$filename = $browse;
$info = "Loading file '$filename'...";
while (<TARGET>) {
#$t->insert("end", "$line_count ");
$t->insert("end", $_);
$line_count++;
}
close(TARGET);
$info = "File '$filename' loaded";
}else{
if (!$filename) {
$info = "Error!";
$t->delete("1.0", "end");
$t->insert("end", "ERROR: Could not open $browse\n");
return;
}else{
return;
}
}
}
I want to incorporate the coloring of the text while the file is being parsed during opening. As of now I am using $_ to insert each line while traversing the while loop. If I wanted to color specific words ine ach line would I have to parse each line further into each word?
From what I read on your link from CPAN was that you have to designate certain words to certain colors.
use Term::ANSIColor;
$red = color('red'); # Retrieve color codes
$bold = color('bold');
$wid->insert('end', "$red red text $bold with bold\n");
Here I would designate "red text" as being red by assigning $red to it in front of the target text. In my implementation now I would have to use something like:
$wid->insert("end", $red $_);
which would color the entire line. ANy suggestion on how to parse the file down to word level and still insert correctly? | [reply] [d/l] [select] |
|
|
Not sure what your $wid exactly is, but this might help you (works for me with Tk::ROText as $wid):
$wid->tag('bind', "black");
$wid->tag('bind', "red");
$wid->tag('bind', "green");
$wid->tag('configure', "black", -foreground=>"black");
$wid->tag('configure', "red", -foreground=>"red");
$wid->tag('configure', "green", -foreground=>"darkgreen");
$wid->insert("end",$_,$color); # where $color can be "black", "red", o
+r "green"
HTH
| [reply] [d/l] |
Re: Coloring text using TK?
by thospel (Hermit) on Dec 01, 2003 at 18:38 UTC
|
In a Text widget, you can use tags to give properties to ranges of characters. You can either set them during insert, or attach them later. The code demonstrates both:
#!/usr/bin/perl -w
use Tk;
my $top = MainWindow->new;
my $t = $top->Text();
$t->tagConfigure("boring", -foreground => "blue");
$t->tagConfigure("special", -foreground => "red");
$t->insert("end", "If you want to be immune from silly letters, don't
+carry your momomark in your hat.\n");
$t->insert("end", "bij bij\n", "special");
$t->insert("end", "Zoem your Zoem\n", "boring");
my $word = "your";
my $word_len = length $word;
my $next = "1.0";
while (my $from = $t->search(-regexp, "\\b$word\\b", $next, "end")) {
$next = "$from + $word_len chars";
$t->tagAdd("special", $from, $next);
}
$t->pack;
MainLoop;
| [reply] [d/l] |
|
|
That works great but what if I want to check for more than just one word? Is there a way I can maybe impplement an array or some sort of list and step through the list and color accordingly? It seems the more words I want to color the slower it will goes exponentially which is why I need to find the fastest way possible to color multiple words instead of just one.
| [reply] |
|
|
#!/usr/bin/perl -w
use strict;
use Tk;
my $top = MainWindow->new;
my $t = $top->Text();
$t->tagConfigure("boring", -foreground => "blue");
$t->tagConfigure("special", -foreground => "red");
# Change the match rule according to taste
my @words = qw(your be is silly);
my $regex = join("|", @words);
$regex = qr/(.*?)\b($regex)\b/s;
my $text = "If you want to be immune from silly letters, don't carry y
+our monomark in your hat.\n";
while ($text =~ /$regex/gc) {
$t->insert("end", $1, "boring");
$t->insert("end", $2, "special");
}
$t->insert("end", $1, "boring") if $text =~ /(.+)/gs;
$t->pack;
MainLoop;
This should avoid too many regex recompiles, and give perl a
chance to build a good matcher. Maybe it's good enough for what you want ?
If that's not good enough, one way might be to build a statemachine for the match by hand and operate on the characters as they come in at the same time as you are putting them in the widget. Though that's guaranteed linear time, it's quite a bit of work per character, so I don't expect it to win until you have a lot of words. It might be useable to handle userinput while he's typing though. Surprisingly I don't directly see a perl module on CPAN (using a search on "DFA") to generate such a statemachine from a set of words.
| [reply] [d/l] [select] |
|
|
|
|
Ok I actually found a way to shorten the code and combine multiple words into an array. It does not speed up the text coloring or parsing but it does not slow it down either. It is the exact same speed just less code then writing out every single word and corresponding parsing code.
Here it is:
my @words = ("sub", "if", "elsif", "else", "my", "use");
foreach my $word (@words) {
my $word_len = length $word;
my $next = "1.0";
while (my $from = $t->search(-regexp, "\\b$word\\b", $n
+ext, "end")) {
$next = "$from + $word_len chars";
$t->tagAdd("blue", $from, $next);
$t->tagAdd("bold", $from, $next);
}
}
| [reply] [d/l] |