From 617cc31fa3aa82c4ac2422f5ad5ff86c0788ee14 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Sat, 2 May 2020 00:14:39 +0200 Subject: [PATCH] show datetime in cli - experiment --- cmd/cli/highlight.go | 7 +++++++ cmd/cli/item.go | 18 ++++++++++++++---- cmd/cli/main.go | 2 +- pkg/records/records.go | 17 +++++++++-------- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/cmd/cli/highlight.go b/cmd/cli/highlight.go index 6a8c39d..b748a0a 100644 --- a/cmd/cli/highlight.go +++ b/cmd/cli/highlight.go @@ -41,6 +41,13 @@ func highlightSelected(str string) string { return invert + cleanHighlight(str) + end } +func highlightDate(str string) string { + // template "\033[3%d;%dm" + yellowNormal := "\033[33m" + end := "\033[0m" + return yellowNormal + cleanHighlight(str) + end +} + func highlightHost(str string) string { // template "\033[3%d;%dm" redNormal := "\033[31m" diff --git a/cmd/cli/item.go b/cmd/cli/item.go index 349da75..fd3cb7b 100644 --- a/cmd/cli/item.go +++ b/cmd/cli/item.go @@ -6,13 +6,14 @@ import ( "log" "strconv" "strings" + "time" "github.com/curusarn/resh/pkg/records" ) type item struct { - // dateWithColor string - // date string + dateWithColor string + date string // [host:]pwd locationWithColor string @@ -36,8 +37,11 @@ func (i item) less(i2 item) bool { return i.score > i2.score } -func (i item) produceLine(flagLength int) (string, int) { +func (i item) produceLine(flagLength int, showDate bool) (string, int) { line := "" + if showDate { + line += i.dateWithColor + } line += i.locationWithColor line += i.flagsWithColor flags := i.flags @@ -167,7 +171,11 @@ func newItemFromRecordForQuery(record records.CliRecord, query query, debug bool // DISPLAY // DISPLAY > date - // TODO + secs := int64(record.RealtimeBeforeLocal) + nsecs := int64((record.RealtimeBeforeLocal - float64(secs)) * 1e9) + tm := time.Unix(secs, nsecs) + date := tm.Format("2006-01-02_15:04 ") + dateWithColor := highlightDate(date) // DISPLAY > location location := "" @@ -213,6 +221,8 @@ func newItemFromRecordForQuery(record records.CliRecord, query query, debug bool cmdLineWithColor := strings.ReplaceAll(cmd, "\n", ";") it := item{ + date: date, + dateWithColor: dateWithColor, location: location, locationWithColor: locationWithColor, flags: flags, diff --git a/cmd/cli/main.go b/cmd/cli/main.go index a843683..147f90b 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -456,7 +456,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { } break } - displayStr, _ := itm.produceLine(longestFlagsLen) + displayStr, _ := itm.produceLine(longestFlagsLen, true) if m.s.highlightedItem == i { // maxX * 2 because there are escape sequences that make it hard to tell the real string lenght displayStr = doHighlightString(displayStr, maxX*2) diff --git a/pkg/records/records.go b/pkg/records/records.go index b8d9d67..04dd0ca 100644 --- a/pkg/records/records.go +++ b/pkg/records/records.go @@ -158,7 +158,7 @@ type CliRecord struct { GitOriginRemote string `json:"gitOriginRemote"` ExitCode int `json:"exitCode"` - // RealtimeBefore float64 `json:"realtimeBefore"` + RealtimeBeforeLocal float64 `json:"realtimeBeforeLocal"` // RealtimeAfter float64 `json:"realtimeAfter"` // RealtimeDuration float64 `json:"realtimeDuration"` } @@ -166,13 +166,14 @@ type CliRecord struct { // NewCliRecord from EnrichedRecord func NewCliRecord(r EnrichedRecord) CliRecord { return CliRecord{ - SessionID: r.SessionID, - CmdLine: r.CmdLine, - Host: r.Host, - Pwd: r.Pwd, - Home: r.Home, - GitOriginRemote: r.GitOriginRemote, - ExitCode: r.ExitCode, + SessionID: r.SessionID, + CmdLine: r.CmdLine, + Host: r.Host, + Pwd: r.Pwd, + Home: r.Home, + GitOriginRemote: r.GitOriginRemote, + ExitCode: r.ExitCode, + RealtimeBeforeLocal: r.RealtimeBeforeLocal, } }