diff options
| author | Chris Lu <chrislusf@users.noreply.github.com> | 2020-05-29 10:12:20 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-29 10:12:20 -0700 |
| commit | ea93d21641ca96cde2b71031a33f390a549090a6 (patch) | |
| tree | 6dad9d73f5fd3dbe96fbdf2d3eb0b018fae5a84a /weed/util/inits.go | |
| parent | 5837fe72fc097ce3db4cd839cfc0f6dfe09f9051 (diff) | |
| parent | 1f8782a1ed504e5f7e7b62218bcf4502903d350f (diff) | |
| download | seaweedfs-ea93d21641ca96cde2b71031a33f390a549090a6.tar.xz seaweedfs-ea93d21641ca96cde2b71031a33f390a549090a6.zip | |
Merge pull request #1337 from bingoohuang/master
add Volume Ids column only for max 100 volumes for convenience in the master ui.
Diffstat (limited to 'weed/util/inits.go')
| -rw-r--r-- | weed/util/inits.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/weed/util/inits.go b/weed/util/inits.go new file mode 100644 index 000000000..378878012 --- /dev/null +++ b/weed/util/inits.go @@ -0,0 +1,52 @@ +package util + +import ( + "fmt" + "sort" +) + +// HumanReadableIntsMax joins a serials of inits into a smart one like 1-3 5 ... for human readable. +func HumanReadableIntsMax(max int, ids ...int) string { + if len(ids) <= max { + return HumanReadableInts(ids...) + } + + return HumanReadableInts(ids[:max]...) + " ..." +} + +// 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 +} |
