VT100 terminal library for TCL

This piece of code makes displaying coloured text (and much more) easy for TCL programmer. It's a package written in pure-TCL.


Full manual page is placed here: http://sqlitestudio.one.pl/libterm/libterm.html

Overview

Short description of procedures provided by the package:

  • tputs - the main procedure of this package. Works similar to TCL 'puts', but allows using of special tags for colors handling.
  • rtputs - this is restricted version of puts. Works faster, but it's less powerful.
  • mv - manages a cursor position. Moving can be relative to current position or absolute.
  • curpos - can remember cursor position and restore it later
  • erase - erases pieces of lines, whole lines, pieces of screen or whole screen
  • reset - resets all settings to defaults
  • scroll - defines terminal scroll region (full screen, or some part of screen)

The library sources are placed here: http://sqlitestudio.one.pl/libterm/libterm-0.9.tar.gz

BUGS

Version 0.9 could be lilbit unstable (sometimes 'scroll' command doesn't work as it should).

If anybody has idea how to speed-up 'tputs' command, fix 'scroll' command, or simply add some another one, their concepts are welcome :)

EXAMPLES

Well, here are few examples of how can it be used:

Hello World

#!/bin/sh
#\
exec tclsh $0 ${@}
 
package require libterm
 
namespace import ::libterm::*
 
rtputs "%BHello %Rworld%Y!" 

It will simply display "Hello world!", but the first word will be blue, the second one will be red and the interjection will be yellow.

Blinking text

#!/bin/sh
#\
exec tclsh $0 ${@}
 
package require libterm
 
namespace import ::libterm::*
 
erase screen both
 
set interval 100
set txt "Sample text"
 
foreach {col1 col2} {b B r R g G y Y m M w W} {
    for {set i 0} {$i < 5} {incr i} {
        mv 5,10
        rtputs "%${col1}$txt"
        after $interval {set sleep 1}
        vwait sleep
        mv 5,10
        rtputs "%${col2}$txt"
        after $interval {set sleep 1}
        vwait sleep
    }
}

This code will display text "Sample text" in the 10th column of the 5th raw of console screen. The text will change its colors for each 100 miliseconds, which will give 'blinking' text effect.

Progress bar

#!/bin/sh
#\
exec tclsh $0 ${@}
 
package require libterm
 
namespace import ::libterm::*
 
erase screen both
 
proc pad {str char cnt} {
    if {$cnt > [string length $str]} {
        for {set i [string length $str]} {$i < $cnt} {incr i} {
            append str $char
        }
    }
    return $str
}
 
for {set i 0} {$i <= 100} {incr i} {
    mv 3,20
    set progress [expr {$i/5}]
    set left [expr {20-$progress}]
    rtputs "%b\[%G[pad [pad {} {=} $progress]> { } 21]%b\]"
    mv 4,29
    tputs "%#r%Y<$i%%>"
    after 100 {set sleep 1}
    vwait sleep
}

This is simple 'progress bar' example made with libterm package.


VI 2004-05-29 Minimalist Curses is somewhat related