From 14611c87c915e155e5c42d2a573564e5c55028a6 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Fri, 1 May 2020 12:23:40 +0200 Subject: [PATCH] fix deduplication shadowing, minor visual improvement --- cmd/cli/highlight.go | 9 +++++++++ cmd/cli/item.go | 33 +++++++++++++++++---------------- cmd/cli/main.go | 29 ++++++++++++++++++++--------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/cmd/cli/highlight.go b/cmd/cli/highlight.go index 775d782..eabe539 100644 --- a/cmd/cli/highlight.go +++ b/cmd/cli/highlight.go @@ -77,3 +77,12 @@ func doHighlightString(str string, minLength int) string { } return highlightSelected(str) } + +// EXTRAS + +func highlightModeTitle(str string) string { + // template "\033[3%d;%dm" + greenNormal := "\033[32;1m" + end := "\033[0m" + return greenNormal + cleanHighlight(str) + end +} diff --git a/cmd/cli/item.go b/cmd/cli/item.go index c3e11bb..98a919f 100644 --- a/cmd/cli/item.go +++ b/cmd/cli/item.go @@ -25,7 +25,7 @@ type item struct { cmdLineWithColor string cmdLine string - hits float64 + score float64 key string // cmdLineRaw string @@ -33,7 +33,7 @@ type item struct { func (i item) less(i2 item) bool { // reversed order - return i.hits > i2.hits + return i.score > i2.score } func (i item) produceLine(flagLength int) (string, int) { @@ -105,7 +105,7 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug // nonZeroExitCodeScorePenalty + differentHostScorePenalty - hits := 0.0 + score := 0.0 anyHit := false cmd := record.CmdLine for _, term := range query.terms { @@ -113,13 +113,13 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug if strings.Contains(record.CmdLine, term) { anyHit = true if termHit == false { - hits += hitScore + score += hitScore } else { - hits += hitScoreConsecutive + score += hitScoreConsecutive } termHit = true if properMatch(cmd, term, " ") { - hits += properMatchScore + score += properMatchScore } cmd = strings.ReplaceAll(cmd, term, highlightMatch(term)) // NO continue @@ -138,32 +138,32 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug if record.Pwd == query.pwd { anyHit = true samePwd = true - hits += actualPwdScore + score += actualPwdScore } else if sameGitRepo { anyHit = true - hits += sameGitRepoScore + score += sameGitRepoScore } differentHost := false if record.Host != query.host { differentHost = true - hits -= differentHostScorePenalty + score -= differentHostScorePenalty } errorExitStatus := false if record.ExitCode != 0 { errorExitStatus = true - hits -= nonZeroExitCodeScorePenalty + score -= nonZeroExitCodeScorePenalty } - if hits <= 0 && !anyHit { + if score <= 0 && !anyHit { return item{}, errors.New("no match for given record and query") } // KEY for deduplication unlikelySeparator := "|||||" - key := record.CmdLine + unlikelySeparator + record.Pwd + - unlikelySeparator + strconv.Itoa(record.ExitCode) + unlikelySeparator + + key := record.CmdLine + unlikelySeparator + record.Pwd + unlikelySeparator + record.GitOriginRemote + unlikelySeparator + record.Host + // + strconv.Itoa(record.ExitCode) + unlikelySeparator // DISPLAY // DISPLAY > date @@ -176,7 +176,8 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug location += record.Host + ":" locationWithColor += highlightHost(record.Host) + ":" } - const locationLenght = 30 + const locationLenght = 35 + // const locationLenght = 20 // small screenshots pwdLength := locationLenght - len(location) pwdTilde := strings.Replace(record.Pwd, record.Home, "~", 1) location += leftCutPadString(pwdTilde, pwdLength) @@ -190,7 +191,7 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug flags := "" flagsWithColor := "" if debug { - hitsStr := fmt.Sprintf("%.1f", hits) + hitsStr := fmt.Sprintf("%.1f", score) flags += " S" + hitsStr } if sameGitRepo { @@ -215,7 +216,7 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug flagsWithColor: flagsWithColor, cmdLine: cmdLine, cmdLineWithColor: cmdLineWithColor, - hits: hits, + score: score, key: key, } return it, nil diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 8d789ce..f61b5ad 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -92,7 +92,7 @@ func runReshCli() (string, int) { defer g.Close() g.Cursor = true - g.SelFgColor = gocui.ColorGreen + // g.SelFgColor = gocui.ColorGreen // g.SelBgColor = gocui.ColorGreen g.Highlight = true @@ -202,6 +202,11 @@ func (m manager) SelectPaste(g *gocui.Gui, v *gocui.View) error { return nil } +type dedupRecord struct { + dataIndex int + score float32 +} + func (m manager) UpdateData(input string) { if debug { log.Println("EDIT start") @@ -210,7 +215,7 @@ func (m manager) UpdateData(input string) { } query := newQueryFromString(input, m.host, m.pwd, m.gitOriginRemote) var data []item - itemSet := make(map[string]bool) + itemSet := make(map[string]int) m.s.lock.Lock() defer m.s.lock.Unlock() for _, rec := range m.s.fullRecords { @@ -220,19 +225,25 @@ func (m manager) UpdateData(input string) { // log.Println(" * continue (no match)", rec.Pwd) continue } - if itemSet[itm.key] { - // log.Println(" * continue (already present)", itm.key(), itm.pwd) + if idx, ok := itemSet[itm.key]; ok { + // duplicate found + if data[idx].score >= itm.score { + // skip duplicate item + continue + } + // update duplicate item + data[idx] = itm continue } - itemSet[itm.key] = true + // add new item + itemSet[itm.key] = len(data) data = append(data, itm) - // log.Println("DATA =", itm.display) } if debug { log.Println("len(tmpdata) =", len(data)) } sort.SliceStable(data, func(p, q int) bool { - return data[p].hits > data[q].hits + return data[p].score > data[q].score }) m.s.data = nil for _, itm := range data { @@ -348,9 +359,9 @@ func (m manager) Layout(g *gocui.Gui) error { v.Editable = true v.Editor = m if m.s.rawMode { - v.Title = " RESH CLI - NON-CONTEXTUAL \"RAW\" MODE " + v.Title = " RESH CLI - NON-CONTEXTUAL \"RAW\" MODE - (CTRL+R to switch BACK) " } else { - v.Title = " RESH CLI - CONTEXTUAL MODE " + v.Title = " RESH CLI - CONTEXTUAL MODE - (CTRL+R to switch to RAW MODE) " } g.SetCurrentView("input")