Skip to content

Commit

Permalink
chore: white color for single entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Feb 10, 2024
1 parent 3d5fbf1 commit 6f46613
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
45 changes: 26 additions & 19 deletions internal/entropy/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package entropy

import (
"fmt"
"os"
"path/filepath"

"github.com/gabotechs/dep-tree/internal/graph"
Expand All @@ -14,14 +15,15 @@ const (
)

type Node struct {
Id int64 `json:"id"`
FileName string `json:"fileName"`
Group string `json:"group,omitempty"`
DirName string `json:"dirName"`
Loc int `json:"loc"`
Size int `json:"size"`
Color []int `json:"color,omitempty"`
IsDir bool `json:"isDir"`
Id int64 `json:"id"`
IsEntrypoint bool `json:"isEntrypoint"`
FileName string `json:"fileName"`
Group string `json:"group,omitempty"`
DirName string `json:"dirName"`
Loc int `json:"loc"`
Size int `json:"size"`
Color []int `json:"color,omitempty"`
IsDir bool `json:"isDir"`
}

type Link struct {
Expand Down Expand Up @@ -51,16 +53,19 @@ func makeGraph(files []string, parser graph.NodeParser[*language.FileInfo], load
if err != nil {
return Graph{}, err
}
var singleEntrypointAbsPath string
var entrypoints []*graph.Node[*language.FileInfo]
if len(files) == 1 {
entrypoint := g.Get(files[0])
if entrypoint == nil {
return Graph{}, fmt.Errorf("could not find entrypoint %s", files[0])
}
entrypoints = append(entrypoints, entrypoint)
singleEntrypointAbsPath = entrypoint.Data.AbsPath
} else {
entrypoints = g.GetNodesWithoutParents()
}

cycles := g.RemoveCycles(entrypoints)
out := Graph{
Nodes: make([]Node, 0),
Expand All @@ -72,25 +77,27 @@ func makeGraph(files []string, parser graph.NodeParser[*language.FileInfo], load
return n.Data.Loc
}), 1)

addedFolders := map[string]bool{}

dirTree := NewDirTree()

for _, node := range allNodes {
dirTree.AddDirsFromFileInfo(node.Data)
}

addedFolders := map[string]bool{}

for _, node := range allNodes {
dirName := filepath.Dir(node.Data.RelPath)
out.Nodes = append(out.Nodes, Node{
Id: node.ID(),
FileName: filepath.Base(node.Data.RelPath),
Group: node.Data.Package,
DirName: dirName + "/",
Loc: node.Data.Loc,
Size: maxNodeSize * node.Data.Loc / maxLoc,
Color: toInt(dirTree.ColorForFileInfo(node.Data, RGB)),
})
n := Node{
Id: node.ID(),
IsEntrypoint: node.Data.AbsPath == singleEntrypointAbsPath,
FileName: filepath.Base(node.Data.RelPath),
Group: node.Data.Package,
DirName: dirName + string(os.PathSeparator),
Loc: node.Data.Loc,
Size: maxNodeSize * node.Data.Loc / maxLoc,
Color: toInt(dirTree.ColorForFileInfo(node.Data, RGB)),
}
out.Nodes = append(out.Nodes, n)

for _, to := range g.FromId(node.Id) {
out.Links = append(out.Links, Link{
Expand Down
4 changes: 2 additions & 2 deletions internal/entropy/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
}

function colorNode(node) {
const [r, g, b] = node['color']
const [r, g, b] = node['isEntrypoint'] ? [255, 255, 255] : node['color']
let alpha = SETTINGS.NODE_ALPHA
if (highlightNodes.size > 0 && !highlightNodes.has(node)) alpha = SETTINGS.UNSELECTED_NODE_ALPHA
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
Expand Down Expand Up @@ -152,7 +152,7 @@
// Node params
.nodeLabel(({fileName, dirName, group, loc}) => selectedNode ? undefined :`<div class="nodeLabel">
<span>${dirName}<span style="font-weight: bold">${fileName}</span></span>
<span>${group ?? ''}</span>
<span>${group != null ? `package: ${group}` : ''}</span>
<span>LOC: ${loc}</span>
</div>`)
.nodeThreeObject(nodeThreeObject)
Expand Down

0 comments on commit 6f46613

Please sign in to comment.