John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

A short time ago I lamented that the built-in file handle tie ability in a Tk Text widget was flawed, in that it didn't 'update' automatically (only when idle, so it was useless for seeing what a running task is doing). I figured not too bad, since I plan some fancier stuff for mine anyway.

Well, I added some concept code to my tie class so I can say

print "Normal, ok, now $EFFECT marked, and $NORMAL back to normal,\n" +;
I'm using a simple control char for each command. In my application, a small number of distinct colors are needed (normal, error, warning, etc.). But to make it general, how should I design it? Maybe embedded strings to indicate any named tag for the widget?

Replies are listed 'Best First'.
Re: Proof of concept: color tty window in Tk
by traveler (Parson) on Dec 12, 2002 at 23:35 UTC
    I missed the earlier lament (and I did not find it searching thru your home node, and the closest I found using Search was this), so if I'm off base, please just ignore this. Anyway I presume you do one of two things: a) mark the text to effect and then apply the effect, or b) break up the string and send it to the widget in parts. In either case, I guess your question is along the lines of "what should the user send to print?" Is that right?

    I think xml style markup might be pretty readable and not too hard to do, particularly if you use the last example below.

    • "This is <em>very</em> important"
    • "This is <font color='red'>a negative balance</font>
    and if you don't like HTML
    • "This is <warn>a warning</warn> for the user

    Of course, this means escaping < and so forth. I guess you could do something like use character codes from a famous color terminal...

    If I misinterpreted the question, just ignore this, OK?

    Just my USD0.02, --traveler

      I figured I would use non-printing control codes at least as the delimiters, so nothing needs to be escaped. Perhaps the classic ESCape.

      I like having an "end" tag instead of having to change back to the old way, because you might not know or care what it was before. But, I also want to prevent accidental runons when something doesn't match. If an intended string is not sent, and that contained a close, or if the error is in something that's rarely printed like an error message, I don't want it to bamboozle the whole thing from there on.

      So, perhaps controls can last only until the end of the line, even if you leave off an end code.

      It might be nice to have implicit ends noted when you start. In the case of "very important", have a code that means "emphasize just the next word, then stop". Maybe have common cases, plus match the specified pattern or natural delimiter that will be in the text.

      FWIW, I'm doing it by breaking up the string into runs and adding one run at a time to the widget.

      I don't particularly care to emulate a famous terminal, since I'm not porting terminal output programs to run here.

      I think the "syntax" of what gets put into the string might be completely hidden from the user, too. Maybe:

      my $warnline= $tty->newcode ( [-foreground => 'red'], # parameters for Tk's configure 'line' #scope ); # later... print "${warnline}Houston, we have a problem.\n";
      —John
        Sounds good. Mostly what I was suggesting was to try to avoid ${warnline} in the string. If your code could parse/translate the string sent to print, it seems to me it would be easier to use/read.

        Just my $0.02, --traveler