123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package fancycolors
- import (
- "fmt"
- "log"
- "regexp"
- "strings"
- door "git.red-green.com/redgreen/doorgo"
- )
- const pipeCodeRegex = "(\\`(.))+"
- var match_reg = regexp.MustCompile(pipeCodeRegex)
- // Replaces all short color codes into their ANSI colors
- func ReplaceAll(input string) string {
- for {
- hit := match_reg.FindStringIndex(input)
- if len(hit) == 0 {
- return input
- }
- match := input[hit[0]:hit[1]]
- input = strings.ReplaceAll(input, match, Color(match))
- }
- }
- // Removes all short color codes from the string
- func StripAll(input string) string {
- for {
- hit := match_reg.FindStringIndex(input)
- if len(hit) == 0 {
- return input
- }
- match := input[hit[0]:hit[1]]
- input = strings.ReplaceAll(input, match, "")
- }
- }
- // I use the official Darkness v2.0 short codes! (Taken directly from the game)
- //
- // Foreground only (Background is always black, and no blinking)
- func Color(code string) string {
- if code[0] == '`' { // In case we call it manually
- code = code[1:]
- }
- switch code {
- case "1": // Dark Blue
- return door.ColorText("BLU ON BLA")
- case "2": // Dark Green
- return door.ColorText("GRE ON BLA")
- case "3": // Dark Cyan
- return door.ColorText("CYA ON BLA")
- case "4": // Dark Red
- return door.ColorText("RED ON BLA")
- case "5": // Dark Magenta
- return door.ColorText("MAG ON BLA")
- case "6": // Brown
- return door.ColorText("BRO ON BLA")
- case "7": // Gray
- return door.ColorText("WHI ON BLA")
- case "8": // Dark Gray
- return door.ColorText("BRI BLA ON BLA")
- case "9": // Bright Blue
- return door.ColorText("BRI BLU ON BLA")
- case "0": // Bright Green
- return door.ColorText("BRI GRE ON BLA")
- case "!": // Bright Cyan
- return door.ColorText("BRI CYA ON BLA")
- case "@": // Bright Red
- return door.ColorText("BRI RED ON BLA")
- case "#": // Bright Magenta
- return door.ColorText("BRI MAG ON BLA")
- case "$": // Yellow
- return door.ColorText("BRI BRO ON BLA")
- case "%": // White
- return door.ColorText("BRI WHI ON BLA")
- default:
- log.Printf("PipeColor.Color('%s') Invalid code", code)
- return ""
- }
- }
- var cachedPanel *door.Panel
- // Makes a panel to show each of the colors and their codes
- func MakePanel() *door.Panel {
- if cachedPanel == nil {
- width := 22
- p := &door.Panel{
- Width: width,
- Style: door.SINGLE,
- BorderColor: Color("`3"),
- Title: "[ Colors ]",
- TitleColor: Color("`!"),
- TitleOffset: width/2 - 5,
- }
- bar := "\xdb"
- if door.Unicode {
- bar = "\u2588"
- }
- nice := func(colorname, colorsymbol string) {
- p.Lines = append(p.Lines, door.Line{
- Text: fmt.Sprintf(Color("%")+"%-22s"+p.BorderColor, fmt.Sprintf("%-15s %s %s"+bar+bar+bar, colorname, colorsymbol, Color(colorsymbol))),
- })
- }
- nice("Dark Blue", "`1")
- nice("Dark Green", "`2")
- nice("Dark Cyan", "`3")
- nice("Dark Red", "`4")
- nice("Dark Magenta", "`5")
- nice("Brown", "`6")
- nice("Gray", "`7")
- nice("Dark Gray", "`8")
- p.Lines = append(p.Lines, p.Spacer())
- nice("Bright Blue", "`9")
- nice("Bright Green", "`0")
- nice("Bright Cyan", "`!")
- nice("Bright Red", "`@")
- nice("Bright Magenta", "`#")
- nice("Yellow", "`$")
- nice("White", "`%")
- p.Lines = append(p.Lines, door.Line{
- Text: fmt.Sprintf(Color("%")+"%-22s"+p.BorderColor,
- "Black Not Available"),
- })
- cachedPanel = p
- }
- return cachedPanel
- }
|