diff --git a/icons/cut.svg b/icons/cut.svg index 7846a8a..aee71be 100644 --- a/icons/cut.svg +++ b/icons/cut.svg @@ -1,7 +1,7 @@ - - - - - - + + + + + + diff --git a/icons/gen_icons.go b/icons/gen_icons.go new file mode 100644 index 0000000..81ca97d --- /dev/null +++ b/icons/gen_icons.go @@ -0,0 +1,104 @@ +//go:build ignore + +//go:generate go run gen_icons.go + +// gen_icons reads SVG files and generates icons/embedded.go with go:embed directives. +// Run: go generate ./icons + +// To regenerate: cd icons && go run gen_icons.go + +package main + +import ( + "bytes" + "fmt" + "go/format" + "io/fs" + "os" + "path/filepath" + "sort" + "strings" + "text/template" +) + +const output = "icons.go" + +var iconTemplate = template.Must(template.New("").Parse(`// Code generated by gen_icons.go; DO NOT EDIT. + +package icons + +import ( + _ "embed" +) + +//go:embed {{.Embeds}} +var svgFS embed.FS + +{{range $name, $files := .Icons}} +// {{$name}}SVG contains the {{$name}} icon SVG source. +var {{$name}}SVG = ` + "`" + `{{.}}` + "`" + ` +{{end}} +`)) + +type iconData struct { + Embeds string + Icons map[string]string +} + +func main() { + icons := make(map[string]string) + var embeds []string + + err := filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() { + return nil + } + if !strings.HasSuffix(path, ".svg") { + return nil + } + + data, err := os.ReadFile(path) + if err != nil { + return err + } + + name := strings.TrimSuffix(filepath.Base(path), ".svg") + icons[name] = string(data) + embeds = append(embeds, filepath.Base(path)) + return nil + }) + if err != nil { + fmt.Fprintf(os.Stderr, "error: %v\n", err) + os.Exit(1) + } + + // Sort embeds for deterministic output + sort.Strings(embeds) + + data := iconData{ + Embeds: strings.Join(embeds, " "), + Icons: icons, + } + + var buf bytes.Buffer + if err := iconTemplate.Execute(&buf, data); err != nil { + fmt.Fprintf(os.Stderr, "template error: %v\n", err) + os.Exit(1) + } + + src, err := format.Source(buf.Bytes()) + if err != nil { + fmt.Fprintf(os.Stderr, "format error: %v\n", err) + os.Exit(1) + } + + if err := os.WriteFile(output, src, 0644); err != nil { + fmt.Fprintf(os.Stderr, "write error: %v\n", err) + os.Exit(1) + } + + fmt.Println("generated icons.go") +} diff --git a/icons/icons.go b/icons/icons.go new file mode 100644 index 0000000..3cbd05f --- /dev/null +++ b/icons/icons.go @@ -0,0 +1,40 @@ +// Code generated by gen_icons.go; DO NOT EDIT. + +package icons + +import ( + _ "embed" +) + +//go:embed copy.svg cut.svg paste.svg +var svgFS embed.FS + +// copySVG contains the copy icon SVG source. +var copySVG = ` + + + +` + +// cutSVG contains the cut icon SVG source. +var cutSVG = ` + + + + + + + + + + +` + +// pasteSVG contains the paste icon SVG source. +var pasteSVG = ` + + + + + +`