package main import ( "archive/tar" "bufio" "bytes" "compress/gzip" "fmt" "go/format" "io" "log" "net/http" "os" "path" "path/filepath" "regexp" "slices" "strconv" "strings" ) // themesFile is the Go file with the themes Ghostty ships. const themesFile = "ghostty_themes.go" // themeKeys are the keys of a theme of Ghostty's after its palette, in the // order of ghosttyTheme's colors. var themeKeys = []string{"background", "foreground", "cursor-color", "cursor-text", "selection-background", "selection-foreground"} // themesLicense is the license of the themes, iTerm2-Color-Schemes'. const themesLicense = `MIT License Copyright (c) 2011 to Present Mark Badolato Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. This license covers the iTerm-Color-Schemes repository collection of themes. The copyright/license for each individual theme belongs to the author of that theme.` // writeThemes writes the themes that the Ghostty at the commit ships, from // the archive its build.zig.zon names, into themesFile: from the checkout // src, else from GitHub. func writeThemes(src string) { var zon []byte if src != "" { var err error if zon, err = os.ReadFile(filepath.Join(src, "build.zig.zon")); err != nil { log.Fatal(err) } } else { zon = fetch("https://raw.githubusercontent.com/ghostty-org/ghostty/" + commit + "/build.zig.zon") } m := regexp.MustCompile(`\.iterm2_themes\s*=\s*\.\{\s*\.url\s*=\s*"([^"]+)"`).FindSubmatch(zon) if m == nil { log.Fatal("build.zig.zon names no iterm2_themes") } url := string(m[1]) log.Printf("reading the themes of %s", url) gz, err := gzip.NewReader(bytes.NewReader(fetch(url))) if err != nil { log.Fatal(err) } type theme struct { name string colors [22]uint32 } var themes []theme tr := tar.NewReader(gz) for { h, err := tr.Next() if err == io.EOF { break } else if err != nil { log.Fatal(err) } dir, name := path.Split(h.Name) if h.Typeflag != tar.TypeReg || path.Clean(dir) != "ghostty" { continue } data, err := io.ReadAll(tr) if err != nil { log.Fatal(err) } colors, err := parseTheme(data) if err != nil { log.Fatalf("theme %q: %v", name, err) } themes = append(themes, theme{name, colors}) } if len(themes) == 0 { log.Fatal("the archive has no themes") } slices.SortFunc(themes, func(a, b theme) int { return strings.Compare(a.name, b.name) }) var b bytes.Buffer b.WriteString("// Code generated by go generate (internal/libbuild); DO NOT EDIT.\n\npackage terminal\n\n") fmt.Fprintf(&b, "// ghosttyThemes are the themes the Ghostty this package binds ships, from\n// iTerm2-Color-Schemes (https://github.com/mbadolato/iTerm2-Color-Schemes),\n// as %s has them:\n//\n", path.Base(url)) for line := range strings.Lines(themesLicense) { b.WriteString(strings.TrimRight("//\t"+line, " \t\n") + "\n") } b.WriteString("var ghosttyThemes = [...]ghosttyTheme{\n") for _, t := range themes { fmt.Fprintf(&b, "\t{%s, [22]uint32{", strconv.Quote(t.name)) for i, c := range t.colors { if i > 0 { b.WriteString(", ") } fmt.Fprintf(&b, "0x%06x", c) } b.WriteString("}},\n") } b.WriteString("}\n") src2, err := format.Source(b.Bytes()) if err != nil { log.Fatal(err) } if err := os.WriteFile(themesFile, src2, 0o644); err != nil { log.Fatal(err) } log.Printf("wrote %d themes into %s", len(themes), themesFile) } // parseTheme reads the colors of a theme Ghostty ships, which sets each // of them in #rrggbb: anything else is an error, for the terminal package // to read it. func parseTheme(data []byte) ([22]uint32, error) { var colors [22]uint32 set := map[int]bool{} sc := bufio.NewScanner(bytes.NewReader(data)) for sc.Scan() { line := strings.TrimSpace(sc.Text()) if line == "" || line[0] == '#' { continue } key, value, ok := strings.Cut(line, "=") if !ok { return colors, fmt.Errorf("%q sets nothing", line) } key, value = strings.TrimSpace(key), strings.TrimSpace(value) i := slices.Index(themeKeys, key) + 16 if key == "palette" { index, c, _ := strings.Cut(value, "=") n, err := strconv.Atoi(strings.TrimSpace(index)) if err != nil || n < 0 || n > 255 { return colors, fmt.Errorf("%q is no palette entry", value) } if n >= 16 { continue } i, value = n, strings.TrimSpace(c) } else if i < 16 { return colors, fmt.Errorf("unknown key %q", key) } hex, ok := strings.CutPrefix(value, "#") v, err := strconv.ParseUint(hex, 16, 32) if !ok || len(hex) != 6 || err != nil { return colors, fmt.Errorf("%s is %q, not #rrggbb", key, value) } colors[i], set[i] = uint32(v), true } if len(set) != len(colors) { return colors, fmt.Errorf("sets %d colors of %d", len(set), len(colors)) } return colors, sc.Err() } func fetch(url string) []byte { res, err := http.Get(url) if err != nil { log.Fatal(err) } defer res.Body.Close() if res.StatusCode != http.StatusOK { log.Fatalf("%s: %s", url, res.Status) } data, err := io.ReadAll(res.Body) if err != nil { log.Fatal(err) } return data }