-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
table: support sorting by any of the columns (#41)
- Loading branch information
Showing
14 changed files
with
454 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package progress | ||
|
||
import "sort" | ||
|
||
// SortBy helps sort a list of Trackers by various means. | ||
type SortBy int | ||
|
||
const ( | ||
// SortByNone doesn't do any sorting == sort by insertion order. | ||
SortByNone SortBy = iota | ||
|
||
// SortByMessage sorts by the Message alphabetically in ascending order. | ||
SortByMessage | ||
|
||
// SortByMessageDsc sorts by the Message alphabetically in descending order. | ||
SortByMessageDsc | ||
|
||
// SortByPercent sorts by the Percentage complete in ascending order. | ||
SortByPercent | ||
|
||
// SortByPercentDsc sorts by the Percentage complete in descending order. | ||
SortByPercentDsc | ||
|
||
// SortByValue sorts by the Value in ascending order. | ||
SortByValue | ||
|
||
// SortByValueDsc sorts by the Value in descending order. | ||
SortByValueDsc | ||
) | ||
|
||
// Sort applies the sorting method defined by SortBy. | ||
func (sb SortBy) Sort(trackers []*Tracker) { | ||
switch sb { | ||
case SortByMessage: | ||
sort.Sort(sortByMessage(trackers)) | ||
case SortByMessageDsc: | ||
sort.Sort(sortByMessageDsc(trackers)) | ||
case SortByPercent: | ||
sort.Sort(sortByPercent(trackers)) | ||
case SortByPercentDsc: | ||
sort.Sort(sortByPercentDsc(trackers)) | ||
case SortByValue: | ||
sort.Sort(sortByValue(trackers)) | ||
case SortByValueDsc: | ||
sort.Sort(sortByValueDsc(trackers)) | ||
default: | ||
// no sort | ||
} | ||
} | ||
|
||
type sortByMessage []*Tracker | ||
|
||
func (sb sortByMessage) Len() int { return len(sb) } | ||
func (sb sortByMessage) Swap(i, j int) { sb[i], sb[j] = sb[j], sb[i] } | ||
func (sb sortByMessage) Less(i, j int) bool { return sb[i].Message < sb[j].Message } | ||
|
||
type sortByMessageDsc []*Tracker | ||
|
||
func (sb sortByMessageDsc) Len() int { return len(sb) } | ||
func (sb sortByMessageDsc) Swap(i, j int) { sb[i], sb[j] = sb[j], sb[i] } | ||
func (sb sortByMessageDsc) Less(i, j int) bool { return sb[i].Message > sb[j].Message } | ||
|
||
type sortByPercent []*Tracker | ||
|
||
func (sb sortByPercent) Len() int { return len(sb) } | ||
func (sb sortByPercent) Swap(i, j int) { sb[i], sb[j] = sb[j], sb[i] } | ||
func (sb sortByPercent) Less(i, j int) bool { return sb[i].PercentDone() < sb[j].PercentDone() } | ||
|
||
type sortByPercentDsc []*Tracker | ||
|
||
func (sb sortByPercentDsc) Len() int { return len(sb) } | ||
func (sb sortByPercentDsc) Swap(i, j int) { sb[i], sb[j] = sb[j], sb[i] } | ||
func (sb sortByPercentDsc) Less(i, j int) bool { return sb[i].PercentDone() > sb[j].PercentDone() } | ||
|
||
type sortByValue []*Tracker | ||
|
||
func (sb sortByValue) Len() int { return len(sb) } | ||
func (sb sortByValue) Swap(i, j int) { sb[i], sb[j] = sb[j], sb[i] } | ||
func (sb sortByValue) Less(i, j int) bool { return sb[i].value < sb[j].value } | ||
|
||
type sortByValueDsc []*Tracker | ||
|
||
func (sb sortByValueDsc) Len() int { return len(sb) } | ||
func (sb sortByValueDsc) Swap(i, j int) { sb[i], sb[j] = sb[j], sb[i] } | ||
func (sb sortByValueDsc) Less(i, j int) bool { return sb[i].value > sb[j].value } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package progress | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSortBy(t *testing.T) { | ||
trackers := []*Tracker{ | ||
{Message: "Downloading File # 2", Total: 1000, value: 300}, | ||
{Message: "Downloading File # 1", Total: 1000, value: 100}, | ||
{Message: "Downloading File # 3", Total: 1000, value: 500}, | ||
} | ||
|
||
SortByNone.Sort(trackers) | ||
assert.Equal(t, "Downloading File # 2", trackers[0].Message) | ||
assert.Equal(t, "Downloading File # 1", trackers[1].Message) | ||
assert.Equal(t, "Downloading File # 3", trackers[2].Message) | ||
|
||
SortByMessage.Sort(trackers) | ||
assert.Equal(t, "Downloading File # 1", trackers[0].Message) | ||
assert.Equal(t, "Downloading File # 2", trackers[1].Message) | ||
assert.Equal(t, "Downloading File # 3", trackers[2].Message) | ||
|
||
SortByMessageDsc.Sort(trackers) | ||
assert.Equal(t, "Downloading File # 3", trackers[0].Message) | ||
assert.Equal(t, "Downloading File # 2", trackers[1].Message) | ||
assert.Equal(t, "Downloading File # 1", trackers[2].Message) | ||
|
||
SortByPercent.Sort(trackers) | ||
assert.Equal(t, "Downloading File # 1", trackers[0].Message) | ||
assert.Equal(t, "Downloading File # 2", trackers[1].Message) | ||
assert.Equal(t, "Downloading File # 3", trackers[2].Message) | ||
|
||
SortByPercentDsc.Sort(trackers) | ||
assert.Equal(t, "Downloading File # 3", trackers[0].Message) | ||
assert.Equal(t, "Downloading File # 2", trackers[1].Message) | ||
assert.Equal(t, "Downloading File # 1", trackers[2].Message) | ||
|
||
SortByValue.Sort(trackers) | ||
assert.Equal(t, "Downloading File # 1", trackers[0].Message) | ||
assert.Equal(t, "Downloading File # 2", trackers[1].Message) | ||
assert.Equal(t, "Downloading File # 3", trackers[2].Message) | ||
|
||
SortByValueDsc.Sort(trackers) | ||
assert.Equal(t, "Downloading File # 3", trackers[0].Message) | ||
assert.Equal(t, "Downloading File # 2", trackers[1].Message) | ||
assert.Equal(t, "Downloading File # 1", trackers[2].Message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.