File: macros.tcl

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (93 lines) | stat: -rw-r--r-- 2,526 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- tcl -*-
# ### ### ### ######### ######### #########
## Terminal packages - ANSI - Higher level macros

# ### ### ### ######### ######### #########
## Requirements

package require textutil::repeat
package require textutil::tabify
package require term::ansi::code::ctrl

namespace eval ::term::ansi::code::macros {}

# ### ### ### ######### ######### #########
## API. Symbolic names.

proc ::term::ansi::code::macros::import {{ns macros} args} {
    if {![llength $args]} {set args *}
    set args ::term::ansi::code::macros::[join $args " ::term::ansi::code::macros::"]
    uplevel 1 [list namespace eval ${ns} [linsert $args 0 namespace import]]
    return
}

# ### ### ### ######### ######### #########
## Higher level operations

# Format a menu / framed block of text

proc ::term::ansi::code::macros::menu {menu} {
    # Menu = dict (label => char)
    array set _ {}
    set shift 0
    foreach {label c} $menu {
	if {[string first $c $label] < 0} {
	    set shift 1
	    break
	}
    }
    set max 0
    foreach {label c} $menu {
	set pos [string first $c $label]
	if {$shift || ($pos < 0)} {
	    set xlabel "$c $label"
	    set pos 0
	} else {
	    set xlabel $label
	}
	set len [string length $xlabel]
	if {$len > $max} {set max $len}
	set _($label) " [string replace $xlabel $pos $pos \
		[cd::sda_fgred][cd::sda_bold][string index $xlabel $pos][cd::sda_reset]]"
    }

    append ms [cd::tlc][textutil::repeat::strRepeat [cd::hl] $max][cd::trc]\n
    foreach {l c} $menu {append ms $_($l)\n}
    append ms [cd::blc][textutil::repeat::strRepeat [cd::hl] $max][cd::brc]

    return [cd::groptim $ms]
}

proc ::term::ansi::code::macros::frame {string} {
    set lines [split [textutil::tabify::untabify2 $string] \n]
    set max 0
    foreach l $lines {
	if {[set len [string length $l]] > $max} {set max $len}
    }
    append fs [cd::tlc][textutil::repeat::strRepeat [cd::hl] $max][cd::trc]\n
    foreach l $lines {
	append fs [cd::vl]${l}[textutil::repeat::strRepeat " " [expr {$max-[string length $l]}]][cd::vl]\n
    }
    append fs [cd::blc][textutil::repeat::strRepeat [cd::hl] $max][cd::brc]
    return [cd::groptim $fs]
}

##
# ### ### ### ######### ######### #########

# ### ### ### ######### ######### #########
## Data structures.

namespace eval ::term::ansi::code::macros {
    term::ansi::code::ctrl::import cd

    namespace export menu frame
}

# ### ### ### ######### ######### #########
## Ready

package provide term::ansi::code::macros 0.1

##
# ### ### ### ######### ######### #########