ipager.tcl

Go to the documentation of this file.
00001 /*  -*- tcl -*-*/
00002 /*  ### ### ### ######### ######### #########*/
00003 /*  Terminal packages - string -> action mappings*/
00004 /*  (pager objects). For use with 'receive listen'.*/
00005 /*  In essence a DFA with tree structure.*/
00006 
00007 /*  ### ### ### ######### ######### #########*/
00008 /*  Requirements*/
00009 
00010 package require snit
00011 package require textutil::repeat
00012 package require textutil::tabify
00013 package require term::ansi::send
00014 package require term::receive::bind
00015 package require term::ansi::code::ctrl
00016 
00017 namespace ::term::receive::pager {}
00018 
00019 /*  ### ### ### ######### ######### #########*/
00020 
00021 snit::type ::term::interact::pager {
00022 
00023     option -in      -default stdin
00024     option -out     -default stdout
00025     option -column  -default 0
00026     option -line    -default 0
00027     option -height  -default 25
00028     option -actions -default {}
00029 
00030     /*  ### ### ### ######### ######### #########*/
00031     /** 
00032  */
00033 
00034     constructor {str args} {
00035     $self configurelist $args
00036     Save $str
00037 
00038     install bind using ::term::receive::bind \
00039         ${selfns}::bind $options(-actions)
00040 
00041     $bind map [cd::cu] [myret  Up]
00042     $bind map [cd::cd] [mymethod Down]
00043     $bind map \033\[5~ [mymethod PageUp]
00044     $bind map \033\[6~ [mymethod PageDown]
00045     $bind map \n       [mymethod Done]
00046     #$bind default [mymethod DEF]
00047 
00048     return
00049     }
00050 
00051     # ### ### ### ######### ######### #########
00052     ##
00053 
00054     method interact () {
00055     Show
00056     $bind listen   $options(-in)
00057     set interacting 1
00058     vwait [myvar done]
00059     set interacting 0
00060     $bind unlisten $options(-in)
00061     return
00062     }
00063 
00064     ret  done  () {set done . ; return}
00065     ret  clear () {Clear      ; return}
00066 
00067     ret  text (type str) {
00068     if {$interacting} {Clear}
00069     Save $str
00070     if {$interacting} {Show}
00071     return
00072     }
00073 
00074     /*  ### ### ### ######### ######### #########*/
00075     /** 
00076  */
00077 
00078     component bind
00079 
00080     /*  ### ### ### ######### ######### #########*/
00081     /** 
00082  */
00083 
00084     variable header
00085     variable text
00086     variable footer
00087     variable empty
00088 
00089     ret  Save (type str) {
00090     upvar 1 header header text text footer footer maxline maxline
00091     upvar 1 options(-height) height empty empty at at
00092 
00093     set lines [split [textutil::tabify::untabify2 $str] \n]
00094 
00095     set max 0
00096     foreach l $lines {
00097         if {[set len [string length $l]] > $max} {set max $len}
00098     }
00099 
00100     set header [cd::groptim [cd::tlc][textutil::repeat::strRepeat [cd::hl] $max][cd::trc]]
00101     set footer [cd::groptim [cd::blc][textutil::repeat::strRepeat [cd::hl] $max][cd::brc]]
00102 
00103     set text {}
00104     foreach l $lines {
00105         lappend text [cd::vl]${l}[textutil::repeat::strRepeat " " [expr {$max-[string length $l]}]][cd::vl]
00106     }
00107 
00108     set h $height
00109     if {$h > [llength $text]} {set h [llength $text]}
00110 
00111     set eline "  [textutil::repeat::strRepeat {  } $max]"
00112     set empty $eline
00113     for {set i 0} {$i <= $h} {incr i} {
00114         append empty \n$eline
00115     }
00116 
00117     set maxline [expr {[llength $text] - $height}]
00118     if {$maxline < 0} {set maxline 0}
00119     set at 0
00120     return
00121     }
00122 
00123     variable interacting 0
00124     variable at   0
00125     variable maxline -1
00126     variable done .
00127 
00128     ret  Show () {
00129     upvar 1 header header text text footer footer at at
00130     upvar 1 options(-in)     in  options(-column) col
00131     upvar 1 options(-out)    out options(-line)   row
00132     upvar 1 options(-height) height
00133 
00134     set to [expr {$at + $height -1}]
00135 
00136     vt::wrch $out [cd::showat $row $col \
00137                $header\n[join [lrange $text $at $to] \n]\n$footer]
00138     return
00139     }
00140 
00141     ret  Clear () {
00142     upvar 1 empty         empty options(-column) col
00143     upvar 1 options(-out) out   options(-line)   row
00144 
00145     vt::wrch $out [cd::showat $row $col $empty]
00146     return
00147     }
00148 
00149     /*  ### ### ### ######### ######### #########*/
00150     /** 
00151  */
00152 
00153     ret  Up (type str) {
00154     if {$at == 0} return
00155     incr at -1
00156     Show
00157     return
00158     }
00159 
00160     ret  Down (type str) {
00161     if {$at >= $maxline} return
00162     incr at
00163     Show
00164     return
00165     }
00166 
00167     ret  PageUp (type str) {
00168     set newat [expr {$at - $options(-height) + 1}]
00169     if {$newat < 0} {set newat 0}
00170     if {$newat == $at} return
00171     set at $newat
00172     Show
00173     return
00174     }
00175 
00176     ret  PageDown (type str) {
00177     set newat [expr {$at + $options(-height) - 1}]
00178     if {$newat >= $maxline} {set newat $maxline}
00179     if {$newat == $at} return
00180     set at $newat
00181     Show
00182     return
00183     }
00184 
00185     ret  Done (type str) {
00186     $self done
00187     return
00188     }
00189 
00190     ret  DEF (type str) {
00191     puts stderr "($str)"
00192     exit
00193     }
00194 
00195     /** 
00196      * ### ### ### ######### ######### #########
00197  */
00198 }
00199 
00200 /*  ### ### ### ######### ######### #########*/
00201 /*  Ready*/
00202 
00203 namespace ::term::interact::pager {
00204     term::ansi::code::ctrl::import cd
00205     term::ansi::send::import       vt
00206 }
00207 
00208 package provide term::interact::pager 0.1
00209 
00210 /** 
00211  * ### ### ### ######### ######### #########
00212 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1