Skip to content

Instantly share code, notes, and snippets.

@tacahiroy
Created September 11, 2019 03:58
Show Gist options
  • Select an option

  • Save tacahiroy/39f3b463eed0cad175ce37bc4c720c62 to your computer and use it in GitHub Desktop.

Select an option

Save tacahiroy/39f3b463eed0cad175ce37bc4c720c62 to your computer and use it in GitHub Desktop.
Converts colour settings from minttyrc to Windows Terminal
package main
// This programme generates Windows Terminal colorScheme definition from minttyrc.
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
)
var colorTable map[string]string
func init() {
colorTable = map[string]string{
"CursorColour": "cursorColor",
"ForegroundColour": "foreground",
"BackgroundColour": "background",
"Black": "black",
"BoldBlack": "brightBlack",
"Red": "red",
"BoldRed": "brightRed",
"Green": "green",
"BoldGreen": "brightGreen",
"Yellow": "yellow",
"BoldYellow": "brightYellow",
"Blue": "blue",
"BoldBlue": "brightBlue",
"Magenta": "purple",
"BoldMagenta": "brightPurple",
"Cyan": "cyan",
"BoldCyan": "brightCyan",
"White": "white",
"BoldWhite": "brightWhite",
}
}
func rgbToHex(r string, g string, b string) (string, error) {
red, _ := strconv.Atoi(r)
green, _ := strconv.Atoi(g)
blue, _ := strconv.Atoi(b)
if red > 255 || green > 255 || blue > 255 {
return "", errors.New("RGB value has to be 255 or less.")
}
return fmt.Sprintf("#%02x%02x%02x", red, green, blue), nil
}
func isColorSetting(line string) bool {
for k, _ := range colorTable {
if strings.HasPrefix(strings.TrimRight(line, "\n"), k) {
return true
}
}
return false
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %s minttyrc\n", os.Args[0])
os.Exit(1)
}
b, err := os.OpenFile(os.Args[1], os.O_RDONLY, os.ModePerm)
if err != nil {
fmt.Println("Error reading the file.")
fmt.Printf("%v\n", err)
os.Exit(1)
}
defer b.Close()
reader := bufio.NewReader(b)
for {
text, err := reader.ReadString('\n')
if err == io.EOF {
os.Exit(0)
}
text = strings.TrimRight(text, "\n")
if !isColorSetting(text) {
continue
}
cols := strings.Split(text, "=")
key := cols[0]
rgb := strings.Split(cols[1], ",")
hex, err := rgbToHex(rgb[0], rgb[1], rgb[2])
if err != nil {
fmt.Println(err)
}
fmt.Printf("\"%s\": \"%s\",\n", colorTable[key], hex)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment