/Why Tea
Looking at this, I'm a bit surprised by the [lsearch]:
proc + {args} {
variable map
set t 0
foreach i $args {
set ix [lsearch -exact $map $i]
if {$ix>-1} {lappend t [lindex $map [incr ix]]}
}
return "\033\[[join $t {;}]m"
}
Why not use an array there ?
array set map {bold 1 ...}
proc + {args} {
variable map
set t 0
foreach i $args {
if {[info exists map($i)]} {lappend t $map($i)}
}
return "\033\[[join $t {;}]m"
}
-Alex
Sure. That code is quite old, I may have been under the impression of
the fact that arrays take 42 bytes overhead per elements, while lists
need only 12. But as the map isn't extremely big, an array is ok here
- and even a local one, in order to avoid one global variable :)
Yeah, the code is from 2001. Back then even I haven't even heard of
arrays. I've only started using arrays in 2004 ;-) I'm not sure when
arrays were introduced but back then not many people were using it yet.
Heh, I just realised. Doubtless that sometime in 2010 someone will ask
the same question about why one would write:
foreach {x y} $xy break
instead of
lassign $xy x y
(and people will inevitably still complain that Tcl doesn't evolve)
Not a text widget, but I wrote an ANSI decoder for window's consoles a ways back:
http://www.pobox.com/~davygrvy/tclstuff/winAnsiCon12_pack.zip
I had the intent to put it in the windows console channel driver, but opted not to
as the interface wouldn't have been "comfortable" for programmers.
--
If you want to stay dad you've got to polish your image. I think the image
we need to create for you is "repentant but learning". -- Calvin
Are you sure about the "not many people using it in 2001" ?? Don't know
either when array was introduced exactly, but I know for sure it was
there in 7.0 (released in 1995), and nearly all current sub-commands of
array were there in 7.4 (june 1995). Hope new features like "lassign",
"dict", etc... won't need 6 or 12 more years before people hear about
them ;-)
I'm pretty sure Richard was mastering arrays much before 2001 (and even
knew the 42 bytes overhead per entry in hash vs 12 for list!)
Eric
Well, a memory footprint per element means nothing since there are
just a constant number of them, while the lookup will occur for each
and every color command, hence the [lsearch] CPU overhead will hit
every line of (colorized) output.
> - and even a local one, in order to avoid one global variable :)
You still need a (namespace) global to avoid deserializing the array
at each call with [array set].
-Alex
Thanks to everyone who replied. The solution provided in
http://wiki.tcl.tk/1143 looks like something I need. Will
explore more on that.
/Why Tea