Browse Source

Tabs to spaces

master
nm0i 2 years ago
parent
commit
73e3905608
  1. 92
      color.tcl
  2. 21
      functions.tcl
  3. 22
      nm2ansi.tcl
  4. 162
      optimize.tcl

92
color.tcl

@ -14,15 +14,15 @@ array set theme "
"
array set col {
red %^RED%^ bred %^BOLD%^%^RED%^
blue %^BLUE%^ bblue %^BOLD%^%^BLUE%^
orange %^ORANGE%^ borange %^BOLD%^%^ORANGE%^
green %^GREEN%^ bgreen %^BOLD%^%^GREEN%^
black %^BOLD%^%^BLACK%^ bblack %^BOLD%^%^BLACK%^
white %^WHITE%^ bwhite %^BOLD%^%^WHITE%^
cyan %^CYAN%^ bcyan %^BOLD%^%^CYAN%^
magenta %^MAGENTA%^ bmagenta %^BOLD%^%^MAGENTA%^
reset %^RESET%^
red %^RED%^ bred %^BOLD%^%^RED%^
blue %^BLUE%^ bblue %^BOLD%^%^BLUE%^
orange %^ORANGE%^ borange %^BOLD%^%^ORANGE%^
green %^GREEN%^ bgreen %^BOLD%^%^GREEN%^
black %^BOLD%^%^BLACK%^ bblack %^BOLD%^%^BLACK%^
white %^WHITE%^ bwhite %^BOLD%^%^WHITE%^
cyan %^CYAN%^ bcyan %^BOLD%^%^CYAN%^
magenta %^MAGENTA%^ bmagenta %^BOLD%^%^MAGENTA%^
reset %^RESET%^
}
#Randomly colors the word
@ -32,48 +32,48 @@ array set col {
# percentage of secondary color accurance
# OUT: color coded string
proc wordColor {word color rcolor tolerance} {
global rnd col
set len [string length $word]
set charList [regexp -inline -all -- {.} $word]
set newWord ""
foreach {char} $charList {
if {[expr {rand()}] < $tolerance} {
set newWord [string cat $newWord "$col(reset)$col($color)$char$col(reset)$col($rcolor)"]
} else {
set newWord [string cat $newWord $char]
}
}
return $newWord
global rnd col
set len [string length $word]
set charList [regexp -inline -all -- {.} $word]
set newWord ""
foreach {char} $charList {
if {[expr {rand()}] < $tolerance} {
set newWord [string cat $newWord "$col(reset)$col($color)$char$col(reset)$col($rcolor)"]
} else {
set newWord [string cat $newWord $char]
}
}
return $newWord
}
proc lineColor {line} {
global rnd theme col
if {$line == ""} {
return ""
}
set wordList [regexp -inline -all -- {\S+} $line]
foreach {word} $wordList {
if {[string index $word 0] == "@"} {
set color $theme(keyword)
set altColor $theme(keywordalt)
if {[string index $word 1] == "("} {
set tmp [regexp -inline -all -- {\@\((\w+),(\w+)\)} $word]
set color [lindex $tmp 1]
set altColor [lindex $tmp 2]
set word [string range $word [expr [string first ")" $word] + 1] [string length $word]]
} else {
set word [string range $word 1 [string length $word]]
}
set word [wordColor $word $altColor $color $rnd(char)]
lappend newText "$col(reset)$col($color)$word$col(reset)$col($theme(text))"
} else {
lappend newText $word
}
}
return "$col($theme(text))[join $newText " "]$col(reset)"
global rnd theme col
if {$line == ""} {
return ""
}
set wordList [regexp -inline -all -- {\S+} $line]
foreach {word} $wordList {
if {[string index $word 0] == "@"} {
set color $theme(keyword)
set altColor $theme(keywordalt)
if {[string index $word 1] == "("} {
set tmp [regexp -inline -all -- {\@\((\w+),(\w+)\)} $word]
set color [lindex $tmp 1]
set altColor [lindex $tmp 2]
set word [string range $word [expr [string first ")" $word] + 1] [string length $word]]
} else {
set word [string range $word 1 [string length $word]]
}
set word [wordColor $word $altColor $color $rnd(char)]
lappend newText "$col(reset)$col($color)$word$col(reset)$col($theme(text))"
} else {
lappend newText $word
}
}
return "$col($theme(text))[join $newText " "]$col(reset)"
}
while {[gets stdin text] >= 0} {
puts [lineColor $text]
puts [lineColor $text]
}

21
functions.tcl

@ -2,18 +2,18 @@
# IN: string
# OUT: list
proc getColorWord {text} {
return [lindex [regexp -inline -- {%\^(\w+)%\^} $text] 1]
return [lindex [regexp -inline -- {%\^(\w+)%\^} $text] 1]
}
#Tells whether this color code one that causes bold vt sequence
# IN: word
# OUT: boolean
proc boldWord {word} {
if {$word == "BOLD" || $word == "YELLOW"} {
return 1
} else {
return 0
}
if {$word == "BOLD" || $word == "YELLOW"} {
return 1
} else {
return 0
}
}
#Word Wrapper
@ -21,8 +21,9 @@ proc boldWord {word} {
# string
# OUT: string
proc wordWrap {max msg} {
if { [string length $msg] > $max } {
regsub -all "(.{1,$max})( +|$)" $msg "\\1\\3\n" msg
}
return $msg
if { [string length $msg] > $max } {
regsub -all "(.{1,$max})( +|$)" $msg "\\1\\3\n" msg
}
return $msg
}

22
nm2ansi.tcl

@ -9,8 +9,8 @@ source [file join [file dirname [info script]] functions.tcl]
# Maps color codes to vt codes.
# IN: color code
proc codeToColor {code} {
switch $code {
RED {vt::sda_fgred}
switch $code {
RED {vt::sda_fgred}
B_RED {vt::sda_bgred}
BLUE {vt::sda_fgblue}
B_BLUE {vt::sda_bgblue}
@ -37,24 +37,24 @@ proc codeToColor {code} {
vt::sda_reset
vt::sda_fgwhite
}
}
}
}
proc decodeLine {line} {
set codeList [regexp -inline -all -- {%\^\w+%\^[^%]*} $line]
foreach codeWord $codeList {
set colorWord [getColorWord $codeWord]
codeToColor $colorWord
set segment [string map [list "%^${colorWord}%^" ""] $codeWord]
vt::wr $segment
}
set codeList [regexp -inline -all -- {%\^\w+%\^[^%]*} $line]
foreach codeWord $codeList {
set colorWord [getColorWord $codeWord]
codeToColor $colorWord
set segment [string map [list "%^${colorWord}%^" ""] $codeWord]
vt::wr $segment
}
}
vt::sda_reset
vt::sda_fgwhite
while {[gets stdin text] >= 0} {
puts [decodeLine $text]
puts [decodeLine $text]
}
vt::sda_reset

162
optimize.tcl

@ -3,91 +3,91 @@
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
##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]
}
##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
return $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]"
puts "[optimizeCode $text]"
}

Loading…
Cancel
Save