Skip to content

Commit

Permalink
refactor: rearrange code
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat committed Dec 11, 2022
1 parent 5fe56b9 commit 13967ce
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 189 deletions.
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"path"
"strings"

"dep-tree/internal/graph"
Expand Down Expand Up @@ -35,7 +36,9 @@ var Root = &cobra.Command{
entrypoint := args[0]

if endsWith(entrypoint, js.Extensions) {
content, err := graph.RenderGraph[js.Data](entrypoint, js.Parser)
content, err := graph.RenderGraph[js.Data](entrypoint, &js.Parser{
path.Dir(entrypoint),
})
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions internal/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ func renderGraph[T any](
})

board := render.MakeBoard(render.BoardOptions{
Indent: 2,
BlockSize: maxSize,
Indent: 2,
})

for i, nodeWithLevel := range nodesWithLevel {
Expand All @@ -98,14 +97,14 @@ func renderGraph[T any](
for _, nodeWithLevel := range nodesWithLevel {
for _, childId := range nodeWithLevel.node.Children.Keys() {
child, _ := nodeWithLevel.node.Children.Get(childId)
err := board.AddDep(nodeWithLevel.node.Id, child.Id)
err := board.AddConnector(nodeWithLevel.node.Id, child.Id)
if err != nil {
return "", err
}
}
}

return board.Render(), nil
return board.Render()
}

func RenderGraph[T any](
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (t *TestGraph) Parse(id string) (*node.Node[[]int], error) {
} else {
children = t.Spec[idInt]
}
return node.MakeNode(id, children), nil
return node.MakeNode(id, id, children), nil
}

func (t *TestGraph) Deps(n *node.Node[[]int]) []string {
Expand Down
10 changes: 5 additions & 5 deletions internal/graph/graph_test/simple.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
0
├▷1
├─┼▷2
│ └─┼▷4 ◁┐
└───┴─┴▷3
0
├▷1
├─┼▷2
│ └─┼▷4◁┐
└───┴─┴▷3
20 changes: 11 additions & 9 deletions internal/js/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ type Data struct {
content []byte
}

type parser struct{}

var Parser = &parser{}
type Parser struct {
RootDir string
}

var _ graph.NodeParser[Data] = &parser{}
var _ graph.NodeParser[Data] = &Parser{}

func retrieveWithExt(absPath string) string {
for _, ext := range Extensions {
Expand All @@ -51,7 +51,7 @@ func retrieveWithExt(absPath string) string {
return ""
}

func (p *parser) Parse(id string) (*node.Node[Data], error) {
func (p *Parser) Parse(id string) (*node.Node[Data], error) {
absPath, err := filepath.Abs(id)
if err != nil {
return nil, err
Expand All @@ -76,13 +76,15 @@ func (p *parser) Parse(id string) (*node.Node[Data], error) {
return nil, err
}

return node.MakeNode(absPath, Data{
dirname: path.Dir(absPath),
dirname := path.Dir(absPath)

return node.MakeNode(absPath, dirname, Data{
dirname: dirname,
content: content,
}), nil
}

func (p *parser) Deps(n *node.Node[Data]) []string {
func (p *Parser) Deps(n *node.Node[Data]) []string {
matched := importRegex.FindAll(n.Data.content, -1)
deps := make([]string, 0)
for _, importMatch := range matched {
Expand All @@ -97,6 +99,6 @@ func (p *parser) Deps(n *node.Node[Data]) []string {
return deps
}

func (p *parser) Display(n *node.Node[Data]) string {
func (p *Parser) Display(n *node.Node[Data]) string {
return path.Join(path.Base(path.Dir(n.Id)), path.Base(n.Id))
}
25 changes: 19 additions & 6 deletions internal/js/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ func TestParser_Parse(t *testing.T) {
absPath, err := filepath.Abs(id)
a.NoError(err)

node, err := Parser.Parse(id)
parser := Parser{
path.Dir(id),
}

node, err := parser.Parse(id)
a.NoError(err)
a.Equal(node.Id, absPath)
a.Equal(node.Data.dirname, path.Dir(absPath))
Expand All @@ -27,22 +31,31 @@ func TestParser_Deps(t *testing.T) {
a := require.New(t)
id := path.Join(testFolder, t.Name()+".js")

node, err := Parser.Parse(id)
parser := Parser{
path.Dir(id),
}

node, err := parser.Parse(id)
a.NoError(err)
deps := Parser.Deps(node)
deps := parser.Deps(node)
a.Equal(len(deps), 9)
}

func TestParser_Deps_imports(t *testing.T) {
a := require.New(t)
id := path.Join(testFolder, t.Name()+".js")
node, err := Parser.Parse(id)

parser := Parser{
path.Dir(id),
}

node, err := parser.Parse(id)
a.NoError(err)

deps := Parser.Deps(node)
deps := parser.Deps(node)
a.Equal(len(deps), 2)
for _, dep := range deps {
node, err := Parser.Parse(dep)
node, err := parser.Parse(dep)
a.NoError(err)
a.Equal(node.Data.content, []byte("console.log(\"hello world!\")\n"))
}
Expand Down
58 changes: 58 additions & 0 deletions internal/render/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package render

import (
"fmt"

"dep-tree/internal/render/graphics"
)

type Point struct {
x int
y int
}

type Block struct {
Id string
Label string
Position Point
}

func (b *Block) Render(d [][]graphics.CellStack) error {
x := b.Position.x
y := b.Position.y
for i := 0; i < len(b.Label); i++ {
idIndex := i
if idIndex >= len(b.Label) || idIndex < 0 {
// nothing here.
} else {
d[y][x+i].PlaceChar(rune(b.Label[idIndex]))
}
}
return nil
}

func (b *Board) AddBlock(id string, label string, column int, row int) error {
if _, ok := b.blocks.Get(id); ok {
return fmt.Errorf("block %s is already present", id)
}

x := column * b.options.Indent
y := row

newW := x + len(label)
newH := y + 1

if newW > b.w {
b.w = newW
}
if newH > b.h {
b.h = newH
}

b.blocks.Set(id, &Block{
Id: id,
Label: label,
Position: Point{x, y},
})
return nil
}

0 comments on commit 13967ce

Please sign in to comment.