diff --git a/cmd/cli/main.go b/cmd/cli/main.go index ec22218..5f117f2 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -13,6 +13,7 @@ import ( "sort" "strings" "sync" + "time" "github.com/BurntSushi/toml" "github.com/awesome-gocui/gocui" @@ -503,7 +504,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { // header // 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) v.WriteString(dispStr + "\n") @@ -515,7 +516,7 @@ func (m manager) normalMode(g *gocui.Gui, v *gocui.View) error { break } - displayStr, _ := itm.ProduceLine(longestDateLen, longestLocationLen, longestFlagsLen, false, true) + displayStr, _ := itm.ProduceLine(longestDateLen, longestLocationLen, longestFlagsLen, false, true, debug) if m.s.highlightedItem == index { // maxX * 2 because there are escape sequences that make it hard to tell the real string length 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 { recJSON, err := json.Marshal(m) 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)) 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") - client := &http.Client{} + client := http.Client{ + Timeout: 3 * time.Second, + } resp, err := client.Do(req) if err != nil { 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() body, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Fatal("read response error") + log.Fatalf("Read response error: %v\n", err) } // log.Println(string(body)) response := msg.CliResponse{} err = json.Unmarshal(body, &response) if err != nil { - log.Fatal("unmarshal resp error: ", err) + log.Fatalf("Unmarshal resp error: %v\n", err) } 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 } diff --git a/pkg/searchapp/item.go b/pkg/searchapp/item.go index eaff1a1..161002b 100644 --- a/pkg/searchapp/item.go +++ b/pkg/searchapp/item.go @@ -3,6 +3,7 @@ package searchapp import ( "fmt" "log" + "math" "strconv" "strings" "time" @@ -46,7 +47,7 @@ type ItemColumns struct { Date string // [host:]pwd - HostWithColor string + differentHost bool Host string PwdTilde string samePwd bool @@ -132,7 +133,6 @@ func (i Item) DrawItemColumns(compactRendering bool, debug bool) ItemColumns { DateWithColor: notAvailable + " ", // dateWithColor: highlightDate(notAvailable) + " ", Host: "", - HostWithColor: "", PwdTilde: notAvailable, CmdLine: i.CmdLine, CmdLineWithColor: i.CmdLineWithColor, @@ -157,10 +157,8 @@ func (i Item) DrawItemColumns(compactRendering bool, debug bool) ItemColumns { // DISPLAY > location // DISPLAY > location > host host := "" - hostWithColor := "" if i.differentHost { - host += i.host + ":" - hostWithColor += highlightHost(i.host) + ":" + host += i.host } // DISPLAY > location > directory pwdTilde := strings.Replace(i.pwd, i.home, "~", 1) @@ -188,9 +186,9 @@ func (i Item) DrawItemColumns(compactRendering bool, debug bool) ItemColumns { Date: date, DateWithColor: dateWithColor, Host: host, - HostWithColor: hostWithColor, PwdTilde: pwdTilde, samePwd: i.samePwd, + differentHost: i.differentHost, Flags: flags, FlagsWithColor: flagsWithColor, 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 ... -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 := "" if showDate { line += strings.Repeat(" ", dateLength-len(ic.Date)) + ic.DateWithColor } // LOCATION - var locationWithColor string - // 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) - } + locationWithColor := produceLocation(locationLength, ic.Host, ic.PwdTilde, ic.differentHost, ic.samePwd, debug) line += locationWithColor + + // FLAGS line += ic.FlagsWithColor flags := ic.Flags if flagLength < len(ic.Flags) { @@ -402,7 +436,7 @@ func NewItemFromRecordForQuery(record records.CliRecord, query Query, debug bool // GetHeader returns header columns func GetHeader(compactRendering bool) ItemColumns { date := "TIME " - host := "HOST:" + host := "HOST" dir := "DIRECTORY" if compactRendering { dir = "DIR" @@ -413,7 +447,6 @@ func GetHeader(compactRendering bool) ItemColumns { Date: date, DateWithColor: date, Host: host, - HostWithColor: host, PwdTilde: dir, samePwd: false, Flags: flags,