erm/app/darktile/cmd/fonts.go

45 lines
882 B
Go
Raw Normal View History

2021-07-30 22:29:20 +00:00
package cmd
import (
"fmt"
2023-01-16 06:03:38 +00:00
"os"
"sort"
"text/tabwriter"
2021-07-30 22:29:20 +00:00
2023-01-16 06:03:38 +00:00
"tuxpa.in/t/erm/app/fontinfo"
2021-07-30 22:29:20 +00:00
)
2023-01-16 02:55:18 +00:00
type ListFonts struct {
2023-01-16 06:03:38 +00:00
Styles []string `name:"styles" default:"regular"`
2021-07-30 22:29:20 +00:00
}
2023-01-16 02:55:18 +00:00
func (r *ListFonts) Run(ctx *Context) error {
2023-01-16 06:03:38 +00:00
matchers := []fontinfo.Matcher{}
for _, v := range r.Styles {
matchers = append(matchers, fontinfo.MatchStyle(v))
}
fonts, err := fontinfo.Match(matchers...)
2023-01-16 02:55:18 +00:00
if err != nil {
return err
}
2023-01-16 06:03:38 +00:00
sort.SliceStable(fonts, func(i, j int) bool {
if fonts[i].Style == fonts[j].Style {
return fonts[i].Family < fonts[j].Family
}
return fonts[i].Style < fonts[j].Style
})
w := tabwriter.NewWriter(os.Stdout, 0, 0, 0, ' ',
tabwriter.Debug)
fmt.Fprintln(w, "family\tstyle\tpath")
fmt.Fprintln(w, "---\t---\t---")
2023-01-16 02:55:18 +00:00
for _, font := range fonts {
2023-01-16 06:03:38 +00:00
fmt.Fprintf(w, "%s\t%s\t%s\n",
font.Style,
font.Family,
font.Path,
)
w.Flush()
2023-01-16 02:55:18 +00:00
}
return nil
2021-07-30 22:29:20 +00:00
}