Skip to content

Commit

Permalink
refactor: make changes after repo moved here (#27)
Browse files Browse the repository at this point in the history
* go mod edit -module gio.tools/icons
* go mod edit -go 1.21.0
* have the generator make the base package's data and the browser's icon data
  • Loading branch information
steverusso authored Sep 17, 2023
1 parent 4c9c3ef commit 1ab7898
Show file tree
Hide file tree
Showing 9 changed files with 2,025 additions and 1,000 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
gio-icon-browser
wasm_assets
/gio-icon-browser
/wasm_assets
90 changes: 68 additions & 22 deletions cmd/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ import (
"golang.org/x/tools/go/packages"
)

const srcHeader = `// generated by go run cmd/gen/main.go. DO NOT EDIT
package main
import (
"golang.org/x/exp/shiny/materialdesign/icons"
)
const numEntries = %d
var allEntries = [%d]iconEntry{
`

func readAndSortNames() ([]string, error) {
names := make([]string, 0, 1000)
cfg := packages.Config{
Expand All @@ -43,27 +30,86 @@ func readAndSortNames() ([]string, error) {
return names, nil
}

func main() {
names, err := readAndSortNames()
const basePkgSrcHeader = `// generated by go run cmd/gen/main.go. DO NOT EDIT
package icons
import "golang.org/x/exp/shiny/materialdesign/icons"
var (
`

func genBasePkgData(names []string) error {
out, err := os.OpenFile("./data.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
log.Fatalf("reading and sorting icon names: %v", err)
return fmt.Errorf("opening out file: %v", err)
}
defer out.Close()

out, err := os.OpenFile("./data.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
nameWidth := 0
for _, name := range names {
if n := len(name); n > nameWidth {
nameWidth = n
}
}

if _, err = fmt.Fprint(out, basePkgSrcHeader); err != nil {
return fmt.Errorf("writing source header: %v", err)
}
for _, name := range names {
fmt.Fprintf(out, "\t%-*s = mi(icons.%s)\n", nameWidth, name, name)
}
if _, err = out.WriteString(")\n"); err != nil {
return fmt.Errorf("writing last parenthesis: %v", err)
}

return nil
}

const browserSrcHeader = `// generated by go run cmd/gen/main.go. DO NOT EDIT
package main
import "gio.tools/icons"
const numEntries = %d
var allEntries = [%d]iconEntry{
`

func genBrowserData(names []string) error {
out, err := os.OpenFile("./cmd/gio-icon-browser/data.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
log.Fatalf("opening out file: %v", err)
return fmt.Errorf("opening out file: %v", err)
}
defer out.Close()

count := len(names)
if _, err = fmt.Fprintf(out, srcHeader, count, count); err != nil {
log.Fatalf("writing source header: %v", err)
if _, err = fmt.Fprintf(out, browserSrcHeader, count, count); err != nil {
return fmt.Errorf("writing source header: %v", err)
}
for _, name := range names {
nameWithSpaces := strings.Join(camelcase.Split(name), " ")
fmt.Fprintf(out, "\t{%q, %q, %q, mi(icons.%s)},\n", nameWithSpaces, name, strings.ToLower(name), name)
fmt.Fprintf(out, "\t{%q, %q, %q, icons.%s},\n", nameWithSpaces, name, strings.ToLower(name), name)
}
if _, err = out.WriteString("}\n"); err != nil {
log.Fatalf("writing last curly bracket: %v", err)
return fmt.Errorf("writing last curly bracket: %v", err)
}

return nil
}

func main() {
names, err := readAndSortNames()
if err != nil {
log.Fatalf("error: reading and sorting icon names: %v", err)
}

if err = genBasePkgData(names); err != nil {
log.Fatalf("error: generating base pkg data: %v", err)
}

if err = genBrowserData(names); err != nil {
log.Fatalf("error: generating browser data: %v", err)
}
}
971 changes: 971 additions & 0 deletions cmd/gio-icon-browser/data.go

Large diffs are not rendered by default.

File renamed without changes.
2 changes: 1 addition & 1 deletion main.go → cmd/gio-icon-browser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type iconEntry struct {
name string // The human readable name.
varName string // The actual variable name in the icons package.
key string // The variable name, but all lowercase for search matching.
icon widget.Icon
icon *widget.Icon
}

type iconBrowser struct {
Expand Down
4 changes: 0 additions & 4 deletions widgets.go → cmd/gio-icon-browser/widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ var (
iconSearch = mustIcon(icons.ActionSearch)
)

// The function `mustIcon` is primarily used as part of each icon entry within
// `data.go`. Shortening the name to `mi` reduces that file size by about 6kb.
var mi = mustIcon

// mustIcon returns a new `widget.Icon` for the given byte slice or panics on error.
func mustIcon(data []byte) widget.Icon {
ic, err := widget.NewIcon(data)
Expand Down
1,934 changes: 965 additions & 969 deletions data.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/steverusso/gio-icon-browser
module gio.tools/icons

go 1.19
go 1.21.0

require (
gioui.org v0.0.0-20221210181309-a22e0f527aa4
Expand Down
16 changes: 16 additions & 0 deletions icons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package icons

import "gioui.org/widget"

// The function `MustIcon` is primarily used to parse each icon's source. Shortening the
// name to `mi` reduces the generated file size by about 6kb.
var mi = MustIcon

// MustIcon returns a new `*widget.Icon` for the given byte slice or panics on error.
func MustIcon(data []byte) *widget.Icon {
ic, err := widget.NewIcon(data)
if err != nil {
panic(err)
}
return ic
}

0 comments on commit 1ab7898

Please sign in to comment.