Skip to content

Commit

Permalink
Merge pull request #32 from Halleck45/feat_screen_risk
Browse files Browse the repository at this point in the history
Feat screen risk
  • Loading branch information
Halleck45 committed Mar 26, 2024
2 parents 7be140e + 1a5fda0 commit 3d62c3e
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 31 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/containerd/console v1.0.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/VKCOM/php-parser v0.8.2/go.mod h1:wLtaD4M5K8bJPwjkl4BVone8dbMiG1OApbM
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
Expand Down
58 changes: 49 additions & 9 deletions src/Cli/ComponentClassTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ComponentTableClass struct {
files []*pb.File
sortColumnIndex int
table table.Model
search ComponentSearch
}

// index of cols
Expand Down Expand Up @@ -46,28 +47,48 @@ func NewComponentTableClass(isInteractive bool, files []*pb.File) *ComponentTabl

func (v *ComponentTableClass) Render() string {

if len(v.table.Rows()) == 0 {
return "No class found.\n" + StyleHelp(`
Press q or esc to quit.
`).Render()
}

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))

return StyleHelp(`
text := StyleHelp(`
Use arrows to navigate and esc to quit.
Press following keys to sort by column:
(n) by name (l) by LLOC (c) by cyclomatic complexity
(m) by methods (i) by maintainability index
Press (ctrl+F) to search
`).Render() + "\n"

// search box
text += v.search.Render()

// Results
// The results
if len(v.table.Rows()) == 0 {

if v.search.HasSearch() {
text += "No class found with the search: " + v.search.Expression + ".\n" + StyleHelp(`
Press Ctrl+C or Esc to quit.
`).Render()
} else {
text += "No class found.\n" + StyleHelp(`
Press Ctrl+C or Esc to quit.
`).Render()
}
} else {
text = text + baseStyle.Render(v.table.View())
}

`).Render() + "\n" +
baseStyle.Render(v.table.View())
return text
}

func (v *ComponentTableClass) Init() {

// search
if &v.search == nil {
v.search = ComponentSearch{Expression: ""}
}

columns := []table.Column{
{Title: "Class", Width: 35},
{Title: "Commits", Width: 6},
Expand Down Expand Up @@ -101,6 +122,14 @@ func (v *ComponentTableClass) Init() {
continue
}

// Search
if v.search.HasSearch() {
// search by file name
if !v.search.Match(class.Name.Qualified) {
continue
}
}

if class.Stmts == nil {
continue
}
Expand Down Expand Up @@ -231,6 +260,15 @@ func (v *ComponentTableClass) SortByCyclomaticComplexity() {

func (v *ComponentTableClass) Update(msg tea.Msg) {

v.search.Update(msg)
if v.search.IsEnabled() {
// Stop here, and make search do its job
// Apply search
v.Init()
v.table, _ = v.table.Update(msg)
return
}

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
Expand All @@ -244,6 +282,8 @@ func (v *ComponentTableClass) Update(msg tea.Msg) {
v.SortByNumberOfMethods()
case "n":
v.SortByName()
case "ctrl+f":
v.search.Toggle("")
}
case DoRefreshModel:
// refresh the model
Expand Down
121 changes: 101 additions & 20 deletions src/Cli/ComponentFileTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ type ComponentFileTable struct {
files []*pb.File
sortColumnIndex int
table table.Model
search ComponentSearch
}

// index of cols
var colsRisks = map[string]int{
"Name": 0,
"Risk": 1,
"Commits": 2,
"Authors": 3,
"LOC": 4,
"Cyclomatic": 5,
}

func NewComponentFileTable(isInteractive bool, files []*pb.File) *ComponentFileTable {
Expand All @@ -30,29 +41,52 @@ func NewComponentFileTable(isInteractive bool, files []*pb.File) *ComponentFileT

func (v *ComponentFileTable) Render() string {

if len(v.table.Rows()) == 0 {
return "No class found.\n" + StyleHelp(`
Press q or esc to quit.
`).Render()
}

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))

return StyleHelp(`
// Help and handers
text := StyleHelp(`
Use arrows to navigate and esc to quit.
Press following keys to sort by column:
(n) by name (l) by LOC (c) by cyclomatic complexity (g) by commits
(n) by name (l) by LOC (r) by risk
(c) by cyclomatic complexity (g) by commits
Press (ctrl+f) to search.
`).Render() + "\n"

// search box
text = text + v.search.Render()

`).Render() + "\n" +
baseStyle.Render(v.table.View())
// The results
if len(v.table.Rows()) == 0 {

if v.search.HasSearch() {
text += "No file found with the search: " + v.search.Expression + ".\n" + StyleHelp(`
Press Ctrl+C or Esc to quit.
`).Render()
} else {
text += "No file found.\n" + StyleHelp(`
Press Ctrl+C or Esc to quit.
`).Render()
}
} else {
text = text + baseStyle.Render(v.table.View())
}

return text
}

func (v *ComponentFileTable) Init() {

// search
if &v.search == nil {
v.search = ComponentSearch{Expression: ""}
}

columns := []table.Column{
{Title: "File", Width: 70},
{Title: "File", Width: 60},
{Title: "Risk", Width: 9},
{Title: "Commits", Width: 9},
{Title: "Authors", Width: 9},
{Title: "LOC", Width: 9},
Expand All @@ -62,6 +96,14 @@ func (v *ComponentFileTable) Init() {
rows := []table.Row{}
for _, file := range v.files {

// Search
if v.search.HasSearch() {
// search by file name
if !v.search.Match(file.Path) {
continue
}
}

nbCommits := 0
nbCommiters := 0
if file.Commits != nil {
Expand All @@ -74,8 +116,22 @@ func (v *ComponentFileTable) Init() {
cyclo = int(*file.Stmts.Analyze.Complexity.Cyclomatic)
}

risk := float32(0.0)
if file.Stmts != nil && file.Stmts.Analyze != nil && file.Stmts.Analyze.Risk != nil {
risk = float32(file.Stmts.Analyze.Risk.Score)
}

// truncate filename, but to the left
filename := file.Path
if len(filename) > 60 {
filename = "..." + filename[len(filename)-57:]
// remove the extension

}

rows = append(rows, table.Row{
file.Path,
filename,
strconv.FormatFloat(float64(risk), 'f', 2, 32),
strconv.Itoa(nbCommits),
strconv.Itoa(nbCommiters),
strconv.Itoa(int(file.LinesOfCode.GetLinesOfCode())),
Expand Down Expand Up @@ -118,33 +174,54 @@ func (v *ComponentFileTable) Sort(sortColumnIndex int) {
return rows[i][sortColumnIndex] < rows[j][sortColumnIndex]
}

a, _ := strconv.Atoi(rows[i][sortColumnIndex])
b, _ := strconv.Atoi(rows[j][sortColumnIndex])
return a > b
// for floats
a, _ := strconv.ParseFloat(rows[i][sortColumnIndex], 32)
b, _ := strconv.ParseFloat(rows[j][sortColumnIndex], 32)
if a != b {
return a > b
}

// for integers
aInt, _ := strconv.Atoi(rows[i][sortColumnIndex])
bInt, _ := strconv.Atoi(rows[j][sortColumnIndex])
return aInt > bInt
})

v.sortColumnIndex = 0
v.sortColumnIndex = sortColumnIndex
v.table.SetRows(rows)
}

func (v *ComponentFileTable) SortByName() {
v.Sort(0)
v.Sort(colsRisks["Name"])
}

func (v *ComponentFileTable) SortByLoc() {
v.Sort(2)
v.Sort(colsRisks["LOC"])
}

func (v *ComponentFileTable) SortByCommits() {
v.Sort(1)
v.Sort(colsRisks["Commits"])
}

func (v *ComponentFileTable) SortByCyclomaticComplexity() {
v.Sort(3)
v.Sort(colsRisks["Cyclomatic"])
}

func (v *ComponentFileTable) SortByRisk() {
v.Sort(colsRisks["Risk"])
}

func (v *ComponentFileTable) Update(msg tea.Msg) {

v.search.Update(msg)
if v.search.IsEnabled() {
// Stop here, and make search do its job
// Apply search
v.Init()
v.table, _ = v.table.Update(msg)
return
}

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
Expand All @@ -156,6 +233,10 @@ func (v *ComponentFileTable) Update(msg tea.Msg) {
v.SortByCyclomaticComplexity()
case "n":
v.SortByName()
case "r":
v.SortByRisk()
case "ctrl+f":
v.search.Toggle("")
}
}

Expand Down

0 comments on commit 3d62c3e

Please sign in to comment.