-
-
Notifications
You must be signed in to change notification settings - Fork 14
Issue #22: Implement sorting using a struct to hold the state of the sorting #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
cfc9cbb
ef0e73b
e0b0aef
7e78f9d
473f319
16c4dbe
fec4a76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,11 +40,32 @@ const ( | |
helpStateFull | ||
) | ||
|
||
type sortType int | ||
|
||
const ( | ||
sortStateByName sortType = iota | ||
sortStateByPercentage | ||
//sortStateByStatements | ||
) | ||
|
||
type sortOrder bool | ||
|
||
const ( | ||
ASC sortOrder = true | ||
DSC sortOrder = false | ||
) | ||
|
||
type SortState struct { | ||
Type sortType | ||
Order sortOrder | ||
} | ||
|
||
// New create a new model that can be used directly in the tea framework. | ||
func New(opts ...Option) *Model { | ||
m := &Model{ | ||
activeView: activeViewList, | ||
helpState: helpStateShort, | ||
sortState: SortState{Type: sortStateByName, Order: ASC}, | ||
|
||
codeRoot: ".", | ||
list: list.New([]list.Item{}, coverProfileDelegate{}, 0, 0), | ||
} | ||
|
@@ -78,9 +99,11 @@ type Model struct { | |
detectedPackageName string | ||
requestedFiles map[string]bool | ||
filteredLinesByFile map[string][]int | ||
profilesLoaded []*cover.Profile | ||
|
||
|
||
activeView viewName | ||
helpState helpState | ||
sortState SortState | ||
ready bool | ||
|
||
err errorview.Model | ||
|
@@ -186,10 +209,8 @@ func (m *Model) onProfilesLoaded(profiles []*cover.Profile) (tea.Model, tea.Cmd) | |
return m.onError(errNoProfiles{}) | ||
} | ||
|
||
if m.sortByCoverage { | ||
sort.Slice(profiles, func(i, j int) bool { | ||
return percentCovered(profiles[i]) < percentCovered(profiles[j]) | ||
}) | ||
if m.sortByCoverage || m.sortState.Type == sortStateByPercentage { | ||
m.sortByPercentage(profiles) | ||
} | ||
|
||
m.items = make([]list.Item, len(profiles)) | ||
|
@@ -254,7 +275,18 @@ func (m *Model) onKeyPressed(key string) (tea.Model, tea.Cmd) { | |
return m, loadFile(adjustedFileName, item.profile) | ||
} | ||
|
||
return m, nil | ||
return m, nil | ||
|
||
//toggle on and insiate sortByCoverage (default = Asc) | ||
case "s": | ||
m.toggleSort() | ||
m.Update(m.profilesLoaded) | ||
|
||
return m, nil | ||
|
||
case "!": | ||
m.toggleSortOrder() | ||
m.Update(m.profilesLoaded) | ||
return m, nil | ||
|
||
case "?": | ||
m.toggleHelp() | ||
|
@@ -264,6 +296,38 @@ func (m *Model) onKeyPressed(key string) (tea.Model, tea.Cmd) { | |
return nil, nil | ||
} | ||
|
||
func (m *Model) toggleSortOrder() { | ||
switch m.sortState.Order { | ||
case ASC: | ||
m.sortState.Order = DSC | ||
case DSC: | ||
m.sortState.Order = ASC | ||
} | ||
} | ||
|
||
func (m *Model) sortByPercentage(profiles []*cover.Profile) { | ||
sort.Slice(profiles, func(i, j int) bool { | ||
if m.sortState.Order == ASC { | ||
return percentCovered(profiles[i]) > percentCovered(profiles[j]) | ||
} else { | ||
return percentCovered(profiles[i]) < percentCovered(profiles[j]) | ||
} | ||
}) | ||
} | ||
|
||
func (m *Model) toggleSort() { | ||
switch m.sortState.Type { | ||
case sortStateByName : | ||
m.sortState.Type = sortStateByPercentage | ||
// sort all profiles by name | ||
|
||
case sortStateByPercentage: | ||
m.sortState.Type = sortStateByName | ||
// sort all profiles by Ascend percent if isAscend is true | ||
// else sort by Descend | ||
} | ||
} | ||
|
||
func (m *Model) toggleHelp() { | ||
// manage help state globally: allow to extend or hide completely | ||
switch m.helpState { | ||
|
@@ -328,6 +392,7 @@ func (m *Model) loadProfiles(codeRoot, profileFilename string) tea.Cmd { | |
|
||
finalProfiles = append(finalProfiles, p) | ||
} | ||
m.profilesLoaded = finalProfiles | ||
|
||
return finalProfiles | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to make these exported if everything else is not exported (use lowercase letters)