diff --git a/internal/searchapp/item.go b/internal/searchapp/item.go index 33fc827..df43678 100644 --- a/internal/searchapp/item.go +++ b/internal/searchapp/item.go @@ -234,7 +234,14 @@ func produceLocation(length int, host string, pwdTilde string, differentHost boo // pwd length is the rest of the length newPwdLen := length - colonLen - newHostLen - hostWithColor := rightCutPadString(host, newHostLen) + // adjust pwd length + if newPwdLen > pwdLen { + diff := newPwdLen - pwdLen + newHostLen += diff + newPwdLen -= diff + } + + hostWithColor := rightCutLeftPadString(host, newHostLen) if differentHost { hostWithColor = highlightHost(hostWithColor) } @@ -275,6 +282,16 @@ func (ic ItemColumns) ProduceLine(dateLength int, locationLength int, flagsLengt return line, length, err } +func rightCutLeftPadString(str string, newLen int) string { + if len(str) > newLen { + return rightCutPadString(str, newLen) + } + if len(str) < newLen { + return leftCutPadString(str, newLen) + } + return str +} + func leftCutPadString(str string, newLen int) string { if newLen <= 0 { return "" @@ -304,24 +321,22 @@ func rightCutPadString(str string, newLen int) string { } // proper match for path is when whole directory is matched -// proper match for command is when term matches word delimeted by whitespace +// proper match for command is when term matches word delimited by whitespace func properMatch(str, term, padChar string) bool { - if strings.Contains(padChar+str+padChar, padChar+term+padChar) { - return true - } - return false + return strings.Contains(padChar+str+padChar, padChar+term+padChar) } // NewItemFromRecordForQuery creates new item from record based on given query -// returns error if the query doesn't match the record +// +// returns error if the query doesn't match the record func NewItemFromRecordForQuery(record recordint.SearchApp, query Query, debug bool) (Item, error) { // Use numbers that won't add up to same score for any number of query words - // query score weigth 1.51 + // query score weight 1.51 const hitScore = 1.517 // 1 * 1.51 const properMatchScore = 0.501 // 0.33 * 1.51 const hitScoreConsecutive = 0.00302 // 0.002 * 1.51 - // context score weigth 1 + // context score weight 1 // Host penalty var actualPwdScore = 0.9 var sameGitRepoScore = 0.8 @@ -469,7 +484,8 @@ type RawItem struct { } // NewRawItemFromRecordForQuery creates new item from record based on given query -// returns error if the query doesn't match the record +// +// returns error if the query doesn't match the record func NewRawItemFromRecordForQuery(record recordint.SearchApp, terms []string, debug bool) (RawItem, error) { const hitScore = 1.0 const hitScoreConsecutive = 0.01 diff --git a/internal/searchapp/item_test.go b/internal/searchapp/item_test.go index 903157a..38caad0 100644 --- a/internal/searchapp/item_test.go +++ b/internal/searchapp/item_test.go @@ -87,3 +87,45 @@ func TestRightCutPadString(t *testing.T) { t.Fatal("Incorrect right pad from ♥♥♥♥ to '♥♥♥♥ '") } } + +// TestRightCutLeftPadString +func TestRightCutLeftPadString(t *testing.T) { + if rightCutLeftPadString("abc", -1) != "" { + t.Fatal("Incorrect right cut from abc to '' (negative)") + } + if rightCutLeftPadString("abc", 0) != "" { + t.Fatal("Incorrect right cut from abc to ''") + } + if rightCutLeftPadString("abc", 1) != "…" { + t.Fatal("Incorrect right cut from abc to …") + } + if rightCutLeftPadString("abc", 2) != "a…" { + t.Fatal("Incorrect right cut from abc to a…") + } + if rightCutLeftPadString("abc", 3) != "abc" { + t.Fatal("Incorrect right cut from abc to abc") + } + if rightCutLeftPadString("abc", 5) != " abc" { + t.Fatal("Incorrect right pad from abc to ' abc'") + } + + // unicode + if rightCutLeftPadString("♥♥♥♥", -1) != "" { + t.Fatal("Incorrect right cut from ♥♥♥♥ to '' (negative)") + } + if rightCutLeftPadString("♥♥♥♥", 0) != "" { + t.Fatal("Incorrect right cut from ♥♥♥♥ to ''") + } + if rightCutLeftPadString("♥♥♥♥", 1) != "…" { + t.Fatal("Incorrect right cut from ♥♥♥♥ to …") + } + if rightCutLeftPadString("♥♥♥♥", 2) != "♥…" { + t.Fatal("Incorrect right cut from ♥♥♥♥ to ♥…") + } + if rightCutLeftPadString("♥♥♥♥", 4) != "♥♥♥♥" { + t.Fatal("Incorrect right cut from ♥♥♥♥ to ♥♥♥♥") + } + if rightCutLeftPadString("♥♥♥♥", 6) != " ♥♥♥♥" { + t.Fatal("Incorrect right pad from ♥♥♥♥ to ' ♥♥♥♥'") + } +}