From 9f85c6552ace7833a47497e16a71dbc7e7beed0a Mon Sep 17 00:00:00 2001 From: Simon Let Date: Mon, 4 May 2020 23:14:10 +0200 Subject: [PATCH] dynamic location size --- cmd/cli/highlight.go | 14 ++++++++++ cmd/cli/item.go | 62 +++++++++++++++++++++++++------------------- cmd/cli/main.go | 45 +++++++++++++++++++++++++++++--- 3 files changed, 91 insertions(+), 30 deletions(-) diff --git a/cmd/cli/highlight.go b/cmd/cli/highlight.go index b748a0a..a121dc9 100644 --- a/cmd/cli/highlight.go +++ b/cmd/cli/highlight.go @@ -27,6 +27,13 @@ func cleanHighlight(str string) string { return str } +func highlightHeader(str string) string { + underline := "\033[4m" + end := "\033[0m" + // no clean highlight + return underline + str + end +} + func highlightStatus(str string) string { invert := "\033[7;1m" end := "\033[0m" @@ -84,6 +91,13 @@ func highlightGit(str string) string { return greenBold + cleanHighlight(str) + end } +func doHighlightHeader(str string, minLength int) string { + if len(str) < minLength { + str = str + strings.Repeat(" ", minLength-len(str)) + } + return highlightHeader(str) +} + func doHighlightString(str string, minLength int) string { if len(str) < minLength { str = str + strings.Repeat(" ", minLength-len(str)) diff --git a/cmd/cli/item.go b/cmd/cli/item.go index dc791ef..e9a6ed8 100644 --- a/cmd/cli/item.go +++ b/cmd/cli/item.go @@ -11,6 +11,8 @@ import ( "github.com/curusarn/resh/pkg/records" ) +const itemLocationLenght = 30 + type item struct { realtimeBefore float64 @@ -39,8 +41,12 @@ type itemColumns struct { date string // [host:]pwd - locationWithColor string - location string + hostWithColor string + host string + pwdTilde string + samePwd bool + //locationWithColor string + //location string // [G] [E#] flagsWithColor string @@ -124,22 +130,15 @@ func (i item) drawItemColumns() itemColumns { dateWithColor := highlightDate(date) // DISPLAY > location - location := "" - locationWithColor := "" + // DISPLAY > location > host + host := "" + hostWithColor := "" if i.differentHost { - location += i.host + ":" - locationWithColor += highlightHost(i.host) + ":" + host += i.host + ":" + hostWithColor += highlightHost(i.host) + ":" } - const locationLenght = 30 - // const locationLenght = 20 // small screenshots - pwdLength := locationLenght - len(location) + // DISPLAY > location > directory pwdTilde := strings.Replace(i.pwd, i.home, "~", 1) - location += leftCutPadString(pwdTilde, pwdLength) - if i.samePwd { - locationWithColor += highlightPwd(leftCutPadString(pwdTilde, pwdLength)) - } else { - locationWithColor += leftCutPadString(pwdTilde, pwdLength) - } // DISPLAY > flags flags := "" @@ -160,20 +159,22 @@ func (i item) drawItemColumns() itemColumns { // flags += " <" + record.GitOriginRemote + ">" // flagsWithColor += " <" + record.GitOriginRemote + ">" return itemColumns{ - date: date, - dateWithColor: dateWithColor, - location: location, - locationWithColor: locationWithColor, - flags: flags, - flagsWithColor: flagsWithColor, - cmdLine: i.cmdLine, - cmdLineWithColor: i.cmdLineWithColor, + date: date, + dateWithColor: dateWithColor, + host: host, + hostWithColor: hostWithColor, + pwdTilde: pwdTilde, + samePwd: i.samePwd, + flags: flags, + flagsWithColor: flagsWithColor, + cmdLine: i.cmdLine, + cmdLineWithColor: i.cmdLineWithColor, // score: i.score, key: i.key, } } -func (ic itemColumns) produceLine(dateLength int, flagLength int, showDate bool) (string, int) { +func (ic itemColumns) produceLine(dateLength int, locationLength int, flagLength int, showDate bool) (string, int) { line := "" if showDate { date := ic.date @@ -181,9 +182,18 @@ func (ic itemColumns) produceLine(dateLength int, flagLength int, showDate bool) line += " " date += " " } + // TODO: use strings.Repeat line += ic.dateWithColor } - line += ic.locationWithColor + // LOCATION + locationWithColor := ic.hostWithColor + pwdLength := locationLength - len(ic.host) + if ic.samePwd { + locationWithColor += highlightPwd(leftCutPadString(ic.pwdTilde, pwdLength)) + } else { + locationWithColor += leftCutPadString(ic.pwdTilde, pwdLength) + } + line += locationWithColor line += ic.flagsWithColor flags := ic.flags if flagLength < len(ic.flags) { @@ -201,7 +211,7 @@ func (ic itemColumns) produceLine(dateLength int, flagLength int, showDate bool) } line += spacer + ic.cmdLineWithColor - length := len(ic.location) + flagLength + len(spacer) + len(ic.cmdLine) + length := dateLength + locationLength + flagLength + len(spacer) + len(ic.cmdLine) return line, length } diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 4529e22..bd401b2 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -411,13 +411,38 @@ func (m manager) Layout(g *gocui.Gui) error { func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } + +func getHeader() itemColumns { + date := "TIME " + host := "HOST:" + dir := "DIRECTORY" + flags := " FLAGS" + cmdLine := "COMMAND-LINE" + return itemColumns{ + date: date, + dateWithColor: date, + host: host, + hostWithColor: host, + pwdTilde: dir, + samePwd: false, + flags: flags, + flagsWithColor: flags, + cmdLine: cmdLine, + cmdLineWithColor: cmdLine, + // score: i.score, + key: "_HEADERS_", + } +} + func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { maxX, maxY := g.Size() - var data []itemColumns + data := []itemColumns{} - longestDateLen := 0 // at least 0 - longestFlagsLen := 2 // at least 2 + header := getHeader() + longestDateLen := len(header.date) + longestLocationLen := len(header.host) + len(header.pwdTilde) + longestFlagsLen := 2 for i, itm := range m.s.data { if i == maxY { break @@ -427,10 +452,16 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { if len(ic.date) > longestDateLen { longestDateLen = len(ic.date) } + if len(ic.host)+len(ic.pwdTilde) > longestLocationLen { + longestLocationLen = len(ic.host) + len(ic.pwdTilde) + } if len(ic.flags) > longestFlagsLen { longestFlagsLen = len(ic.flags) } } + if longestLocationLen > maxX/5 { + longestLocationLen = maxX / 5 + } if m.s.highlightedItem >= len(m.s.data) { m.s.highlightedItem = len(m.s.data) - 1 @@ -449,6 +480,12 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { mainViewHeight := maxY - topBoxHeight - statusLineHeight - helpLineHeight m.s.displayedItemsCount = mainViewHeight + // header + // header := getHeader() + dispStr, _ := header.produceLine(longestDateLen, longestLocationLen, longestFlagsLen, true) + dispStr = doHighlightHeader(dispStr, maxX*2) + v.WriteString(dispStr + "\n") + var index int for index < len(data) { itm := data[index] @@ -457,7 +494,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { break } - displayStr, _ := itm.produceLine(longestDateLen, longestFlagsLen, true) + 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)