Skip to content

Commit

Permalink
stat: shorten names
Browse files Browse the repository at this point in the history
  • Loading branch information
egonelbre committed Dec 29, 2020
1 parent 25265d9 commit 157887d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ goda list github.com/loov/goda/...:noroot - golang.org/x/tools/...:root
Here's an example output for:

```
goda graph github.com/loov/goda/... | dot -Tsvg -o graph.svg
goda graph github.com/loov/goda/...:root | dot -Tsvg -o graph.svg
```

![github.com/loov/goda dependency graph](./graph.svg)
Expand Down
12 changes: 6 additions & 6 deletions graph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion graph/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (cmd *Command) SetFlags(f *flag.FlagSet) {
f.StringVar(&cmd.docs, "docs", "https://pkg.go.dev/", "override the docs url to use")

f.StringVar(&cmd.outputType, "type", "dot", "output type")
f.StringVar(&cmd.labelFormat, "f", "{{.ID}}\\l{{ .Stat.GoFiles.Lines }} / {{ .Stat.GoFiles.Size }}\\l", "label formatting")
f.StringVar(&cmd.labelFormat, "f", "{{.ID}}\\l{{ .Stat.Go.Lines }} / {{ .Stat.Go.Size }}\\l", "label formatting")

f.BoolVar(&cmd.clusters, "cluster", false, "create clusters")
f.BoolVar(&cmd.shortID, "short", false, "use short package id-s")
Expand Down
12 changes: 6 additions & 6 deletions stat/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ import (
"go/token"
)

// TopDecl stats about top-level declarations.
type TopDecl struct {
// Decls stats about top-level declarations.
type Decls struct {
Func int64
Type int64
Const int64
Var int64
Other int64
}

func (s *TopDecl) Add(b TopDecl) {
func (s *Decls) Add(b Decls) {
s.Func += b.Func
s.Type += b.Type
s.Const += b.Const
s.Var += b.Var
s.Other += b.Other
}

func (s *TopDecl) Total() int64 {
func (s *Decls) Total() int64 {
return s.Func + s.Type + s.Const + s.Var + s.Other
}

func TopDeclFromAst(f *ast.File) TopDecl {
stat := TopDecl{}
func DeclsFromAst(f *ast.File) Decls {
stat := Decls{}
for _, decl := range f.Decls {
switch decl := decl.(type) {
case *ast.GenDecl:
Expand Down
28 changes: 14 additions & 14 deletions stat/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import (
)

type Stat struct {
GoFiles Source
OtherFiles Source
Go Source
Other Source

DeclCount TopDecl
TokenCount Tokens
Decls Decls
Tokens Tokens
}

func (info *Stat) AllFiles() Source {
var c Source
c.Add(info.GoFiles)
c.Add(info.OtherFiles)
c.Add(info.Go)
c.Add(info.Other)
return c
}

func (s *Stat) Add(b Stat) {
s.GoFiles.Add(b.GoFiles)
s.OtherFiles.Add(b.OtherFiles)
s.DeclCount.Add(b.DeclCount)
s.TokenCount.Add(b.TokenCount)
s.Go.Add(b.Go)
s.Other.Add(b.Other)
s.Decls.Add(b.Decls)
s.Tokens.Add(b.Tokens)
}

func Package(p *packages.Package) (Stat, []error) {
Expand All @@ -46,21 +46,21 @@ func Package(p *packages.Package) (Stat, []error) {
}

count := SourceFromBytes(src)
info.GoFiles.Add(count)
info.Go.Add(count)

f, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
if err != nil {
errs = append(errs, fmt.Errorf("failed to parse %q: %w", filename, err))
continue
}

info.DeclCount.Add(TopDeclFromAst(f))
info.TokenCount.Add(TokensFromAst(f))
info.Decls.Add(DeclsFromAst(f))
info.Tokens.Add(TokensFromAst(f))
}

for _, filename := range p.OtherFiles {
count, err := SourceFromPath(filename)
info.OtherFiles.Add(count)
info.Other.Add(count)
if err != nil {
if !errors.Is(err, ErrEmptyFile) {
errs = append(errs, err)
Expand Down

0 comments on commit 157887d

Please sign in to comment.