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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
|
# report.tcl --
#
# Implementation of report objects for Tcl.
#
# Copyright (c) 2001-2014 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: report.tcl,v 1.8 2004/01/15 06:36:13 andreas_kupries Exp $
package require Tcl 8.2
package provide report 0.3.2
namespace eval ::report {
# Data storage in the report module
# -------------------------------
#
# One namespace per object, containing
# 1) An array mapping from template codes to templates
# 2) An array mapping from template codes and columns to horizontal template items
# 3) An array mapping from template codes and columns to vertical template items
# 4) ... deleted, local to formatting
# 5) An array mapping from columns to left padding
# 6) An array mapping from columns to right padding
# 7) An array mapping from columns to column size
# 8) An array mapping from columns to justification
# 9) A scalar containing the number of columns in the report.
# 10) An array mapping from template codes to enabledness
# 11) A scalar containing the size of the top caption
# 12) A scalar containing the size of the bottom caption
#
# 1 - template 5 - lpad 9 - columns
# 2 - hTemplate 6 - rpad 10 - enabled
# 3 - vTemplate 7 - csize 11 - tcaption
# 4 - fullHTemplate 8 - cjust 12 - bcaption
# commands is the list of subcommands recognized by the report
variable commands [list \
"bcaption" \
"botcapsep" \
"botdata" \
"botdatasep" \
"bottom" \
"columns" \
"data" \
"datasep" \
"justify" \
"pad" \
"printmatrix" \
"printmatrix2channel" \
"size" \
"sizes" \
"tcaption" \
"top" \
"topcapsep" \
"topdata" \
"topdatasep"
]
# Only export the toplevel commands
namespace export report defstyle rmstyle stylearguments stylebody
# Global data, style definitions
variable styles [list plain]
variable styleargs
variable stylebody
array set styleargs {plain {}}
array set stylebody {plain {}}
# Global data, template codes, for easy checking
variable tcode
array set tcode {
topdata 0 data 0
botdata 0 top 1
topdatasep 1 topcapsep 1
datasep 1 botcapsep 1
botdatasep 1 bottom 1
}
}
# ::report::report --
#
# Create a new report with a given name
#
# Arguments:
# name Optional name of the report; if null or not given, generate one.
#
# Results:
# name Name of the report created
proc ::report::report {name columns args} {
variable styleargs
if { [llength [info commands ::$name]] } {
error "command \"$name\" already exists, unable to create report"
}
if {![string is integer $columns]} {
return -code error "columns: expected integer greater than zero, got \"$columns\""
} elseif {$columns <= 0} {
return -code error "columns: expected integer greater than zero, got \"$columns\""
}
set styleName ""
switch -exact -- [llength $args] {
0 {# No style was specied. This is OK}
1 {
# We possibly got the "style" keyword, but everything behind is missing
return -code error "wrong # args: report name columns ?\"style\" styleName ?arg...??"
}
default {
# Break tail apart, check for correct keyword, ensure that style is known too.
# Don't forget to check the actual against the formal arguments.
foreach {dummy styleName} $args break
set args [lrange $args 2 end]
if {![string equal $dummy style]} {
return -code error "wrong # args: report name columns ?\"style\" styleName ?arg...??"
}
if {![info exists styleargs($styleName)]} {
return -code error "style \"$styleName\" is not known"
}
CheckStyleArguments $styleName $args
}
}
# The arguments seem to be ok, setup the namespace for the object
# and configure it to style "plain".
namespace eval ::report::report$name "variable columns $columns"
namespace eval ::report::report$name {
variable tcaption 0
variable bcaption 0
variable template
variable enabled
variable hTemplate
variable vTemplate
variable lpad
variable rpad
variable csize
variable cjust
variable t
variable i
variable dt [list]
variable st [list]
for {set i 0} {$i < $columns} {incr i} {
set lpad($i) ""
set rpad($i) ""
set csize($i) dyn
set cjust($i) left
lappend dt {}
lappend st {} {}
}
lappend dt {}
lappend st {}
foreach t {
topdata data botdata
} {
set enabled($t) 1
set template($t) $dt
for {set i 0} {$i <= $columns} {incr i} {
set vTemplate($t,$i) {}
}
}
foreach t {
top topdatasep topcapsep
datasep
botcapsep botdatasep bottom
} {
set enabled($t) 0
set template($t) $st
for {set i 0} {$i < $columns} {incr i} {
set hTemplate($t,$i) {}
}
for {set i 0} {$i <= $columns} {incr i} {
set vTemplate($t,$i) {}
}
}
unset t i dt st
}
# Create the command to manipulate the report
# $name -> ::report::ReportProc $name
interp alias {} ::$name {} ::report::ReportProc $name
# If a style was specified execute it now, before the oobject is
# handed back to the user.
if {$styleName != {}} {
ExecuteStyle $name $styleName $args
}
return $name
}
# ::report::defstyle --
#
# Defines a new named style, with arguments and defining script.
#
# Arguments:
# styleName Name of the new style.
# arguments Formal arguments of the style, some format as for proc.
# body The script actually defining the style.
#
# Results:
# None.
proc ::report::defstyle {styleName arguments body} {
variable styleargs
variable stylebody
variable styles
if {[info exists styleargs($styleName)]} {
return -code error "Cannot create style \"$styleName\", already exists"
}
# Check the formal arguments
# 1. Arguments without default may not follow an argument with a
# default. The special "args" is no exception!
# 2. Compute the minimal number of arguments required by the proc.
set min 0
set def 0
set ca 0
foreach v $arguments {
switch -- [llength $v] {
1 {
if {$def} {
return -code error \
"Found argument without default after arguments having defaults"
}
incr min
}
2 {
set def 1
}
default {
error "Illegal length of value \"$v\""
}
}
}
if {[string equal args [lindex $arguments end]]} {
# Correct requirements if we have a catch-all at the end.
incr min -1
set ca 1
}
# Now we are allowed to extend the internal database
set styleargs($styleName) [list $min $ca $arguments]
set stylebody($styleName) $body
lappend styles $styleName
return
}
# ::report::rmstyle --
#
# Deletes the specified style.
#
# Arguments:
# styleName Name of the style to destroy.
#
# Results:
# None.
proc ::report::rmstyle {styleName} {
variable styleargs
variable stylebody
variable styles
if {![info exists styleargs($styleName)]} {
return -code error "cannot delete unknown style \"$styleName\""
}
if {[string equal $styleName plain]} {
return -code error {cannot delete builtin style "plain"}
}
unset styleargs($styleName)
unset stylebody($styleName)
set pos [lsearch -exact $styles $styleName]
set styles [lreplace $styles $pos $pos]
return
}
# ::report::_stylearguments --
#
# Introspection, returns the list of formal arguments of the
# specified style.
#
# Arguments:
# styleName Name of the style to query.
#
# Results:
# A list containing the formal argument of the style
proc ::report::stylearguments {styleName} {
variable styleargs
if {![info exists styleargs($styleName)]} {
return -code error "style \"$styleName\" is not known"
}
return [lindex $styleargs($styleName) 2]
}
# ::report::_stylebody --
#
# Introspection, returns the body/script of the
# specified style.
#
# Arguments:
# styleName Name of the style to query.
#
# Results:
# A script, the body of the style.
proc ::report::stylebody {styleName} {
variable stylebody
if {![info exists stylebody($styleName)]} {
return -code error "style \"$styleName\" is not known"
}
return $stylebody($styleName)
}
# ::report::_styles --
#
# Returns alist containing the names of all known styles.
#
# Arguments:
# None.
#
# Results:
# A list containing the names of all known styles
proc ::report::styles {} {
variable styles
return $styles
}
##########################
# Private functions follow
# ::report::CheckStyleArguments --
#
# Internal helper. Used to check actual arguments of a style against the formal ones.
#
# Arguments:
# styleName Name of the style in question
# arguments Actual arguments for the style.
#
# Results:
# None, or an error in case of problems.
proc ::report::CheckStyleArguments {styleName arguments} {
variable styleargs
# Match formal and actual arguments, error out in case of problems.
foreach {min catchall formal} $styleargs($styleName) break
if {[llength $arguments] < $min} {
# Determine the name of the first formal parameter which did not get a value.
set firstmissing [lindex $formal [llength $arguments]]
return -code error "no value given for parameter \"$firstmissing\" to style \"$styleName\""
} elseif {[llength $arguments] > $min} {
if {!$catchall && ([llength $arguments] > [llength $formal])} {
# More actual arguments than formals, without catch-all argument, error
return -code error "called style \"$styleName\" with too many arguments"
}
}
}
# ::report::ExecuteStyle --
#
# Internal helper. Applies a named style to the specified report object.
#
# Arguments:
# name Name of the report the style is applied to.
# styleName Name of the style to apply
# arguments Actual arguments for the style.
#
# Results:
# None.
proc ::report::ExecuteStyle {name styleName arguments} {
variable styleargs
variable stylebody
variable styles
variable commands
CheckStyleArguments $styleName $arguments
foreach {min catchall formal} $styleargs($styleName) break
array set a {}
if {([llength $arguments] > $min) && $catchall} {
# #min = number of formal arguments - 1
set a(args) [lrange $arguments $min end]
set formal [lrange $formal 0 end-1]
incr min -1
set arguments [lrange $arguments 0 $min]
# arguments and formal are now of equal length and we also
# know that there are no arguments having a default value.
foreach v $formal aval $arguments {
set a($v) $aval
}
}
# More arguments than minimally required, but no more than formal
# arguments! Proceed to standard matching: Go through the actual
# values and associate them with a formal argument. Then fill the
# remaining formal arguments with their default values.
foreach aval $arguments {
set v [lindex $formal 0]
set formal [lrange $formal 1 end]
if {[llength $v] > 1} {set v [lindex $v 0]}
set a($v) $aval
}
foreach vd $formal {
foreach {var default} $vd {
set a($var) $default
}
}
# Create and initialize a safe interpreter, execute the style and
# then break everything down again.
set ip [interp create -safe]
# -- Report methods --
foreach m $commands {
# safe-ip method --> here report method
interp alias $ip $m {} $name $m
}
# -- Styles defined before this one --
foreach s $styles {
if {[string equal $s $styleName]} {break}
interp alias $ip $s {} ::report::LinkExec $name $s
}
# -- Arguments as variables --
foreach {var val} [array get a] {
$ip eval [list set $var $val]
}
# Finally execute / apply the style.
$ip eval $stylebody($styleName)
interp delete $ip
return
}
# ::report::_LinkExec --
#
# Internal helper. Used for application of styles from within
# another style script. Collects the formal arguments into the
# one list which is expected by "ExecuteStyle".
#
# Arguments:
# name Name of the report the style is applied to.
# styleName Name of the style to apply
# args Actual arguments for the style.
#
# Results:
# None.
proc ::report::LinkExec {name styleName args} {
ExecuteStyle $name $styleName $args
}
# ::report::ReportProc --
#
# Command that processes all report object commands.
#
# Arguments:
# name Name of the report object to manipulate.
# cmd Subcommand to invoke.
# args Arguments for subcommand.
#
# Results:
# Varies based on command to perform
proc ::report::ReportProc {name {cmd ""} args} {
variable tcode
# Do minimal args checks here
if { [llength [info level 0]] == 2 } {
error "wrong # args: should be \"$name option ?arg arg ...?\""
}
# Split the args into command and args components
if {[info exists tcode($cmd)]} {
# Template codes are a bit special
eval [list ::report::_tAction $name $cmd] $args
} else {
if { [llength [info commands ::report::_$cmd]] == 0 } {
variable commands
set optlist [join $commands ", "]
set optlist [linsert $optlist "end-1" "or"]
error "bad option \"$cmd\": must be $optlist"
}
eval [list ::report::_$cmd $name] $args
}
}
# ::report::CheckColumn --
#
# Helper to check and transform column indices. Returns the
# absolute index number belonging to the specified
# index. Rejects indices out of the valid range of columns.
#
# Arguments:
# columns Number of columns
# column The incoming index to check and transform
#
# Results:
# The absolute index to the column
proc ::report::CheckColumn {columns column} {
switch -regex -- $column {
{end-[0-9]+} {
regsub -- {end-} $column {} column
set cc [expr {$columns - 1 - $column}]
if {($cc < 0) || ($cc >= $columns)} {
return -code error "column: index \"end-$column\" out of range"
}
return $cc
}
end {
if {$columns <= 0} {
return -code error "column: index \"$column\" out of range"
}
return [expr {$columns - 1}]
}
{[0-9]+} {
if {($column < 0) || ($column >= $columns)} {
return -code error "column: index \"$column\" out of range"
}
return $column
}
default {
return -code error "column: syntax error in index \"$column\""
}
}
}
# ::report::CheckVerticals --
#
# Internal helper. Used to check the consistency of all active
# templates with respect to the generated vertical separators
# (Same length).
#
# Arguments:
# name Name of the report object to check.
#
# Results:
# None.
proc ::report::CheckVerticals {name} {
upvar ::report::report${name}::vTemplate vTemplate
upvar ::report::report${name}::enabled enabled
upvar ::report::report${name}::columns columns
upvar ::report::report${name}::tcaption tcaption
upvar ::report::report${name}::bcaption bcaption
for {set c 0} {$c <= $columns} {incr c} {
# Collect all lengths for a column in a list, sort that and
# compare first against last element. If they are not equal we
# have found an inconsistent definition.
set res [list]
lappend res [string length $vTemplate(data,$c)]
if {$tcaption > 0} {
lappend res [string length $vTemplate(topdata,$c)]
if {($tcaption > 1) && $enabled(topdatasep)} {
lappend res [string length $vTemplate(topdatasep,$c)]
}
if {$enabled(topcapsep)} {
lappend res [string length $vTemplate(topcapsep,$c)]
}
}
if {$bcaption > 0} {
lappend res [string length $vTemplate(botdata,$c)]
if {($bcaption > 1) && $enabled(botdatasep)} {
lappend res [string length $vTemplate(botdatasep,$c)]
}
if {$enabled(botcapsep)} {
lappend res [string length $vTemplate(botcapsep,$c)]
}
}
foreach t {top datasep bottom} {
if {$enabled($t)} {
lappend res [string length $vTemplate($t,$c)]
}
}
set res [lsort $res]
if {[lindex $res 0] != [lindex $res end]} {
return -code error "inconsistent verticals in report"
}
}
}
# ::report::_tAction --
#
# Implements the actions on templates (set, get, enable, disable, enabled)
#
# Arguments:
# name Name of the report object.
# template Name of the template to query or manipulate.
# cmd The action applied to the template
# args Additional arguments per action, see documentation.
#
# Results:
# None.
proc ::report::_tAction {name template cmd args} {
# When coming in here we know that $template contains a legal
# template code. No need to check again. We need 'tcode'
# nevertheless to distinguish between separator (1) and data
# templates (0).
variable tcode
switch -exact -- $cmd {
set {
if {[llength $args] != 1} {
return -code error "Wrong # args: $name $template $cmd template"
}
set templval [lindex $args 0]
upvar ::report::report${name}::columns columns
upvar ::report::report${name}::template tpl
upvar ::report::report${name}::hTemplate hTemplate
upvar ::report::report${name}::vTemplate vTemplate
upvar ::report::report${name}::enabled enabled
if {$tcode($template)} {
# Separator template, expected size = 2*colums+1
if {[llength $templval] > (2*$columns+1)} {
return -code error {template to long for number of columns in report}
} elseif {[llength $templval] < (2*$columns+1)} {
return -code error {template to short for number of columns in report}
}
set tpl($template) $templval
set even 1
set c1 0
set c2 0
foreach item $templval {
if {$even} {
set vTemplate($template,$c1) $item
incr c1
set even 0
} else {
set hTemplate($template,$c2) $item
incr c2
set even 1
}
}
} else {
# Data template, expected size = columns+1
if {[llength $templval] > ($columns+1)} {
return -code error {template to long for number of columns in report}
} elseif {[llength $templval] < ($columns+1)} {
return -code error {template to short for number of columns in report}
}
set tpl($template) $templval
set c 0
foreach item $templval {
set vTemplate($template,$c) $item
incr c
}
}
if {$enabled($template)} {
# Perform checks for active separator templates and
# all data templates.
CheckVerticals $name
}
}
get -
enable -
disable -
enabled {
if {[llength $args] > 0} {
return -code error "Wrong # args: $name $template $cmd"
}
switch -exact -- $cmd {
get {
upvar ::report::report${name}::template tpl
return $tpl($template)
}
enable {
if {!$tcode($template)} {
# Data template, can't be enabled.
return -code error "Cannot enable data template \"$template\""
}
upvar ::report::report${name}::enabled enabled
if {!$enabled($template)} {
set enabled($template) 1
CheckVerticals $name
}
}
disable {
if {!$tcode($template)} {
# Data template, can't be disabled.
return -code error "Cannot disable data template \"$template\""
}
upvar ::report::report${name}::enabled enabled
if {$enabled($template)} {
set enabled($template) 0
}
}
enabled {
if {!$tcode($template)} {
# Data template, can't be disabled.
return -code error "Cannot query state of data template \"$template\""
}
upvar ::report::report${name}::enabled enabled
return $enabled($template)
}
default {error "Can't happen, panic, run, shout"}
}
}
default {
return -code error "Unknown template command \"$cmd\""
}
}
return ""
}
# ::report::_tcaption --
#
# Sets or queries the size of the top caption region of the report.
#
# Arguments:
# name Name of the report object.
# size The new size, if not empty. Emptiness indicates that a
# query was requested
#
# Results:
# None, or the current size of the top caption region
proc ::report::_tcaption {name {size {}}} {
upvar ::report::report${name}::tcaption tcaption
if {$size == {}} {
return $tcaption
}
if {![string is integer $size]} {
return -code error "size: expected integer greater than or equal to zero, got \"$size\""
}
if {$size < 0} {
return -code error "size: expected integer greater than or equal to zero, got \"$size\""
}
if {$size == $tcaption} {
# No change, nothing to do
return ""
}
if {($size > 0) && ($tcaption == 0)} {
# Perform a consistency check after the assignment, the
# template might have been changed.
set tcaption $size
CheckVerticals $name
} else {
set tcaption $size
}
return ""
}
# ::report::_bcaption --
#
# Sets or queries the size of the bottom caption region of the report.
#
# Arguments:
# name Name of the report object.
# size The new size, if not empty. Emptiness indicates that a
# query was requested
#
# Results:
# None, or the current size of the bottom caption region
proc ::report::_bcaption {name {size {}}} {
upvar ::report::report${name}::bcaption bcaption
if {$size == {}} {
return $bcaption
}
if {![string is integer $size]} {
return -code error "size: expected integer greater than or equal to zero, got \"$size\""
}
if {$size < 0} {
return -code error "size: expected integer greater than or equal to zero, got \"$size\""
}
if {$size == $bcaption} {
# No change, nothing to do
return ""
}
if {($size > 0) && ($bcaption == 0)} {
# Perform a consistency check after the assignment, the
# template might have been changed.
set bcaption $size
CheckVerticals $name
} else {
set bcaption $size
}
return ""
}
# ::report::_size --
#
# Sets or queries the size of the specified column.
#
# Arguments:
# name Name of the report object.
# column Index of the column to manipulate or query
# size The new size, if not empty. Emptiness indicates that a
# query was requested
#
# Results:
# None, or the current size of the column
proc ::report::_size {name column {size {}}} {
upvar ::report::report${name}::columns columns
upvar ::report::report${name}::csize csize
set column [CheckColumn $columns $column]
if {$size == {}} {
return $csize($column)
}
if {[string equal $size dyn]} {
set csize($column) $size
return ""
}
if {![string is integer $size]} {
return -code error "expected integer greater than zero, got \"$size\""
}
if {$size <= 0} {
return -code error "expected integer greater than zero, got \"$size\""
}
set csize($column) $size
return ""
}
# ::report::_sizes --
#
# Sets or queries the sizes of all columns.
#
# Arguments:
# name Name of the report object.
# sizes The new sizes, if not empty. Emptiness indicates that a
# query was requested
#
# Results:
# None, or a list containing the sizes of all columns.
proc ::report::_sizes {name {sizes {}}} {
upvar ::report::report${name}::columns columns
upvar ::report::report${name}::csize csize
if {$sizes == {}} {
set res [list]
foreach k [lsort -integer [array names csize]] {
lappend res $csize($k)
}
return $res
}
if {[llength $sizes] != $columns} {
return -code error "Wrong # number of column sizes"
}
foreach size $sizes {
if {[string equal $size dyn]} {
continue
}
if {![string is integer $size]} {
return -code error "expected integer greater than zero, got \"$size\""
}
if {$size <= 0} {
return -code error "expected integer greater than zero, got \"$size\""
}
}
set i 0
foreach s $sizes {
set csize($i) $s
incr i
}
return ""
}
# ::report::_pad --
#
# Sets or queries the padding for the specified column.
#
# Arguments:
# name Name of the report object.
# column Index of the column to manipulate or query
# where Where to place the padding. Emptiness indicates
# that a query was requested.
#
# Results:
# None, or the padding for the specified column.
proc ::report::_pad {name column {where {}} {string { }}} {
upvar ::report::report${name}::columns columns
upvar ::report::report${name}::lpad lpad
upvar ::report::report${name}::rpad rpad
set column [CheckColumn $columns $column]
if {$where == {}} {
return [list $lpad($column) $rpad($column)]
}
switch -exact -- $where {
left {
set lpad($column) $string
}
right {
set rpad($column) $string
}
both {
set lpad($column) $string
set rpad($column) $string
}
default {
return -code error "where: expected left, right, or both, got \"$where\""
}
}
return ""
}
# ::report::_justify --
#
# Sets or queries the justification for the specified column.
#
# Arguments:
# name Name of the report object.
# column Index of the column to manipulate or query
# jvalue Justification to set. Emptiness indicates
# that a query was requested
#
# Results:
# None, or the current justication for the specified column
proc ::report::_justify {name column {jvalue {}}} {
upvar ::report::report${name}::columns columns
upvar ::report::report${name}::cjust cjust
set column [CheckColumn $columns $column]
if {$jvalue == {}} {
return $cjust($column)
}
switch -exact -- $jvalue {
left - right - center {
set cjust($column) $jvalue
return ""
}
default {
return -code error "justification: expected, left, right, or center, got \"$jvalue\""
}
}
}
# ::report::_printmatrix --
#
# Format the specified matrix according to the configuration of
# the report.
#
# Arguments:
# name Name of the report object.
# matrix Name of the matrix object to format.
#
# Results:
# A string containing the formatted matrix.
proc ::report::_printmatrix {name matrix} {
CheckMatrix $name $matrix
ColumnSizes $name $matrix state
upvar ::report::report${name}::tcaption tcaption
upvar ::report::report${name}::bcaption bcaption
set row 0
set out ""
append out [Separator top $name $matrix state]
if {$tcaption > 0} {
set n $tcaption
while {$n > 0} {
append out [FormatData topdata $name state [$matrix get row $row] [$matrix rowheight $row]]
if {$n > 1} {
append out [Separator topdatasep $name $matrix state]
}
incr n -1
incr row
}
append out [Separator topcapsep $name $matrix state]
}
set n [expr {[$matrix rows] - $bcaption}]
while {$row < $n} {
append out [FormatData data $name state [$matrix get row $row] [$matrix rowheight $row]]
incr row
if {$row < $n} {
append out [Separator datasep $name $matrix state]
}
}
if {$bcaption > 0} {
append out [Separator botcapsep $name $matrix state]
set n $bcaption
while {$n > 0} {
append out [FormatData botdata $name state [$matrix get row $row] [$matrix rowheight $row]]
if {$n > 1} {
append out [Separator botdatasep $name $matrix state]
}
incr n -1
incr row
}
}
append out [Separator bottom $name $matrix state]
#parray state
return $out
}
# ::report::_printmatrix2channel --
#
# Format the specified matrix according to the configuration of
# the report.
#
# Arguments:
# name Name of the report.
# matrix Name of the matrix object to format.
# chan Handle of the channel to write the formatting result into.
#
# Results:
# None.
proc ::report::_printmatrix2channel {name matrix chan} {
CheckMatrix $name $matrix
ColumnSizes $name $matrix state
upvar ::report::report${name}::tcaption tcaption
upvar ::report::report${name}::bcaption bcaption
set row 0
puts -nonewline $chan [Separator top $name $matrix state]
if {$tcaption > 0} {
set n $tcaption
while {$n > 0} {
puts -nonewline $chan \
[FormatData topdata $name state [$matrix get row $row] [$matrix rowheight $row]]
if {$n > 1} {
puts -nonewline $chan [Separator topdatasep $name $matrix state]
}
incr n -1
incr row
}
puts -nonewline $chan [Separator topcapsep $name $matrix state]
}
set n [expr {[$matrix rows] - $bcaption}]
while {$row < $n} {
puts -nonewline $chan \
[FormatData data $name state [$matrix get row $row] [$matrix rowheight $row]]
incr row
if {$row < $n} {
puts -nonewline $chan [Separator datasep $name $matrix state]
}
}
if {$bcaption > 0} {
puts -nonewline $chan [Separator botcapsep $name $matrix state]
set n $bcaption
while {$n > 0} {
puts -nonewline $chan \
[FormatData botdata $name state [$matrix get row $row] [$matrix rowheight $row]]
if {$n > 1} {
puts -nonewline $chan [Separator botdatasep $name $matrix state]
}
incr n -1
incr row
}
}
puts -nonewline $chan [Separator bottom $name $matrix state]
return
}
# ::report::_columns --
#
# Retrieves the number of columns in the report.
#
# Arguments:
# name Name of the report queried
#
# Results:
# A number
proc ::report::_columns {name} {
upvar ::report::report${name}::columns columns
return $columns
}
# ::report::_destroy --
#
# Destroy a report, including its associated command and data storage.
#
# Arguments:
# name Name of the report to destroy.
#
# Results:
# None.
proc ::report::_destroy {name} {
namespace delete ::report::report$name
interp alias {} ::$name {}
return
}
# ::report::CheckMatrix --
#
# Internal helper for the "print" methods. Checks that the
# supplied matrix can be formatted by the specified report.
#
# Arguments:
# name Name of the report to use for the formatting
# matrix Name of the matrix to format.
#
# Results:
# None, or an error in case of problems.
proc ::report::CheckMatrix {name matrix} {
upvar ::report::report${name}::columns columns
upvar ::report::report${name}::tcaption tcaption
upvar ::report::report${name}::bcaption bcaption
if {$columns != [$matrix columns]} {
return -code error "report/matrix mismatch in number of columns"
}
if {($tcaption + $bcaption) > [$matrix rows]} {
return -code error "matrix too small, top and bottom captions overlap"
}
}
# ::report::ColumnSizes --
#
# Internal helper for the "print" methods. Computes the final
# column sizes (with and without padding) and stores them in
# the print-state
#
# Arguments:
# name Name of the report used for the formatting
# matrix Name of the matrix to format.
# statevar Name of the array variable holding the state
# of the formatter.
#
# Results:
# None.
proc ::report::ColumnSizes {name matrix statevar} {
# Calculate the final column sizes with and without padding and
# store them in the local state.
upvar $statevar state
upvar ::report::report${name}::columns columns
upvar ::report::report${name}::csize csize
upvar ::report::report${name}::lpad lpad
upvar ::report::report${name}::rpad rpad
for {set c 0} {$c < $columns} {incr c} {
if {[string equal dyn $csize($c)]} {
set size [$matrix columnwidth $c]
} else {
set size $csize($c)
}
set state(s,$c) $size
incr size [string length $lpad($c)]
incr size [string length $rpad($c)]
set state(s/pad,$c) $size
}
return
}
# ::report::Separator --
#
# Internal helper for the "print" methods. Computes the final
# shape of the various separators using the column sizes with
# padding found in the print state. Uses also the print state as
# a cache to avoid costly recomputation for the separators which
# are used multiple times.
#
# Arguments:
# tcode Code of the separator to compute / template to use
# name Name of the report used for the formatting
# matrix Name of the matrix to format.
# statevar Name of the array variable holding the state
# of the formatter.
#
# Results:
# The final separator string. Empty for disabled separators.
proc ::report::Separator {tcode name matrix statevar} {
upvar ::report::report${name}::enabled e
if {!$e($tcode)} {return ""}
upvar $statevar state
if {![info exists state($tcode)]} {
upvar ::report::report${name}::vTemplate vt
upvar ::report::report${name}::hTemplate ht
upvar ::report::report${name}::columns cs
set str ""
for {set c 0} {$c < $cs} {incr c} {
append str $vt($tcode,$c)
set fill $ht($tcode,$c)
set flen [string length $fill]
set rep [expr {($state(s/pad,$c)/$flen)+1}]
append str [string range [string repeat $fill $rep] 0 [expr {$state(s/pad,$c)-1}]]
}
append str $vt($tcode,$cs)
set state($tcode) $str
}
return $state($tcode)\n
}
# ::report::FormatData --
#
# Internal helper for the "print" methods. Computes the output
# for one row in the matrix, given its values, the rowheight,
# padding and justification.
#
# Arguments:
# tcode Code of the data template to use
# name Name of the report used for the formatting
# statevar Name of the array variable holding the state
# of the formatter.
# line List containing the values to format
# rh Height of the row (line) in lines.
#
# Results:
# The formatted string for the supplied row.
proc ::report::FormatData {tcode name statevar line rh} {
upvar $statevar state
upvar ::report::report${name}::vTemplate vt
upvar ::report::report${name}::columns cs
upvar ::report::report${name}::lpad lpad
upvar ::report::report${name}::rpad rpad
upvar ::report::report${name}::cjust cjust
if {$rh == 1} {
set str ""
set c 0
foreach cell $line {
# prefix, cell (pad-l, value, pad-r)
append str $vt($tcode,$c)$lpad($c)[FormatCell $cell $state(s,$c) $cjust($c)]$rpad($c)
incr c
}
append str $vt($tcode,$cs)\n
return $str
} else {
array set str {}
for {set l 1} {$l <= $rh} {incr l} {set str($l) ""}
# - Future - Vertical justification of cells less tall than rowheight
# - Future - Vertical cutff aftert n lines, auto-repeat of captions
# - Future - => Higher level, not here, use virtual matrices for this
# - Future - and count the generated lines
set c 0
foreach fcell $line {
set fcell [split $fcell \n]
for {set l 1; set lo 0} {$l <= $rh} {incr l; incr lo} {
append str($l) $vt($tcode,$c)$lpad($c)[FormatCell \
[lindex $fcell $lo] $state(s,$c) $cjust($c)]$rpad($c)
}
incr c
}
set strout ""
for {set l 1} {$l <= $rh} {incr l} {
append strout $str($l)$vt($tcode,$cs)\n
}
return $strout
}
}
# ::report::FormatCell --
#
# Internal helper for the "print" methods. Formats the value of
# a single cell according to column size and justification.
#
# Arguments:
# value The value to format
# size The size of the column, without padding
# just The justification for the current cell/column
#
# Results:
# The formatted string for the supplied cell.
proc ::report::FormatCell {value size just} {
set vlen [string length [StripAnsiColor $value]]
if {$vlen == $size} {
# Value fits exactly, justification is irrelevant
return $value
}
# - Future - Other fill characters ...
# - Future - Different fill characters per class of value => regex/glob pattern|functions
# - Future - Wraparound - interacts with rowheight!
switch -exact -- $just {
left {
if {$vlen < $size} {
return $value[string repeat " " [expr {$size - $vlen}]]
}
return [string range $value [expr {$vlen - $size}] end]
}
right {
if {$vlen < $size} {
return [string repeat " " [expr {$size - $vlen}]]$value
}
incr size -1
return [string range $value 0 $size]
}
center {
if {$vlen < $size} {
set fill [expr {$size - $vlen}]
set rfill [expr {$fill / 2}]
set lfill [expr {$fill - $rfill}]
return [string repeat " " $lfill]$value[string repeat " " $rfill]
}
set cut [expr {$vlen - $size}]
set lcut [expr {$cut / 2}]
set rcut [expr {$cut - $lcut}]
return [string range $value $lcut end-$rcut]
}
default {
error "Can't happen, panic, run, shout"
}
}
}
proc ::report::StripAnsiColor {string} {
# Look for ANSI color control sequences and remove them. Avoid
# counting their characters as such sequences as a whole represent
# a state change, and are logically of zero/no width.
regsub -all "\033\\\[\[0-9;\]*m" $string {} string
return $string
}
|