1# -*- tcl -*- 2# ### ### ### ######### ######### ######### 3## Terminal packages - ANSI - Higher level macros 4 5# ### ### ### ######### ######### ######### 6## Requirements 7 8package require textutil::repeat 9package require textutil::tabify 10package require term::ansi::code::ctrl 11 12namespace eval ::term::ansi::code::macros {} 13 14# ### ### ### ######### ######### ######### 15## API. Symbolic names. 16 17proc ::term::ansi::code::macros::import {{ns macros} args} { 18 if {![llength $args]} {set args *} 19 set args ::term::ansi::code::macros::[join $args " ::term::ansi::code::macros::"] 20 uplevel 1 [list namespace eval ${ns} [linsert $args 0 namespace import]] 21 return 22} 23 24# ### ### ### ######### ######### ######### 25## Higher level operations 26 27# Format a menu / framed block of text 28 29proc ::term::ansi::code::macros::menu {menu} { 30 # Menu = dict (label => char) 31 array set _ {} 32 set shift 0 33 foreach {label c} $menu { 34 if {[string first $c $label] < 0} { 35 set shift 1 36 break 37 } 38 } 39 set max 0 40 foreach {label c} $menu { 41 set pos [string first $c $label] 42 if {$shift || ($pos < 0)} { 43 set xlabel "$c $label" 44 set pos 0 45 } else { 46 set xlabel $label 47 } 48 set len [string length $xlabel] 49 if {$len > $max} {set max $len} 50 set _($label) " [string replace $xlabel $pos $pos \ 51 [cd::sda_fgred][cd::sda_bold][string index $xlabel $pos][cd::sda_reset]]" 52 } 53 54 append ms [cd::tlc][textutil::repeat::strRepeat [cd::hl] $max][cd::trc]\n 55 foreach {l c} $menu {append ms $_($l)\n} 56 append ms [cd::blc][textutil::repeat::strRepeat [cd::hl] $max][cd::brc] 57 58 return [cd::groptim $ms] 59} 60 61proc ::term::ansi::code::macros::frame {string} { 62 set lines [split [textutil::tabify::untabify2 $string] \n] 63 set max 0 64 foreach l $lines { 65 if {[set len [string length $l]] > $max} {set max $len} 66 } 67 append fs [cd::tlc][textutil::repeat::strRepeat [cd::hl] $max][cd::trc]\n 68 foreach l $lines { 69 append fs [cd::vl]${l}[textutil::repeat::strRepeat " " [expr {$max-[string length $l]}]][cd::vl]\n 70 } 71 append fs [cd::blc][textutil::repeat::strRepeat [cd::hl] $max][cd::brc] 72 return [cd::groptim $fs] 73} 74 75## 76# ### ### ### ######### ######### ######### 77 78# ### ### ### ######### ######### ######### 79## Data structures. 80 81namespace eval ::term::ansi::code::macros { 82 term::ansi::code::ctrl::import cd 83 84 namespace export menu frame 85} 86 87# ### ### ### ######### ######### ######### 88## Ready 89 90package provide term::ansi::code::macros 0.1 91 92## 93# ### ### ### ######### ######### ######### 94