-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize.tcl
executable file
·93 lines (85 loc) · 3.18 KB
/
optimize.tcl
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
#!/usr/bin/tclsh
source [file join [file dirname [info script]] functions.tcl]
proc optimizeCode {line} {
##Bold sequencing: removing unneeded resets and bold codes
set codeList [regexp -inline -all -- {%\^\w+%\^[^%]*} $line]
set newLine ""
set boldFlag 0
for {set i 0} {$i<[llength $codeList]} {incr i} {
set segment [lindex $codeList $i]
set colorWord [getColorWord $segment]
set nextColorWord [getColorWord [lindex $codeList [expr {$i+1}]]]
if {$boldFlag && $colorWord == "BOLD"} {
set segment [string map {"%^BOLD%^" ""} $segment]
}
if {[boldWord $colorWord] && $nextColorWord == "RESET"} {
set segment [string map {"%^BOLD%^" ""} $segment]
}
if {[boldWord $colorWord]} {
set boldFlag 1
}
if {$colorWord == "RESET"} {
if {[boldWord $nextColorWord]} {
set segment [string map {"%^RESET%^" ""} $segment]
} else {
set boldFlag 0
}
}
set newLine [string cat $newLine $segment]
}
set line $newLine
##Color sequencing: resets after color alterations without bold alteration
set codeList [regexp -inline -all -- {%\^\w+%\^[^%]*} $line]
set newLine ""
set boldFlag 0
for {set i 0} {$i<[llength $codeList]} {incr i} {
set segment [lindex $codeList $i]
set colorWord [getColorWord $segment]
set nextColorWord [getColorWord [lindex $codeList [expr {$i+1}]]]
if {$colorWord == "BOLD" || $colorWord == "YELLOW"} {
set boldFlag 1
}
if {$colorWord == "RESET"} {
if {$boldFlag && [boldWord $nextColorWord]} {
set segment [string map {"%^RESET%^" ""} $segment]
} elseif { (! $boldFlag) && (! [boldWord $nextColorWord]) } {
set segment [string map {"%^RESET%^" ""} $segment]
} else {
set boldFlag 0
}
}
set newLine [string cat $newLine $segment]
}
set line $newLine
##Color sequencing: color codes that account for empty space
set codeList [regexp -inline -all -- {%\^\w+%\^[^%]*} $line]
set newLine ""
for {set i 0} {$i<[llength $codeList]} {incr i} {
set segment [lindex $codeList $i]
set colorWord [getColorWord $segment]
if {! ([boldWord $colorWord] && $colorWord == "RESET")} {
if {[regexp -- {%\^\w+%\^ +$} $segment]} {
set segment " "
}
}
set newLine [string cat $newLine $segment]
}
set line $newLine
#General sequencing: non-resets and non-bolds that account for nothing
set codeList [regexp -inline -all -- {%\^\w+%\^[^%]*} $line]
set newLine ""
for {set i 0} {$i<[llength $codeList]} {incr i} {
set segment [lindex $codeList $i]
set colorWord [getColorWord $segment]
if {$colorWord != "RESET" && $colorWord != "BOLD"} {
if {[regexp -- {%\^\w+%\^$} $segment]} {
set segment ""
}
}
set newLine [string cat $newLine $segment]
}
return $newLine
}
while {[gets stdin text] >= 0} {
puts "[optimizeCode $text]"
}