macros.tcl

Go to the documentation of this file.
00001 /*  -*- tcl -*-*/
00002 /*  ### ### ### ######### ######### #########*/
00003 /*  Terminal packages - ANSI - Higher level macros*/
00004 
00005 /*  ### ### ### ######### ######### #########*/
00006 /*  Requirements*/
00007 
00008 package require textutil::repeat
00009 package require textutil::tabify
00010 package require term::ansi::code::ctrl
00011 
00012 namespace ::term::ansi::code::macros {}
00013 
00014 /*  ### ### ### ######### ######### #########*/
00015 /*  API. Symbolic names.*/
00016 
00017 ret  ::term::ansi::code::macros::import (optional ns =macros , type args) {
00018     if {![llength $args]} {set args *}
00019     set args ::term::ansi::code::macros::[join $args " ::term::ansi::code::macros::"]
00020     uplevel 1 [list namespace eval ${ns} [linsert $args 0 namespace import]]
00021     return
00022 }
00023 
00024 /*  ### ### ### ######### ######### #########*/
00025 /*  Higher level operations*/
00026 
00027 /*  Format a menu / framed block of text*/
00028 
00029 ret  ::term::ansi::code::macros::menu (type menu) {
00030     # Menu = dict (label => char)
00031     array set _ {}
00032     set shift 0
00033     foreach {label c} $menu {
00034     if {[string first $c $label] < 0} {
00035         set shift 1
00036         break
00037     }
00038     }
00039     set max 0
00040     foreach {label c} $menu {
00041     set pos [string first $c $label]
00042     if {$shift || ($pos < 0)} {
00043         set xlabel "$c $label"
00044         set pos 0
00045     } else {
00046         set xlabel $label
00047     }
00048     set len [string length $xlabel]
00049     if {$len > $max} {set max $len}
00050     set _($label) " [string replace $xlabel $pos $pos \
00051         [cd::sda_fgred][cd::sda_bold][string index $xlabel $pos][cd::sda_reset]]"
00052     }
00053 
00054     append ms [cd::tlc][textutil::repeat::strRepeat [cd::hl] $max][cd::trc]\n
00055     foreach {l c} $menu {append ms $_($l)\n}
00056     append ms [cd::blc][textutil::repeat::strRepeat [cd::hl] $max][cd::brc]
00057 
00058     return [cd::groptim $ms]
00059 }
00060 
00061 ret  ::term::ansi::code::macros::frame (type string) {
00062     set lines [split [textutil::tabify::untabify2 $string] \n]
00063     set max 0
00064     foreach l $lines {
00065     if {[set len [string length $l]] > $max} {set max $len}
00066     }
00067     append fs [cd::tlc][textutil::repeat::strRepeat [cd::hl] $max][cd::trc]\n
00068     foreach l $lines {
00069     append fs [cd::vl]${l}[textutil::repeat::strRepeat " " [expr {$max-[string length $l]}]][cd::vl]\n
00070     }
00071     append fs [cd::blc][textutil::repeat::strRepeat [cd::hl] $max][cd::brc]
00072     return [cd::groptim $fs]
00073 }
00074 
00075 /** 
00076  * ### ### ### ######### ######### #########
00077  */
00078 
00079 /*  ### ### ### ######### ######### #########*/
00080 /*  Data structures.*/
00081 
00082 namespace ::term::ansi::code::macros {
00083     term::ansi::code::ctrl::import cd
00084 
00085     namespace export menu frame
00086 }
00087 
00088 /*  ### ### ### ######### ######### #########*/
00089 /*  Ready*/
00090 
00091 package provide term::ansi::code::macros 0.1
00092 
00093 /** 
00094  * ### ### ### ######### ######### #########
00095 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1