Skip to content

Commit

Permalink
Redo how HOST and PWD are displayed
Browse files Browse the repository at this point in the history
Fix regression introduced earlier
  • Loading branch information
curusarn committed Dec 11, 2021
1 parent 3d18e16 commit a88aced
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 31 deletions.
23 changes: 14 additions & 9 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sort"
"strings"
"sync"
"time"

"github.com/BurntSushi/toml"
"github.com/awesome-gocui/gocui"
Expand Down Expand Up @@ -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")

Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
77 changes: 55 additions & 22 deletions pkg/searchapp/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package searchapp
import (
"fmt"
"log"
"math"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -46,7 +47,7 @@ type ItemColumns struct {
Date string

// [host:]pwd
HostWithColor string
differentHost bool
Host string
PwdTilde string
samePwd bool
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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"
Expand All @@ -413,7 +447,6 @@ func GetHeader(compactRendering bool) ItemColumns {
Date: date,
DateWithColor: date,
Host: host,
HostWithColor: host,
PwdTilde: dir,
samePwd: false,
Flags: flags,
Expand Down

0 comments on commit a88aced

Please sign in to comment.