1 # vim: foldmarker=<<<,>>>
3 class tlc
::Baseconfig {
7 variable configfile
"config"
9 if {[info exists tlc
::theme]} {
10 variable textbackground
[$tlc::theme setting textbackground
]
11 variable disabledbackground
[$tlc::theme setting disabledbackground
]
12 variable enabledbackground
[$tlc::theme setting textbackground
]
13 variable disabledforeground
[$tlc::theme setting disabledforeground
]
14 variable enabledforeground
[$tlc::theme setting enabledforeground
]
15 variable fixedfont
[$tlc::theme setting fixedfont
]
16 variable selectbackground
[$tlc::theme setting selectbackground
]
17 variable selectforeground
[$tlc::theme setting selectforeground
]
18 variable background
[$tlc::theme setting background
]
19 variable highlighttextbackground
[$tlc::theme setting background
]
20 variable hidebg
[$tlc::theme setting hidebg
]
21 variable tinyfont
[$tlc::theme setting tinyfont
]
22 variable smallfont
[$tlc::theme setting smallfont
]
23 variable normalfont
[$tlc::theme setting
font]
24 variable boldfont
[$tlc::theme setting boldfont
]
25 variable hugefont
[$tlc::theme setting hugefont
]
29 method get
{key
{default ""}}
30 method show_options
{}
31 method app_specific_opts
{}
40 variable general_opts
{
41 configfile background fixedfont tinyfont smallfont normalfont
42 boldfont hugefont disabledbackground hidebg disabledforeground
43 selectbackground enabledbackground selectforeground textbackground
44 texthighlightbackground enabledforeground
48 method format_desc
{in padding
}
53 body tlc
::Baseconfig::constructor {args
} { #<<<1
54 array set descriptions
{
55 help
"Display this help"
56 configfile
"The path of the config file\nOverride to point at a new location"
57 background
"The background colour of app"
66 # Handle "--" marking end of args <<<
78 } elseif
{[string index
$a 0] != "-"} {
88 set expecting_arg
[expr {!($expecting_arg)}]
92 # Handle "--" marking end of args >>>
94 if {[lsearch $args -h] != -1 ||
[lsearch $args --help] != -1 ||
[lsearch $args -help] != -1} {
103 if {[catch {eval configure
$args} msg
]} {
111 if {[file isfile
$configfile]} {
112 set fp
[open $configfile r
]
113 set dat
[tlc
::decomment [read $fp]]
116 foreach {option value
} $dat {
117 if {$option == ""} continue
119 configure
-$option $value
124 # allow the command line specified options to override the config file
128 body tlc
::Baseconfig::dump_config {} { #<<<1
130 foreach item
[configure
] {
131 foreach {name
default value
} $item {break}
132 lappend build
[string range
$name 1 end
] $value
139 body tlc
::Baseconfig::get {key
{default ""}} { #<<<1
140 if {[catch {set value
[cget
-$key]} msg
]} {
148 body tlc
::Baseconfig::show_options {} { #<<<1
149 set general
{--help ""}
152 set options_header
"Options"
153 set default_header
"Default Value"
154 set desc_header
"Description"
156 set max_arg_len
[string length
$options_header]
157 set max_default_len
[string length
$default_header]
158 foreach opt
[$this configure
] {
159 foreach {name
default current
} $opt break
160 if {[string length
$name] > $max_arg_len} {
161 set max_arg_len
[string length
$name]
164 if {[string length
$default] > $max_default_len} {
165 set max_default_len
[string length
$default]
168 if {[lsearch $general_opts [string trimleft
$name -]] == -1} {
169 lappend specific
$name $default
171 lappend general
$name $default
175 incr max_default_len
2
178 puts stderr
[format "%-${max_arg_len}s %-${max_default_len}s %-s" \
183 set padding
[format "%-${max_arg_len}s %-${max_default_len}s " "" ""]
185 puts stderr
"\nGeneral Options:"
186 foreach {name
default} $general {
188 if {[info exists descriptions
([string trimleft
$name -])]} {
189 set desc
$descriptions([string trimleft
$name -])
191 puts stderr
[format "%-${max_arg_len}s %-${max_default_len}s %-s" \
194 [format_desc
$desc $padding]]
197 puts stderr
"\nApplication Specific Options:"
198 foreach {name
default} $specific {
200 if {[info exists descriptions
([string trimleft
$name -])]} {
201 set desc
$descriptions([string trimleft
$name -])
203 puts stderr
[format "%-${max_arg_len}s %-${max_default_len}s %-s" \
206 [format_desc
$desc $padding]]
211 body tlc
::Baseconfig::format_desc {in padding
} { #<<<1
214 set width
[expr {80 - [string length
$padding]}]
216 foreach char
[split $in {}] {
218 lappend desc
[string trim
$part]
226 set idx
[string wordstart
$part $i]
227 lappend desc
[string trim
[string range
$part 0 [expr {$idx-1}]]]
228 set part
[string range
$part $idx end
]
229 set i
[string length
$part]
233 lappend desc
[string trim
$part]
236 return [join $desc "\n$padding"]
240 body tlc
::Baseconfig::app_specific_opts {} { #<<<1
243 foreach opt
[$this configure
] {
244 foreach {name
default current
} $opt break
246 if {[lsearch $general_opts [string trimleft
$name -]] == -1} {
247 lappend specific
$name $default
255 body tlc
::Baseconfig::rest {} { #<<<1