add Volume Ids column only for max 100 volumes for convenience in the master ui.

This commit is contained in:
bingoohuang
2020-05-29 15:37:58 +08:00
parent b4e93b639d
commit 1a642b9876
4 changed files with 77 additions and 0 deletions

43
weed/util/inits.go Normal file
View File

@@ -0,0 +1,43 @@
package util
import (
"fmt"
"sort"
)
// HumanReadableInts joins a serials of inits into a smart one like 1-3 5 7-10 for human readable.
func HumanReadableInts(ids ...int) string {
sort.Ints(ids)
s := ""
start := 0
last := 0
for i, v := range ids {
if i == 0 {
start = v
last = v
s = fmt.Sprintf("%d", v)
continue
}
if last+1 == v {
last = v
continue
}
if last > start {
s += fmt.Sprintf("-%d", last)
}
s += fmt.Sprintf(" %d", v)
start = v
last = v
}
if last != start {
s += fmt.Sprintf("-%d", last)
}
return s
}

14
weed/util/inits_test.go Normal file
View File

@@ -0,0 +1,14 @@
package util
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestHumanReadableInts(t *testing.T) {
assert.Equal(t, "1-3", HumanReadableInts(1, 2, 3))
assert.Equal(t, "1 3", HumanReadableInts(1, 3))
assert.Equal(t, "1 3 5", HumanReadableInts(5, 1, 3))
assert.Equal(t, "1-3 5", HumanReadableInts(1, 2, 3, 5))
assert.Equal(t, "1-3 5 7-9", HumanReadableInts(7, 9, 8, 1, 2, 3, 5))
}