Skip to content

Commit

Permalink
refactor: add support for go1.16's WalkDir
Browse files Browse the repository at this point in the history
Add support for using the faster WalkDir method when building against
go1.16.
  • Loading branch information
darkliquid committed Mar 29, 2022
1 parent 47b7c3d commit 2e68f77
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.16']
go: ['1.15', '1.16']
steps:
- name: Setup
uses: actions/setup-go@v2
Expand Down
19 changes: 2 additions & 17 deletions finder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sysfont

import (
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -43,14 +42,7 @@ func NewFinder(opts *FinderOpts) *Finder {
}

var fonts []*Font
walker := func(filename string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}

findFonts(opts, func(filename string) error {
// Check file extension.
if extensions := opts.Extensions; len(extensions) > 0 {
extension := filepath.Ext(strings.ToLower(filename))
Expand All @@ -67,14 +59,7 @@ func NewFinder(opts *FinderOpts) *Finder {

fonts = append(fonts, matches...)
return nil
}

// Traverse OS font directories.
for _, dir := range opts.SearchPaths {
if err := filepath.Walk(dir, walker); err != nil {
continue
}
}
})

return &Finder{
fonts: fonts,
Expand Down
29 changes: 29 additions & 0 deletions finder_dirwalk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build go1.16
// +build go1.16

package sysfont

import (
"io/fs"
"path/filepath"
)

func findFonts(opts *FinderOpts, fn func(filename string) error) {
walker := func(filename string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}

return fn(filename)
}

// Traverse OS font directories.
for _, dir := range opts.SearchPaths {
if err := filepath.WalkDir(dir, walker); err != nil {
continue
}
}
}
29 changes: 29 additions & 0 deletions finder_walk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !go1.16
// +build !go1.16

package sysfont

import (
"os"
"path/filepath"
)

func findFonts(opts *FinderOpts, fn func(filename string) error) {
walker := func(filename string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}

return fn(filename)
}

// Traverse OS font directories.
for _, dir := range opts.SearchPaths {
if err := filepath.Walk(dir, walker); err != nil {
continue
}
}
}

0 comments on commit 2e68f77

Please sign in to comment.