Skip to content

Instantly share code, notes, and snippets.

@Alan72104
Last active February 16, 2023 01:18
Show Gist options
  • Save Alan72104/4eef408e96095bb866ece7b91980a244 to your computer and use it in GitHub Desktop.
Save Alan72104/4eef408e96095bb866ece7b91980a244 to your computer and use it in GitHub Desktop.
Script that transforms mc colored string to a discord ansi code block
Global Const $code = ChrW(0x1B)
Enum $FNORMAL = 0, _ ; Normal
$FBOLD = 1, _ ; Bold
$FUNDERLINE = 4 ; Underline
Enum $CGRAY = 30, _ ; Gray 8
$CRED = 31, _ ; Red 4
$CGREEN = 32, _ ; Green 2
$CYELLOW = 33, _ ; Yellow e
$CBLUE = 34, _ ; Blue 1
$CPINK = 35, _ ; Pink 5
$CCYAN = 36, _ ; Cyan 3
$CWHITE = 37 ; White 0
Enum $BDARKBLUE = 40, _ ; Firefly dark blue
$BORANGE = 41, _ ; Orange
$BBLUE = 42, _ ; Marble blue
$BGRAY4 = 43, _ ; Greyish turquoise
$BGRAY3 = 44, _ ; Gray
$BGRAY2 = 45, _ ; Indigo
$BINDIGO = 46, _ ; Light gray
$BWHITE = 47 ; White
Global $map[]
$map["0"] = $CWHITE
$map["1"] = $CBLUE
$map["2"] = $CGREEN
$map["3"] = $CCYAN
$map["4"] = $CRED
$map["5"] = $CPINK
$map["6"] = $CYELLOW
$map["7"] = $CWHITE
$map["8"] = $CGRAY
$map["9"] = $CBLUE
$map["a"] = $CGREEN
$map["b"] = $CBLUE
$map["c"] = $CRED
$map["d"] = $CPINK
$map["e"] = $CYELLOW
$map["f"] = $CWHITE
$map["l"] = $FBOLD
$map["n"] = $FUNDERLINE
$map["r"] = $FNORMAL
$map["z"] = $CWHITE
$map["Z"] = $CWHITE
Func IsFormat($i)
Return $i = 0 Or $i = 1 Or $i = 4
EndFunc
Func IsColor($i)
Return $i >= 30 And $i <= 37
EndFunc
Func IsBg($i)
Return $i >= 40 And $i <= 47
EndFunc
; Global $replaced = ReplaceColors(ClipGet())
Global $replaced = ReplaceColors(MakeRainbow("The quick brown fox jumps over the lazy dog"))
; Global $replaced = ReplaceColors(MakeRainbow("walter white"))
; Global $replaced = ReplaceColors(MakeRainbow("abcdefghijklmnopqr"))
; Global $replaced = ReplaceColors("§4The world you attempted to join is presently occupied!")
c($replaced)
ClipPut($replaced)
Func ReplaceColors($s)
c("Replacing: $", 1, $s)
$s = StringStripCR($s)
Local $new = ""
Local $curF = $FNORMAL
Local $curC = $FNORMAL
Local $curB = $FNORMAL
Local $targetF = $FNORMAL
Local $targetC = $FNORMAL
Local $targetB = $FNORMAL
For $line In StringSplit($s, @LF, 2)
Local $colorCode = False
Local $randomizing = False
For $c In StringSplit($line, "", 2)
If $c == "§" Or $c == "&" Then
If Not $colorCode Then
$colorCode = True
Else
$colorCode = True
FlushFormat($new, $curF, $curC, $curB, $targetF, $targetC, $targetB)
$new &= $randomizing ? GetRandomChar() : $c
EndIf
ElseIf $colorCode And MapExists($map, $c) Then
$colorCode = False
Local $target = $map[$c]
If $c == "r" Then
$randomizing = False
$targetF = $FNORMAL
$targetC = $FNORMAL
$targetB = $FNORMAL
ElseIf $c == "z" Or $c == "Z" Then
$targetC = $CYELLOW
$targetB = $BDARKBLUE
ElseIf IsFormat($target) Then
$targetF = $target
ElseIf IsColor($target) Then
$targetF = $FNORMAL
$targetC = $target
$targetB = $FNORMAL
ElseIf IsBg($target) Then
$targetB = $target
EndIf
ElseIf $colorCode And ($c == "o" Or $c == "m") Then ; Remove italic/strikethrough
$colorCode = False
$targetF = $FNORMAL
ElseIf $colorCode And $c == "k" Then
$colorCode = False
$randomizing = True
Else
$colorCode = False
If Not StringIsSpace($c) Then
FlushFormat($new, $curF, $curC, $curB, $targetF, $targetC, $targetB)
EndIf
$new &= $randomizing ? GetRandomChar() : $c
EndIf
Next
$new &= @CRLF
$targetF = $FNORMAL
$targetC = $FNORMAL
$targetB = $FNORMAL
Next
$new = iv("```ansi\n$\n```", StringStripWS($new, 3) & $code & "[" & $map["r"] & "m")
Return $new
EndFunc
Func FlushFormat(ByRef $s, ByRef $curF, ByRef $curC, ByRef $curB, ByRef $targetF, ByRef $targetC, ByRef $targetB)
Local $params = ""
Local $requireReformat = False ; Whether to reset format, color, or background
If $curF <> $targetF Then
If $targetF = 0 Then
$requireReformat = True
Else
$params &= $targetF & " "
EndIf
$curF = $targetF
EndIf
If $curC <> $targetC Then
If $targetC = 0 Then
$requireReformat = True
Else
$params &= $targetC & " "
EndIf
$curC = $targetC
EndIf
If $curB <> $targetB Then
If $targetB = 0 Then
$requireReformat = True
Else
$params &= $targetB & " "
EndIf
$curB = $targetB
EndIf
If $requireReformat Then
$params = $map["r"] & " "
; Add them back to account for unchanged formats, when some other format needs to be reset
If $targetF <> 0 Then $params &= $targetF & " "
If $targetC <> 0 Then $params &= $targetC & " "
If $targetB <> 0 Then $params &= $targetB & " "
EndIf
If Not ($params == "") Then
$params = StringStripWS($params, 2) ; Trailing
$params = StringReplace($params, " ", ";")
$s &= $code & "[" & $params & "m"
EndIf
EndFunc
Func GetRandomChar()
Local Static $randomChars = "ÀÁÂÈÊËÍÓÔÕÚßãõğİıŒœŞşŴŵžȇ!\""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u0000ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø׃áíóúñѪº¿®¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αβΓπΣσμτΦΘΩδ∞∅∈∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■"
Return StringMid($randomChars, Random(1, StringLen($randomChars), 1), 1)
EndFunc
Func MakeRainbow($s)
$s = StringStripWS($s, 3)
Local $len = StringLen($s)
Local $codes = [8,4,2,"e",1,5,3,0]
Local $codeCur = 0
Local $cur = 1
Local $new = ""
While $cur <= $len
Local $partLen = Ceiling(($len - ($cur-1)) / (8 - $codeCur))
$new &= "&" & $codes[$codeCur]
$new &= StringMid($s, $cur, $partLen)
$cur += $partLen
$codeCur += 1
WEnd
Return $new
EndFunc
; Old function
Func ReplaceColorsOld($s)
Local Const $code = ChrW(0x1B)
Local Const $map = [["0","37"],["1","34"], _ ; White blue
["2","32"],["3","36"], _ ; Green cyan
["4","31"],["5","35"], _ ; Red pink
["6","33"],["7","37"], _ ; Yelow white
["8","30"],["9","34"], _ ; Gray blue
["a","32"],["b","34"], _ ; Green blue
["c","31"],["d","35"], _ ; Red pink
["e","33"],["f","37"], _ ; Yellow white
["l","1"],["n","4"], _ ; Bold underline
["r","0"], _ ; Reset
["z","37"],["Z","37"]] ; White white
c("Replacing: $", 1, $s)
$s = "&r" & $s
For $i = 0 To UBound($map) - 1
Local $ele = [$map[$i][0], $map[$i][1]]
$s = StringReplace($s, "&" & $ele[0], $code & "[" & $ele[1] & "m", 0, 2)
$s = StringReplace($s, "§" & $ele[0], $code & "[" & $ele[1] & "m", 0, 2)
Next
$s = iv("```ansi\n$\n```", $s)
Return $s
EndFunc
; Consoleout
; Automatically replaces $ to variables given
; Escape $ using $$
; Use \n for newline char
Func c($s = "", $nl = True, $v1 = 0x0, $v2 = 0x0, $v3 = 0x0, _
$v4 = 0x0, $v5 = 0x0, $v6 = 0x0, _
$v7 = 0x0, $v8 = 0x0, $v9 = 0x0, $v10 = 0x0)
$s = StringReplace($s, "\n", @CRLF)
If @NumParams > 2 Then
$s = StringReplace($s, "$$", "@PH@")
$s = StringReplace($s, "$", "@PH2@")
For $i = 1 To @NumParams - 2
; Don't use Eval() to prevent breaking when compiled using stripper param /rm "rename variables"
Switch ($i)
Case 1
$s = StringReplace($s, "@PH2@", $v1, 1)
Case 2
$s = StringReplace($s, "@PH2@", $v2, 1)
Case 3
$s = StringReplace($s, "@PH2@", $v3, 1)
Case 4
$s = StringReplace($s, "@PH2@", $v4, 1)
Case 5
$s = StringReplace($s, "@PH2@", $v5, 1)
Case 6
$s = StringReplace($s, "@PH2@", $v6, 1)
Case 7
$s = StringReplace($s, "@PH2@", $v7, 1)
Case 8
$s = StringReplace($s, "@PH2@", $v8, 1)
Case 9
$s = StringReplace($s, "@PH2@", $v9, 1)
Case 10
$s = StringReplace($s, "@PH2@", $v10, 1)
EndSwitch
If @extended = 0 Then ExitLoop
Next
$s = StringReplace($s, "@PH@", "$")
$s = StringReplace($s, "@PH2@", "$")
EndIf
If $nl Then
ConsoleWrite($s & @CRLF)
Else
ConsoleWrite($s)
EndIf
Return $s
EndFunc
; Insert Variable
; Returns a string with all the given variables inserted into
; Use \n for newline char
Func iv($s = "", $v1 = 0x0, $v2 = 0x0, $v3 = 0x0, _
$v4 = 0x0, $v5 = 0x0, $v6 = 0x0, _
$v7 = 0x0, $v8 = 0x0, $v9 = 0x0, $v10 = 0x0)
$s = StringReplace($s, "\n", @CRLF)
If @NumParams > 1 Then
$s = StringReplace($s, "$$", "@PH@")
$s = StringReplace($s, "$", "@PH2@")
For $i = 1 To @NumParams - 1
; Don't use Eval() to prevent breaking when compiled using stripper param /rm "rename variables"
Switch ($i)
Case 1
$s = StringReplace($s, "@PH2@", $v1, 1)
Case 2
$s = StringReplace($s, "@PH2@", $v2, 1)
Case 3
$s = StringReplace($s, "@PH2@", $v3, 1)
Case 4
$s = StringReplace($s, "@PH2@", $v4, 1)
Case 5
$s = StringReplace($s, "@PH2@", $v5, 1)
Case 6
$s = StringReplace($s, "@PH2@", $v6, 1)
Case 7
$s = StringReplace($s, "@PH2@", $v7, 1)
Case 8
$s = StringReplace($s, "@PH2@", $v8, 1)
Case 9
$s = StringReplace($s, "@PH2@", $v9, 1)
Case 10
$s = StringReplace($s, "@PH2@", $v10, 1)
EndSwitch
If @extended = 0 Then ExitLoop
Next
$s = StringReplace($s, "@PH@", "$")
EndIf
Return $s
EndFunc
; Designed for NppExec Console
; Gets string before a newline from the console with a timeout
; Anything read after the newline in this function is discarded
Func cget($timeout = 2147483647)
Local $s = ""
Local $timer = TimerInit()
While TimerDiff($timer) < $timeout
$s = ConsoleRead()
Local $pos = StringInStr($s, @LF, $STR_NOCASESENSEBASIC)
If $pos <> 0 Then
ConsoleRead()
Return StringStripWS(StringLeft($s, $pos), $STR_STRIPTRAILING)
EndIf
Sleep(50)
WEnd
Return ""
EndFunc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment