dynamic location size

pull/137/head
Simon Let 6 years ago
parent 312f92168f
commit 9f85c6552a
  1. 14
      cmd/cli/highlight.go
  2. 62
      cmd/cli/item.go
  3. 45
      cmd/cli/main.go

@ -27,6 +27,13 @@ func cleanHighlight(str string) string {
return str 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 { func highlightStatus(str string) string {
invert := "\033[7;1m" invert := "\033[7;1m"
end := "\033[0m" end := "\033[0m"
@ -84,6 +91,13 @@ func highlightGit(str string) string {
return greenBold + cleanHighlight(str) + end 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 { func doHighlightString(str string, minLength int) string {
if len(str) < minLength { if len(str) < minLength {
str = str + strings.Repeat(" ", minLength-len(str)) str = str + strings.Repeat(" ", minLength-len(str))

@ -11,6 +11,8 @@ import (
"github.com/curusarn/resh/pkg/records" "github.com/curusarn/resh/pkg/records"
) )
const itemLocationLenght = 30
type item struct { type item struct {
realtimeBefore float64 realtimeBefore float64
@ -39,8 +41,12 @@ type itemColumns struct {
date string date string
// [host:]pwd // [host:]pwd
locationWithColor string hostWithColor string
location string host string
pwdTilde string
samePwd bool
//locationWithColor string
//location string
// [G] [E#] // [G] [E#]
flagsWithColor string flagsWithColor string
@ -124,22 +130,15 @@ func (i item) drawItemColumns() itemColumns {
dateWithColor := highlightDate(date) dateWithColor := highlightDate(date)
// DISPLAY > location // DISPLAY > location
location := "" // DISPLAY > location > host
locationWithColor := "" host := ""
hostWithColor := ""
if i.differentHost { if i.differentHost {
location += i.host + ":" host += i.host + ":"
locationWithColor += highlightHost(i.host) + ":" hostWithColor += highlightHost(i.host) + ":"
} }
const locationLenght = 30 // DISPLAY > location > directory
// const locationLenght = 20 // small screenshots
pwdLength := locationLenght - len(location)
pwdTilde := strings.Replace(i.pwd, i.home, "~", 1) 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 // DISPLAY > flags
flags := "" flags := ""
@ -160,20 +159,22 @@ func (i item) drawItemColumns() itemColumns {
// flags += " <" + record.GitOriginRemote + ">" // flags += " <" + record.GitOriginRemote + ">"
// flagsWithColor += " <" + record.GitOriginRemote + ">" // flagsWithColor += " <" + record.GitOriginRemote + ">"
return itemColumns{ return itemColumns{
date: date, date: date,
dateWithColor: dateWithColor, dateWithColor: dateWithColor,
location: location, host: host,
locationWithColor: locationWithColor, hostWithColor: hostWithColor,
flags: flags, pwdTilde: pwdTilde,
flagsWithColor: flagsWithColor, samePwd: i.samePwd,
cmdLine: i.cmdLine, flags: flags,
cmdLineWithColor: i.cmdLineWithColor, flagsWithColor: flagsWithColor,
cmdLine: i.cmdLine,
cmdLineWithColor: i.cmdLineWithColor,
// score: i.score, // score: i.score,
key: i.key, 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 := "" line := ""
if showDate { if showDate {
date := ic.date date := ic.date
@ -181,9 +182,18 @@ func (ic itemColumns) produceLine(dateLength int, flagLength int, showDate bool)
line += " " line += " "
date += " " date += " "
} }
// TODO: use strings.Repeat
line += ic.dateWithColor 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 line += ic.flagsWithColor
flags := ic.flags flags := ic.flags
if flagLength < len(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 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 return line, length
} }

@ -411,13 +411,38 @@ func (m manager) Layout(g *gocui.Gui) error {
func quit(g *gocui.Gui, v *gocui.View) error { func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit 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 { func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error {
maxX, maxY := g.Size() maxX, maxY := g.Size()
var data []itemColumns data := []itemColumns{}
longestDateLen := 0 // at least 0 header := getHeader()
longestFlagsLen := 2 // at least 2 longestDateLen := len(header.date)
longestLocationLen := len(header.host) + len(header.pwdTilde)
longestFlagsLen := 2
for i, itm := range m.s.data { for i, itm := range m.s.data {
if i == maxY { if i == maxY {
break break
@ -427,10 +452,16 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error {
if len(ic.date) > longestDateLen { if len(ic.date) > longestDateLen {
longestDateLen = len(ic.date) longestDateLen = len(ic.date)
} }
if len(ic.host)+len(ic.pwdTilde) > longestLocationLen {
longestLocationLen = len(ic.host) + len(ic.pwdTilde)
}
if len(ic.flags) > longestFlagsLen { if len(ic.flags) > longestFlagsLen {
longestFlagsLen = len(ic.flags) longestFlagsLen = len(ic.flags)
} }
} }
if longestLocationLen > maxX/5 {
longestLocationLen = maxX / 5
}
if m.s.highlightedItem >= len(m.s.data) { if m.s.highlightedItem >= len(m.s.data) {
m.s.highlightedItem = len(m.s.data) - 1 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 mainViewHeight := maxY - topBoxHeight - statusLineHeight - helpLineHeight
m.s.displayedItemsCount = mainViewHeight 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 var index int
for index < len(data) { for index < len(data) {
itm := data[index] itm := data[index]
@ -457,7 +494,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error {
break break
} }
displayStr, _ := itm.produceLine(longestDateLen, longestFlagsLen, true) displayStr, _ := itm.produceLine(longestDateLen, longestLocationLen, longestFlagsLen, true)
if m.s.highlightedItem == index { if m.s.highlightedItem == index {
// maxX * 2 because there are escape sequences that make it hard to tell the real string lenght // 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*2)

Loading…
Cancel
Save