Redo how HOST and PWD are displayed

Fix regression introduced earlier
pull/174/head
Simon Let 4 years ago
parent 3d18e16015
commit a88acedde0
No known key found for this signature in database
GPG Key ID: D650C65DD46D431D
  1. 23
      cmd/cli/main.go
  2. 77
      pkg/searchapp/item.go

@ -13,6 +13,7 @@ import (
"sort" "sort"
"strings" "strings"
"sync" "sync"
"time"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/awesome-gocui/gocui" "github.com/awesome-gocui/gocui"
@ -503,7 +504,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error {
// header // header
// header := getHeader() // header := getHeader()
dispStr, _ := header.ProduceLine(longestDateLen, longestLocationLen, longestFlagsLen, true, true) dispStr, _ := header.ProduceLine(longestDateLen, longestLocationLen, longestFlagsLen, true, true, debug)
dispStr = searchapp.DoHighlightHeader(dispStr, maxX*2) dispStr = searchapp.DoHighlightHeader(dispStr, maxX*2)
v.WriteString(dispStr + "\n") v.WriteString(dispStr + "\n")
@ -515,7 +516,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error {
break break
} }
displayStr, _ := itm.ProduceLine(longestDateLen, longestLocationLen, longestFlagsLen, false, true) displayStr, _ := itm.ProduceLine(longestDateLen, longestLocationLen, longestFlagsLen, false, true, debug)
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 length // maxX * 2 because there are escape sequences that make it hard to tell the real string length
displayStr = searchapp.DoHighlightString(displayStr, maxX*3) displayStr = searchapp.DoHighlightString(displayStr, maxX*3)
@ -596,17 +597,21 @@ func (m manager) rawMode(g *gocui.Gui, v *gocui.View) error {
func SendCliMsg(m msg.CliMsg, port string) msg.CliResponse { func SendCliMsg(m msg.CliMsg, port string) msg.CliResponse {
recJSON, err := json.Marshal(m) recJSON, err := json.Marshal(m)
if err != nil { if err != nil {
log.Fatal("send err 1", err) log.Fatalf("Failed to marshal message: %v\n", err)
} }
req, err := http.NewRequest("POST", "http://localhost:"+port+"/dump", req, err := http.NewRequest(
"POST",
"http://localhost:"+port+"/dump",
bytes.NewBuffer(recJSON)) bytes.NewBuffer(recJSON))
if err != nil { if err != nil {
log.Fatal("send err 2", err) log.Fatalf("Failed to build request: %v\n", err)
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
client := &http.Client{} client := http.Client{
Timeout: 3 * time.Second,
}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Fatal("resh-daemon is not running - try restarting this terminal") log.Fatal("resh-daemon is not running - try restarting this terminal")
@ -615,16 +620,16 @@ func SendCliMsg(m msg.CliMsg, port string) msg.CliResponse {
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Fatal("read response error") log.Fatalf("Read response error: %v\n", err)
} }
// log.Println(string(body)) // log.Println(string(body))
response := msg.CliResponse{} response := msg.CliResponse{}
err = json.Unmarshal(body, &response) err = json.Unmarshal(body, &response)
if err != nil { if err != nil {
log.Fatal("unmarshal resp error: ", err) log.Fatalf("Unmarshal resp error: %v\n", err)
} }
if debug { if debug {
log.Printf("recieved %d records from daemon\n", len(response.CliRecords)) log.Printf("Recieved %d records from daemon\n", len(response.CliRecords))
} }
return response return response
} }

@ -3,6 +3,7 @@ package searchapp
import ( import (
"fmt" "fmt"
"log" "log"
"math"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -46,7 +47,7 @@ type ItemColumns struct {
Date string Date string
// [host:]pwd // [host:]pwd
HostWithColor string differentHost bool
Host string Host string
PwdTilde string PwdTilde string
samePwd bool samePwd bool
@ -132,7 +133,6 @@ func (i Item) DrawItemColumns(compactRendering bool, debug bool) ItemColumns {
DateWithColor: notAvailable + " ", DateWithColor: notAvailable + " ",
// dateWithColor: highlightDate(notAvailable) + " ", // dateWithColor: highlightDate(notAvailable) + " ",
Host: "", Host: "",
HostWithColor: "",
PwdTilde: notAvailable, PwdTilde: notAvailable,
CmdLine: i.CmdLine, CmdLine: i.CmdLine,
CmdLineWithColor: i.CmdLineWithColor, CmdLineWithColor: i.CmdLineWithColor,
@ -157,10 +157,8 @@ func (i Item) DrawItemColumns(compactRendering bool, debug bool) ItemColumns {
// DISPLAY > location // DISPLAY > location
// DISPLAY > location > host // DISPLAY > location > host
host := "" host := ""
hostWithColor := ""
if i.differentHost { if i.differentHost {
host += i.host + ":" host += i.host
hostWithColor += highlightHost(i.host) + ":"
} }
// DISPLAY > location > directory // DISPLAY > location > directory
pwdTilde := strings.Replace(i.pwd, i.home, "~", 1) pwdTilde := strings.Replace(i.pwd, i.home, "~", 1)
@ -188,9 +186,9 @@ func (i Item) DrawItemColumns(compactRendering bool, debug bool) ItemColumns {
Date: date, Date: date,
DateWithColor: dateWithColor, DateWithColor: dateWithColor,
Host: host, Host: host,
HostWithColor: hostWithColor,
PwdTilde: pwdTilde, PwdTilde: pwdTilde,
samePwd: i.samePwd, samePwd: i.samePwd,
differentHost: i.differentHost,
Flags: flags, Flags: flags,
FlagsWithColor: flagsWithColor, FlagsWithColor: flagsWithColor,
CmdLine: i.CmdLine, CmdLine: i.CmdLine,
@ -200,27 +198,63 @@ func (i Item) DrawItemColumns(compactRendering bool, debug bool) ItemColumns {
} }
} }
func minInt(values ...int) int {
min := math.MaxInt32
for _, val := range values {
if val < min {
min = val
}
}
return min
}
func produceLocation(length int, host string, pwdTilde string, differentHost bool, samePwd bool, debug bool) string {
hostLen := len(host)
if hostLen <= 0 {
pwdWithColor := leftCutPadString(pwdTilde, length)
if samePwd {
pwdWithColor = highlightPwd(pwdWithColor)
}
return pwdWithColor
}
colonLen := 1
pwdLen := len(pwdTilde)
totalLen := hostLen + colonLen + pwdLen
// how much we need to shrink/crop the location
shrinkFactor := float32(length) / float32(totalLen)
shrinkedHostLen := int(float32(hostLen) * shrinkFactor)
if debug {
log.Printf("shrinkFactor: %f\n", shrinkFactor)
}
halfLocationLen := length/2 - colonLen
newHostLen := minInt(hostLen, shrinkedHostLen, halfLocationLen)
newPwdLen := length - colonLen - newHostLen
hostWithColor := rightCutPadString(host, newHostLen)
if differentHost {
hostWithColor = highlightHost(hostWithColor)
}
pwdWithColor := leftCutPadString(pwdTilde, newPwdLen)
if samePwd {
pwdWithColor = highlightPwd(pwdWithColor)
}
return hostWithColor + ":" + pwdWithColor
}
// ProduceLine ... // ProduceLine ...
func (ic ItemColumns) ProduceLine(dateLength int, locationLength int, flagLength int, header bool, showDate bool) (string, int) { func (ic ItemColumns) ProduceLine(dateLength int, locationLength int, flagLength int, header bool, showDate bool, debug bool) (string, int) {
line := "" line := ""
if showDate { if showDate {
line += strings.Repeat(" ", dateLength-len(ic.Date)) + ic.DateWithColor line += strings.Repeat(" ", dateLength-len(ic.Date)) + ic.DateWithColor
} }
// LOCATION // LOCATION
var locationWithColor string locationWithColor := produceLocation(locationLength, ic.Host, ic.PwdTilde, ic.differentHost, ic.samePwd, debug)
// ensure that host will not take up all the space
if len(ic.Host) >= locationLength {
locationWithColor = rightCutPadString(ic.Host, locationLength / 2 - 1) + ":"
} else {
locationWithColor = ic.HostWithColor
}
pwdLength := locationLength - len(locationWithColor)
if ic.samePwd {
locationWithColor += highlightPwd(leftCutPadString(ic.PwdTilde, pwdLength))
} else {
locationWithColor += leftCutPadString(ic.PwdTilde, pwdLength)
}
line += locationWithColor line += locationWithColor
// FLAGS
line += ic.FlagsWithColor line += ic.FlagsWithColor
flags := ic.Flags flags := ic.Flags
if flagLength < len(ic.Flags) { if flagLength < len(ic.Flags) {
@ -402,7 +436,7 @@ func NewItemFromRecordForQuery(record records.CliRecord, query Query, debug bool
// GetHeader returns header columns // GetHeader returns header columns
func GetHeader(compactRendering bool) ItemColumns { func GetHeader(compactRendering bool) ItemColumns {
date := "TIME " date := "TIME "
host := "HOST:" host := "HOST"
dir := "DIRECTORY" dir := "DIRECTORY"
if compactRendering { if compactRendering {
dir = "DIR" dir = "DIR"
@ -413,7 +447,6 @@ func GetHeader(compactRendering bool) ItemColumns {
Date: date, Date: date,
DateWithColor: date, DateWithColor: date,
Host: host, Host: host,
HostWithColor: host,
PwdTilde: dir, PwdTilde: dir,
samePwd: false, samePwd: false,
Flags: flags, Flags: flags,

Loading…
Cancel
Save