Skip to content

Commit

Permalink
Merge pull request #1391 from cogentcore/miscfix
Browse files Browse the repository at this point in the history
Misc fixes for core from the cogent lab development branch and removal of all lab stuff.
  • Loading branch information
kkoreilly authored Dec 23, 2024
2 parents e7d7703 + 9f1a5d9 commit d797b66
Show file tree
Hide file tree
Showing 409 changed files with 1,913 additions and 43,257 deletions.
48 changes: 0 additions & 48 deletions base/atomiccounter/atomiccounter.go

This file was deleted.

28 changes: 0 additions & 28 deletions base/atomiccounter/atomiccounter_test.go

This file was deleted.

14 changes: 9 additions & 5 deletions base/exec/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ func (st *StdIO) Set(o *StdIO) *StdIO {
func (st *StdIO) SetToOS() *StdIO {
cur := &StdIO{}
cur.SetFromOS()
if sif, ok := st.In.(*os.File); ok {
os.Stdin = sif
} else {
fmt.Printf("In is not an *os.File: %#v\n", st.In)
}
os.Stdout = st.Out.(*os.File)
os.Stderr = st.Err.(*os.File)
os.Stdin = st.In.(*os.File)
return cur
}

Expand Down Expand Up @@ -98,13 +102,10 @@ func IsPipe(rw any) bool {
if rw == nil {
return false
}
w, ok := rw.(io.Writer)
_, ok := rw.(io.Writer)
if !ok {
return false
}
if w == os.Stdout {
return false
}
of, ok := rw.(*os.File)
if !ok {
return false
Expand Down Expand Up @@ -247,6 +248,9 @@ func (st *StdIOState) PopToStart() {
for len(st.InStack) > st.InStart {
st.PopIn()
}
for len(st.PipeIn) > 0 {
CloseReader(st.PipeIn.Pop())
}
}

// ErrIsInOut returns true if the given Err writer is also present
Expand Down
4 changes: 2 additions & 2 deletions base/exec/stdio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func TestStdIO(t *testing.T) {
assert.Equal(t, os.Stdout, st.Out)
assert.Equal(t, os.Stderr, st.Err)
assert.Equal(t, os.Stdin, st.In)
assert.Equal(t, false, st.OutIsPipe())
// assert.Equal(t, false, st.OutIsPipe())

obuf := &bytes.Buffer{}
ibuf := &bytes.Buffer{}
var ss StdIOState
ss.SetFromOS()
ss.StackStart()
assert.Equal(t, false, ss.OutIsPipe())
// assert.Equal(t, false, ss.OutIsPipe())

ss.PushOut(obuf)
assert.NotEqual(t, os.Stdout, ss.Out)
Expand Down
5 changes: 5 additions & 0 deletions base/fileinfo/fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ type FileInfo struct { //types:add
// version control system status, when enabled
VCS vcs.FileStatus `table:"-"`

// Generated indicates that the file is generated and should not be edited.
// For Go files, this regex: `^// Code generated .* DO NOT EDIT\.$` is used.
Generated bool `table:"-"`

// full path to file, including name; for file functions
Path string `table:"-"`
}
Expand Down Expand Up @@ -143,6 +147,7 @@ func (fi *FileInfo) SetMimeInfo() error {
}
fi.Cat = UnknownCategory
fi.Known = Unknown
fi.Generated = IsGeneratedFile(fi.Path)
fi.Kind = ""
mtyp, _, err := MimeFromFile(fi.Path)
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion base/fileinfo/mimetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package fileinfo
import (
"fmt"
"mime"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/h2non/filetype"
Expand Down Expand Up @@ -99,6 +101,18 @@ func MimeFromFile(fname string) (mtype, ext string, err error) {
return "", ext, fmt.Errorf("fileinfo.MimeFromFile could not find mime type for ext: %v file: %v", ext, fn)
}

var generatedRe = regexp.MustCompile(`^// Code generated .* DO NOT EDIT`)

func IsGeneratedFile(fname string) bool {
file, err := os.Open(fname)
if err != nil {
return false
}
head := make([]byte, 2048)
file.Read(head)
return generatedRe.Match(head)
}

// todo: use this to check against mime types!

// MimeToKindMapInit makes sure the MimeToKindMap is initialized from
Expand Down Expand Up @@ -316,7 +330,7 @@ var StandardMimes = []MimeType{
{"text/x-forth", []string{".frt"}, Code, Forth}, // note: ".fs" conflicts with fsharp
{"text/x-fortran", []string{".f", ".F"}, Code, Fortran},
{"text/x-fsharp", []string{".fs", ".fsi"}, Code, FSharp},
{"text/x-gosrc", []string{".go", ".mod", ".work", ".cosh"}, Code, Go},
{"text/x-gosrc", []string{".go", ".mod", ".work", ".goal"}, Code, Go},
{"text/x-haskell", []string{".hs", ".lhs"}, Code, Haskell},
{"text/x-literate-haskell", nil, Code, Haskell}, // todo: not sure if same or not

Expand Down
2 changes: 1 addition & 1 deletion base/fileinfo/typegen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions base/fsx/fsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (
"time"
)

// Filename is used to open a file picker dialog when used as an argument
// type in a function, or as a field value.
type Filename string

// GoSrcDir tries to locate dir in GOPATH/src/ or GOROOT/src/pkg/ and returns its
// full path. GOPATH may contain a list of paths. From Robin Elkind github.com/mewkiz/pkg.
func GoSrcDir(dir string) (absDir string, err error) {
Expand Down
22 changes: 17 additions & 5 deletions base/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,28 @@ func PrintHeader(w io.Writer, pkg string, imports ...string) {
}
}

// Inspect goes through all of the files in the given package
// and calls the given function on each node in files that
// are not generated. The bool return value from the given function
// ExcludeFile returns true if the given file is on the exclude list.
func ExcludeFile(pkg *packages.Package, file *ast.File, exclude ...string) bool {
fpos := pkg.Fset.Position(file.FileStart)
_, fname := filepath.Split(fpos.Filename)
for _, ex := range exclude {
if fname == ex {
return true
}
}
return false
}

// Inspect goes through all of the files in the given package,
// except those listed in the exclude list, and calls the given
// function on each node. The bool return value from the given function
// indicates whether to continue traversing down the AST tree
// of that node and look at its children. If a non-nil error value
// is returned by the given function, the traversal of the tree is
// stopped and the error value is returned.
func Inspect(pkg *packages.Package, f func(n ast.Node) (bool, error)) error {
func Inspect(pkg *packages.Package, f func(n ast.Node) (bool, error), exclude ...string) error {
for _, file := range pkg.Syntax {
if ast.IsGenerated(file) {
if ExcludeFile(pkg, file, exclude...) {
continue
}
var terr error
Expand Down
28 changes: 28 additions & 0 deletions base/iox/imagex/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"os"
"path/filepath"
"strings"

"cogentcore.org/core/base/num"
)

// TestingT is an interface wrapper around *testing.T
Expand Down Expand Up @@ -56,6 +58,25 @@ func CompareColors(cc, ic color.RGBA, tol int) bool {
return true
}

// DiffImage returns the difference between two images,
// with pixels having the abs of the difference between pixels.
func DiffImage(a, b image.Image) image.Image {
ab := a.Bounds()
di := image.NewRGBA(ab)
for y := ab.Min.Y; y < ab.Max.Y; y++ {
for x := ab.Min.X; x < ab.Max.X; x++ {
cc := color.RGBAModel.Convert(a.At(x, y)).(color.RGBA)
ic := color.RGBAModel.Convert(b.At(x, y)).(color.RGBA)
r := uint8(num.Abs(int(cc.R) - int(ic.R)))
g := uint8(num.Abs(int(cc.G) - int(ic.G)))
b := uint8(num.Abs(int(cc.B) - int(ic.B)))
c := color.RGBA{r, g, b, 255}
di.Set(x, y, c)
}
}
return di
}

// Assert asserts that the given image is equivalent
// to the image stored at the given filename in the testdata directory,
// with ".png" added to the filename if there is no extension
Expand All @@ -77,6 +98,7 @@ func Assert(t TestingT, img image.Image, filename string) {

ext := filepath.Ext(filename)
failFilename := strings.TrimSuffix(filename, ext) + ".fail" + ext
diffFilename := strings.TrimSuffix(filename, ext) + ".diff" + ext

if UpdateTestImages {
err := Save(img, filename)
Expand All @@ -87,6 +109,7 @@ func Assert(t TestingT, img image.Image, filename string) {
if err != nil {
t.Errorf("AssertImage: error removing old fail image: %v", err)
}
os.RemoveAll(diffFilename)
return
}

Expand Down Expand Up @@ -133,10 +156,15 @@ func Assert(t TestingT, img image.Image, filename string) {
if err != nil {
t.Errorf("AssertImage: error saving fail image: %v", err)
}
err = Save(DiffImage(img, fimg), diffFilename)
if err != nil {
t.Errorf("AssertImage: error saving diff image: %v", err)
}
} else {
err := os.RemoveAll(failFilename)
if err != nil {
t.Errorf("AssertImage: error removing old fail image: %v", err)
}
os.RemoveAll(diffFilename)
}
}
6 changes: 6 additions & 0 deletions base/keylist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# keylist

keylist implements an ordered list (slice) of items (Values), with a map from a Key (e.g., names) to indexes, to support fast lookup by name. There is also a Keys slice.

This is a different implementation of the [ordmap](../ordmap) package, and has the advantage of direct slice access to the values, instead of having to go through the KeyValue tuple struct in ordmap.

Loading

0 comments on commit d797b66

Please sign in to comment.