From 8b2e45c748ba9ef189d312f16bceb07cacbca5c1 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Tue, 5 May 2020 00:27:31 +0200 Subject: [PATCH] make location column responsive, add compact rendering mode --- cmd/cli/item.go | 64 +++-------------- cmd/cli/main.go | 23 ++++-- cmd/cli/time.go | 181 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+), 62 deletions(-) create mode 100644 cmd/cli/time.go diff --git a/cmd/cli/item.go b/cmd/cli/item.go index e9a6ed8..e781c55 100644 --- a/cmd/cli/item.go +++ b/cmd/cli/item.go @@ -65,68 +65,20 @@ func (i item) less(i2 item) bool { // reversed order return i.score > i2.score } - -func formatDatetime(tm time.Time) string { - tmSince := time.Since(tm) - hrs := tmSince.Hours() - yrs := int(hrs / (365 * 24)) - if yrs > 0 { - if yrs == 1 { - return "1 year ago" - } - return strconv.Itoa(yrs) + " years ago" - } - months := int(hrs / (30 * 24)) - if months > 0 { - if months == 1 { - return "1 month ago" - } - return strconv.Itoa(months) + " months ago" - } - days := int(hrs / 24) - if days > 0 { - if days == 1 { - return "1 day ago" - } - return strconv.Itoa(days) + " days ago" - } - hrsInt := int(hrs) - if hrsInt > 0 { - if hrsInt == 1 { - return "1 hour ago" - } - return strconv.Itoa(hrsInt) + " hours ago" - } - mins := int(hrs * 60) - if mins > 0 { - if mins == 1 { - return "1 min ago" - } - return strconv.Itoa(mins) + " mins ago" - } - secs := int(hrs * 60 * 60) - if secs > 0 { - if secs == 1 { - return "1 sec ago" - } - return strconv.Itoa(secs) + " secs ago" - } - return "now" -} - -func (i item) drawItemColumns() itemColumns { +func (i item) drawItemColumns(compactRendering bool) itemColumns { // DISPLAY // DISPLAY > date secs := int64(i.realtimeBefore) nsecs := int64((i.realtimeBefore - float64(secs)) * 1e9) tm := time.Unix(secs, nsecs) - //date := tm.Format("2006-01-02 15:04 ") - // dateLength := len("12 months ago ") // longest date - date := formatDatetime(tm) + " " - // for len(date) < dateLength { - // date = " " + date - // } + + var date string + if compactRendering { + date = formatTimeRelativeShort(tm) + " " + } else { + date = formatTimeRelativeLong(tm) + " " + } dateWithColor := highlightDate(date) // DISPLAY > location diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 06afbf0..a5dc44a 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -412,10 +412,13 @@ func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } -func getHeader() itemColumns { +func getHeader(compactRendering bool) itemColumns { date := "TIME " host := "HOST:" dir := "DIRECTORY" + if compactRendering { + dir = "DIR" + } flags := " FLAGS" cmdLine := "COMMAND-LINE" return itemColumns{ @@ -434,12 +437,19 @@ func getHeader() itemColumns { } } +const smallTerminalTresholdWidth = 110 + func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { maxX, maxY := g.Size() + compactRenderingMode := false + if maxX < smallTerminalTresholdWidth { + compactRenderingMode = true + } + data := []itemColumns{} - header := getHeader() + header := getHeader(compactRenderingMode) longestDateLen := len(header.date) longestLocationLen := len(header.host) + len(header.pwdTilde) longestFlagsLen := 2 @@ -447,7 +457,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { if i == maxY { break } - ic := itm.drawItemColumns() + ic := itm.drawItemColumns(compactRenderingMode) data = append(data, ic) if len(ic.date) > longestDateLen { longestDateLen = len(ic.date) @@ -459,8 +469,9 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { longestFlagsLen = len(ic.flags) } } - if longestLocationLen > maxX/5 { - longestLocationLen = maxX / 5 + maxLocationLen := maxX/7 + 8 + if longestLocationLen > maxLocationLen { + longestLocationLen = maxLocationLen } if m.s.highlightedItem >= len(m.s.data) { @@ -498,7 +509,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { displayStr, _ := itm.produceLine(longestDateLen, longestLocationLen, longestFlagsLen, true) if m.s.highlightedItem == index { // maxX * 2 because there are escape sequences that make it hard to tell the real string lenght - displayStr = doHighlightString(displayStr, maxX*2) + displayStr = doHighlightString(displayStr, maxX*3) if debug { log.Println("### HightlightedItem string :", displayStr) } diff --git a/cmd/cli/time.go b/cmd/cli/time.go new file mode 100644 index 0000000..77ca367 --- /dev/null +++ b/cmd/cli/time.go @@ -0,0 +1,181 @@ +package main + +import ( + "strconv" + "time" +) + +func formatTimeRelativeLongest(tm time.Time) string { + tmSince := time.Since(tm) + hrs := tmSince.Hours() + yrs := int(hrs / (365 * 24)) + if yrs > 0 { + if yrs == 1 { + return "1 year ago" + } + return strconv.Itoa(yrs) + " years ago" + } + months := int(hrs / (30 * 24)) + if months > 0 { + if months == 1 { + return "1 month ago" + } + return strconv.Itoa(months) + " months ago" + } + days := int(hrs / 24) + if days > 0 { + if days == 1 { + return "1 day ago" + } + return strconv.Itoa(days) + " days ago" + } + hrsInt := int(hrs) + if hrsInt > 0 { + if hrsInt == 1 { + return "1 hour ago" + } + return strconv.Itoa(hrsInt) + " hours ago" + } + mins := int(hrs*60) % 60 + if mins > 0 { + if mins == 1 { + return "1 min ago" + } + return strconv.Itoa(mins) + " mins ago" + } + secs := int(hrs*60*60) % 60 + if secs > 0 { + if secs == 1 { + return "1 sec ago" + } + return strconv.Itoa(secs) + " secs ago" + } + return "now" +} + +func formatTimeRelativeLong(tm time.Time) string { + tmSince := time.Since(tm) + hrs := tmSince.Hours() + yrs := int(hrs / (365 * 24)) + if yrs > 0 { + if yrs == 1 { + return "1 year" + } + return strconv.Itoa(yrs) + " years" + } + months := int(hrs / (30 * 24)) + if months > 0 { + if months == 1 { + return "1 month" + } + return strconv.Itoa(months) + " months" + } + days := int(hrs / 24) + if days > 0 { + if days == 1 { + return "1 day" + } + return strconv.Itoa(days) + " days" + } + hrsInt := int(hrs) + if hrsInt > 0 { + if hrsInt == 1 { + return "1 hour" + } + return strconv.Itoa(hrsInt) + " hours" + } + mins := int(hrs*60) % 60 + if mins > 0 { + if mins == 1 { + return "1 min" + } + return strconv.Itoa(mins) + " mins" + } + secs := int(hrs*60*60) % 60 + if secs > 0 { + if secs == 1 { + return "1 sec" + } + return strconv.Itoa(secs) + " secs" + } + return "now" +} + +func formatTimeMixedLongest(tm time.Time) string { + tmSince := time.Since(tm) + hrs := tmSince.Hours() + yrs := int(hrs / (365 * 24)) + if yrs > 0 { + if yrs == 1 { + return "1 year ago" + } + return strconv.Itoa(yrs) + " years ago" + } + months := int(hrs / (30 * 24)) + if months > 0 { + if months == 1 { + return "1 month ago" + } + return strconv.Itoa(months) + " months ago" + } + days := int(hrs / 24) + if days > 0 { + if days == 1 { + return "1 day ago" + } + return strconv.Itoa(days) + " days ago" + } + hrsInt := int(hrs) + mins := int(hrs*60) % 60 + return strconv.Itoa(hrsInt) + ":" + strconv.Itoa(mins) +} + +func formatTimeRelativeShort(tm time.Time) string { + tmSince := time.Since(tm) + hrs := tmSince.Hours() + yrs := int(hrs / (365 * 24)) + if yrs > 0 { + return strconv.Itoa(yrs) + " Y" + } + months := int(hrs / (30 * 24)) + if months > 0 { + return strconv.Itoa(months) + " M" + } + days := int(hrs / 24) + if days > 0 { + return strconv.Itoa(days) + " D" + } + hrsInt := int(hrs) + if hrsInt > 0 { + return strconv.Itoa(hrsInt) + " h" + } + mins := int(hrs*60) % 60 + if mins > 0 { + return strconv.Itoa(mins) + " m" + } + secs := int(hrs*60*60) % 60 + if secs > 0 { + return strconv.Itoa(secs) + " s" + } + return "now" +} + +func formatTimeMixedShort(tm time.Time) string { + tmSince := time.Since(tm) + hrs := tmSince.Hours() + yrs := int(hrs / (365 * 24)) + if yrs > 0 { + return strconv.Itoa(yrs) + " Y" + } + months := int(hrs / (30 * 24)) + if months > 0 { + return strconv.Itoa(months) + " M" + } + days := int(hrs / 24) + if days > 0 { + return strconv.Itoa(days) + " D" + } + hrsInt := int(hrs) + mins := int(hrs*60) % 60 + return strconv.Itoa(hrsInt) + ":" + strconv.Itoa(mins) +}