Created
November 14, 2023 16:39
-
-
Save miekg/f9f723ee8c3168d3def5c6eed614151d to your computer and use it in GitHub Desktop.
replace BBCode [color=#xxxxx] with terminal equivalent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
"strconv" | |
) | |
func main() { | |
flag.Parse() | |
img, err := os.ReadFile(flag.Arg(0)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
img = bytes.ReplaceAll(img, []byte("[/color]"), []byte{0x1b, 0x5b, 0x30, 0x6d}) | |
img = bytes.ReplaceAll(img, []byte("[/font]"), nil) | |
img = bytes.ReplaceAll(img, []byte("[/size]"), nil) | |
img = bytes.ReplaceAll(img, []byte("[size=9px][font=monospace]"), nil) | |
buf := &bytes.Buffer{} | |
color := 0 | |
colstart := 0 | |
for i, c := range img { | |
switch color { | |
case 0: | |
if c == '[' { | |
color++ | |
continue | |
} | |
color = 0 | |
case 1: | |
if c == 'c' { | |
color++ | |
continue | |
} | |
buf.Write([]byte{'['}) | |
color = 0 | |
case 2: | |
if c == 'o' { | |
color++ | |
continue | |
} | |
buf.Write([]byte("[c")) | |
color = 0 | |
case 3: | |
if c == 'l' { | |
color++ | |
continue | |
} | |
buf.Write([]byte("[co")) | |
color = 0 | |
case 4: | |
if c == 'o' { | |
color++ | |
continue | |
} | |
color = 0 | |
case 5: | |
if c == 'r' { | |
color++ | |
continue | |
} | |
color = 0 | |
case 6: | |
if c == '=' { | |
color++ | |
continue | |
} | |
color = 0 | |
} | |
if color == 7 && c == '#' { | |
colstart = i + 1 | |
} | |
if color == 7 && c == ']' { | |
// parse each of the 3 tupels to decimal | |
dec1, _ := strconv.ParseInt(string(img[colstart+0:colstart+2]), 16, 64) | |
dec2, _ := strconv.ParseInt(string(img[colstart+2:colstart+4]), 16, 64) | |
dec3, _ := strconv.ParseInt(string(img[colstart+4:colstart+6]), 16, 64) | |
color = 0 | |
cmd := exec.Command("printf", fmt.Sprintf("\033[38;2;%d;%d;%dm", dec1, dec2, dec3)) | |
control, err := cmd.Output() | |
if err != nil { | |
log.Fatal(err) | |
} | |
buf.Write(control) | |
continue | |
} | |
if color == 7 { | |
// as long as we are in the parsing colors; don't output anything. | |
continue | |
} | |
buf.Write([]byte{c}) | |
} | |
os.WriteFile("out", buf.Bytes(), 0644) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment