Skip to content

Commit

Permalink
print format
Browse files Browse the repository at this point in the history
Signed-off-by: Soham Arora <[email protected]>
  • Loading branch information
arorasoham9 committed Aug 15, 2024
1 parent 4092bce commit d7f2658
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
20 changes: 11 additions & 9 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,12 @@ func ComputeStringDiffs(dmp *diffmatchpatch.DiffMatchPatch, text1, text2 string)
// Enable line mode for faster processing on large texts
diffs := dmp.DiffMain(text1, text2, true)
diffs = dmp.DiffCleanupSemantic(diffs) // Optional: Clean up diff for better readability
return FormatDiffs(diffs)
diffString := FormatDiffs(diffs)
return diffString
}



func FormatDiffs(diffs []diffmatchpatch.Diff) string {

var parts []string
Expand All @@ -635,11 +638,11 @@ func FormatDiffs(diffs []diffmatchpatch.Diff) string {
var prefix string
switch diff.Type {
case diffmatchpatch.DiffInsert:
prefix = colorGreen + "+" + diff.Text + colorReset
prefix = colorGreen + "+" + CheckEmptyTrim(diff.Text) + colorReset
case diffmatchpatch.DiffDelete:
prefix = colorRed + "-" + diff.Text + colorReset
prefix = colorRed + "-" + CheckEmptyTrim(diff.Text) + colorReset
case diffmatchpatch.DiffEqual:
prefix = colorWhite + " " + diff.Text + colorReset
prefix = colorWhite + " " + CheckEmptyTrim(diff.Text) + colorReset
}
parts = append(parts, prefix)
}
Expand Down Expand Up @@ -967,7 +970,7 @@ func compareNodes(dmp *diffmatchpatch.DiffMatchPatch, nodeOne, nodeTwo Node) (No
}
}
}
}
}
return diffedNode, diffs, nil
}
func CompareTwoPaths(dmp *diffmatchpatch.DiffMatchPatch, analysisListOne, analysisListTwo []*Node) ([]Node, [][]string, int, error) {
Expand Down Expand Up @@ -1007,7 +1010,7 @@ func CompareTwoPaths(dmp *diffmatchpatch.DiffMatchPatch, analysisListOne, analys
dumnode.NodeType = "DependencyPackage"
dumnode.DepPkg = model.AllIsDependencyTreeDependencyPackage{}
}

diffNode, diffs, err = compareNodes(dmp, *node, *dumnode)
} else {
diffNode, diffs, err = compareNodes(dmp, *node, *shorterPath[i])
Expand Down Expand Up @@ -1128,7 +1131,6 @@ func CompareAllPaths(listOne, listTwo [][]*Node) (DiffResult, error) {
for i, val := range big {
_, ok := used[i]
if !ok {


//diff each missing path and append to result
var missingPath []Node
Expand All @@ -1143,11 +1145,11 @@ func CompareAllPaths(listOne, listTwo [][]*Node) (DiffResult, error) {
}
dmp := diffmatchpatch.New()
diffNode, _, err := compareNodes(dmp, *node, *dumnode)
if err != nil {
if err != nil {
return DiffResult{}, fmt.Errorf(err.Error())
}
missingPath = append(missingPath, diffNode)

}
pathResults = append(pathResults, DiffedPath{PathOne: val, NodeDiffs: missingPath})
}
Expand Down
25 changes: 13 additions & 12 deletions pkg/analyzer/print_analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

const (
colMinWidth = 50
trimLength = 20
trimLength = 150
)

func GetNodeString(node Node) (string, error) {
Expand Down Expand Up @@ -47,29 +47,30 @@ func GetNodeString(node Node) (string, error) {
return message, nil
case "DependencyPackage":
depPkg := node.DepPkg
message := "Type:" + CheckEmptyTrim(depPkg.Type) + "\n"
message := "Type:" + depPkg.Type + "\n"
for _, namespace := range depPkg.Namespaces {
message += "Namespace: " + CheckEmptyTrim(namespace.Namespace) + "\n"
message += "Namespace: " + namespace.Namespace + "\n"

for _, name := range namespace.Names {
message += "\t"
message += "Name: " + CheckEmptyTrim(name.Name)
message += "Name: " + name.Name
message += "\n"

for _, version := range name.Versions {
message += "\t\t"
message += "Version: " + CheckEmptyTrim(version.Version) + "\n"
message += "Version: " + version.Version + "\n"
message += "\t\t"
message += "Subpath: " + CheckEmptyTrim(version.Subpath) + "\n"
message += "Subpath: " + version.Subpath + "\n"
message += "\t\tQualifiers: {\n"

for _, outlier := range version.Qualifiers {
message += "\t\t\t"
message += CheckEmptyTrim(outlier.Key) + ": " + CheckEmptyTrim(outlier.Value) + "\n"
message += outlier.Key + ": " + outlier.Value + "\n"
}
message += "\t\t}\n"
}
}

message += "\n"
}
return message, nil
Expand Down Expand Up @@ -161,6 +162,8 @@ func PrintDiffedNodeTable(diffs DiffResult) error {

table.SetColumnSeparator("\t\t")
table.SetAutoMergeCells(false)
table.SetAutoWrapText(false)
table.SetRowLine(true)

table.SetHeader([]string{"Node Differences"})
var row []string
Expand All @@ -185,7 +188,6 @@ func PrintDiffedNodeTable(diffs DiffResult) error {
row = append(row, s)
table.Append(row)
table.Append([]string{fmt.Sprintf("Node pair causing %v paths to differ", diff.Count)})
table.Append([]string{"+------------------------------------------------------------------+"})
}

table.SetAlignment(tablewriter.ALIGN_LEFT)
Expand All @@ -204,8 +206,9 @@ func PrintDiffedPathTable(diffs DiffResult) error {
table.SetBorders(tablewriter.Border{Left: true, Bottom: true})

table.SetNoWhiteSpace(true)
table.SetRowLine(true)


table.SetColumnSeparator("\t\t")
table.SetAutoMergeCells(false)

table.SetHeader([]string{"Path Differences"})
Expand All @@ -227,10 +230,8 @@ func PrintDiffedPathTable(diffs DiffResult) error {
}
row = append(row, s)
}

table.Append(row)

table.Append([]string{"+------------------------------------------------------------------+"})
}

table.SetAlignment(tablewriter.ALIGN_LEFT)
Expand Down

0 comments on commit d7f2658

Please sign in to comment.