Skip to content

Commit

Permalink
#1268 age sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Oct 2, 2021
1 parent 67cf6f8 commit 42ecf9c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
18 changes: 11 additions & 7 deletions internal/render/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ import (
"k8s.io/apimachinery/pkg/util/duration"
)

var durationRx = regexp.MustCompile(`\A(\d*d)*?(\d*h)*?(\d*m)*?(\d*s)*?\z`)
var durationRx = regexp.MustCompile(`\A(\d*y)*?(\d*d)*?(\d*h)*?(\d*m)*?(\d*s)*?\z`)

func durationToSeconds(duration string) string {
tokens := durationRx.FindAllStringSubmatch(duration, -1)
if len(tokens) == 0 {
return duration
}
if len(tokens[0]) < 5 {
if len(tokens[0]) < 6 {
return duration
}

d, h, m, s := tokens[0][1], tokens[0][2], tokens[0][3], tokens[0][4]
y, d, h, m, s := tokens[0][1], tokens[0][2], tokens[0][3], tokens[0][4], tokens[0][5]
var n int
if v, err := strconv.Atoi(strings.Replace(y, "y", "", 1)); err == nil {
n += v * 365 * 24 * 60 * 60
}
if v, err := strconv.Atoi(strings.Replace(d, "d", "", 1)); err == nil {
n += v * 24 * 60 * 60
}
Expand Down Expand Up @@ -201,7 +204,7 @@ func Truncate(str string, width int) string {
return runewidth.Truncate(str, width, string(tview.SemigraphicsHorizontalEllipsis))
}

func mapToStr(m map[string]string) (s string) {
func mapToStr(m map[string]string) string {
if len(m) == 0 {
return ""
}
Expand All @@ -212,14 +215,15 @@ func mapToStr(m map[string]string) (s string) {
}
sort.Strings(kk)

bb := make([]byte, 0, 100)
for i, k := range kk {
s += k + "=" + m[k]
bb = append(bb, k+"="+m[k]...)
if i < len(kk)-1 {
s += " "
bb = append(bb, ' ')
}
}

return
return string(bb)
}

func mapToIfc(m interface{}) (s string) {
Expand Down
8 changes: 5 additions & 3 deletions internal/render/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestLabelize(t *testing.T) {
}
}

func TestDurationToNumber(t *testing.T) {
func TestDurationToSecond(t *testing.T) {
uu := map[string]struct {
s, e string
}{
Expand All @@ -63,6 +63,8 @@ func TestDurationToNumber(t *testing.T) {
"day_hour": {s: "3d9h", e: "291600"},
"day_hour_minute": {s: "2d22h3m", e: "252180"},
"day_hour_minute_seconds": {s: "2d22h3m50s", e: "252230"},
"year": {s: "3y", e: "94608000"},
"year_day": {s: "1y2d", e: "31708800"},
}

for k := range uu {
Expand Down Expand Up @@ -353,9 +355,9 @@ func BenchmarkMapToStr(b *testing.B) {
"blee": "duh",
"aa": "bb",
}
b.ResetTimer()
b.ReportAllocs()

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mapToStr(ll)
}
Expand Down

0 comments on commit 42ecf9c

Please sign in to comment.