Skip to content

Commit 8e63589

Browse files
committed
progress: SortByIndex for better control of sorting; fixes #393
1 parent b0a2ab9 commit 8e63589

File tree

22 files changed

+522
-134
lines changed

22 files changed

+522
-134
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ jobs:
3030
# Download all the tools used in the steps that follow
3131
- name: Set up Tools
3232
run: |
33-
go install github.com/fzipp/gocyclo/cmd/[email protected]
34-
go install github.com/mattn/[email protected]
35-
go install github.com/rinchsan/gosimports/cmd/[email protected]
33+
make tools
3634
3735
# Run all the unit-tests
3836
- name: Test

Makefile

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Declare all phony targets (targets that don't create files)
2-
.PHONY: all bench cyclo default demo-colors demo-list demo-progress demo-table fmt help profile test test-race tools vet
2+
.PHONY: all bench cyclo default demo-colors demo-list demo-progress demo-table fmt help lint profile test test-race tools vet
3+
4+
default: help
35

46
# ============================================================================
57
# Main targets
68
# ============================================================================
79

8-
## default: Run tests (default target)
9-
default: test
10-
1110
## all: Run all checks: tests and benchmarks
1211
all: test bench
1312

@@ -19,8 +18,8 @@ all: test bench
1918
bench:
2019
go test -bench=. -benchmem
2120

22-
## test: Run tests with coverage (runs fmt, vet, and cyclo first)
23-
test: fmt vet cyclo
21+
## test: Run tests with coverage (runs fmt, vet, lint, and cyclo first)
22+
test: fmt vet lint cyclo
2423
go test -cover -coverprofile=.coverprofile ./...
2524

2625
## test-race: Run progress demo with race detector
@@ -40,6 +39,10 @@ fmt:
4039
go fmt ./...
4140
gosimports -w .
4241

42+
## lint: Run golangci-lint static analysis
43+
lint:
44+
golangci-lint run ./...
45+
4346
## vet: Run go vet static analysis
4447
vet:
4548
go vet ./...
@@ -79,6 +82,7 @@ profile:
7982

8083
## tools: Install required development tools
8184
tools:
82-
go install github.com/fzipp/gocyclo/cmd/gocyclo@v0.5.1
85+
go install github.com/fzipp/gocyclo/cmd/gocyclo@v0.6.0
8386
go install github.com/rinchsan/gosimports/cmd/[email protected]
87+
go install github.com/golangci/golangci-lint/v2/cmd/[email protected]
8488

cmd/demo-progress/demo.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ var (
2121
flagHideTime = flag.Bool("hide-time", false, "Hide the time taken?")
2222
flagHideValue = flag.Bool("hide-value", false, "Hide the tracker value?")
2323
flagNumTrackers = flag.Int("num-trackers", 13, "Number of Trackers")
24-
flagShowSpeed = flag.Bool("show-speed", false, "Show the tracker speed?")
25-
flagShowSpeedOverall = flag.Bool("show-speed-overall", false, "Show the overall tracker speed?")
26-
flagShowPinned = flag.Bool("show-pinned", false, "Show a pinned message?")
2724
flagRandomFail = flag.Bool("rnd-fail", false, "Introduce random failures in tracking")
2825
flagRandomDefer = flag.Bool("rnd-defer", false, "Introduce random deferred starts")
2926
flagRandomRemove = flag.Bool("rnd-remove", false, "Introduce random remove of trackers on completion")
3027
flagRandomLogs = flag.Bool("rnd-logs", false, "Output random logs in the middle of tracking")
28+
flagSortBy = flag.String("sort-by", "percent-dsc", "Sort trackers by? (none, index, index-dsc, message, message-dsc, percent, percent-dsc, value, value-dsc)")
29+
flagShowSpeed = flag.Bool("show-speed", false, "Show the tracker speed?")
30+
flagShowSpeedOverall = flag.Bool("show-speed-overall", false, "Show the overall tracker speed?")
31+
flagShowPinned = flag.Bool("show-pinned", false, "Show a pinned message?")
3132

3233
messageColors = []text.Color{
3334
text.FgRed,
@@ -100,6 +101,31 @@ func getMessage(idx int64, units *progress.Units) string {
100101
return message
101102
}
102103

104+
func getSortBy() progress.SortBy {
105+
switch *flagSortBy {
106+
case "none":
107+
return progress.SortByNone
108+
case "index":
109+
return progress.SortByIndex
110+
case "index-dsc":
111+
return progress.SortByIndexDsc
112+
case "message":
113+
return progress.SortByMessage
114+
case "message-dsc":
115+
return progress.SortByMessageDsc
116+
case "percent":
117+
return progress.SortByPercent
118+
case "percent-dsc":
119+
return progress.SortByPercentDsc
120+
case "value":
121+
return progress.SortByValue
122+
case "value-dsc":
123+
return progress.SortByValueDsc
124+
default:
125+
return progress.SortByPercentDsc
126+
}
127+
}
128+
103129
func getUnits(idx int64) *progress.Units {
104130
var units *progress.Units
105131
switch {
@@ -123,6 +149,7 @@ func trackSomething(pw progress.Writer, idx int64, updateMessage bool) {
123149
message := getMessage(idx, units)
124150
tracker := progress.Tracker{
125151
DeferStart: *flagRandomDefer && rng.Float64() < 0.5,
152+
Index: uint64(idx),
126153
Message: message,
127154
RemoveOnCompletion: *flagRandomRemove && rng.Float64() < 0.25,
128155
Total: total,
@@ -175,7 +202,7 @@ func main() {
175202
pw.SetAutoStop(*flagAutoStop)
176203
pw.SetMessageLength(24)
177204
pw.SetNumTrackersExpected(*flagNumTrackers)
178-
pw.SetSortBy(progress.SortByPercentDsc)
205+
pw.SetSortBy(getSortBy())
179206
pw.SetStyle(progress.StyleDefault)
180207
pw.SetTrackerLength(25)
181208
pw.SetTrackerPosition(progress.PositionRight)

list/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ func (l *List) hasMoreItemsInLevel(levelIdx int, fromItemIdx int) bool {
160160
func (l *List) render(out *strings.Builder) string {
161161
outStr := out.String()
162162
if l.outputMirror != nil && len(outStr) > 0 {
163-
l.outputMirror.Write([]byte(outStr))
164-
l.outputMirror.Write([]byte("\n"))
163+
_, _ = l.outputMirror.Write([]byte(outStr))
164+
_, _ = l.outputMirror.Write([]byte("\n"))
165165
}
166166
return outStr
167167
}

list/render.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (l *List) renderItem(out *strings.Builder, idx int, item *listItem, hint re
4646

4747
// convert newlines if newlines are not "\n" in l.style
4848
if strings.Contains(itemStr, "\n") && l.style.CharNewline != "\n" {
49-
itemStr = strings.Replace(itemStr, "\n", l.style.CharNewline, -1)
49+
itemStr = strings.ReplaceAll(itemStr, "\n", l.style.CharNewline)
5050
}
5151

5252
// render the item.Text line by line

list/render_html.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (l *List) htmlRenderRecursively(out *strings.Builder, idx int, item *listIt
5151
if l.items[itemIdx].Level == item.Level {
5252
out.WriteString(linePrefix)
5353
out.WriteString(" <li>")
54-
out.WriteString(strings.Replace(html.EscapeString(l.items[itemIdx].Text), "\n", "<br/>", -1))
54+
out.WriteString(strings.ReplaceAll(html.EscapeString(l.items[itemIdx].Text), "\n", "<br/>"))
5555
out.WriteString("</li>\n")
5656
numItemsRendered++
5757
} else if l.items[itemIdx].Level > item.Level { // indent

progress/indicator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ func indeterminateIndicatorDominoes() IndeterminateIndicatorGenerator {
155155
return func(maxLen int) IndeterminateIndicator {
156156
currentPosition := nextPosition
157157

158-
if currentPosition == 0 {
158+
switch currentPosition {
159+
case 0:
159160
direction = 1
160-
} else if currentPosition == maxLen {
161+
case maxLen:
161162
direction = -1
162163
}
163164
nextPosition += direction

0 commit comments

Comments
 (0)