fix deduplication shadowing, minor visual improvement

pull/123/head
Simon Let 6 years ago
parent 78d9d51484
commit 14611c87c9
  1. 9
      cmd/cli/highlight.go
  2. 33
      cmd/cli/item.go
  3. 29
      cmd/cli/main.go

@ -77,3 +77,12 @@ func doHighlightString(str string, minLength int) string {
} }
return highlightSelected(str) 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
}

@ -25,7 +25,7 @@ type item struct {
cmdLineWithColor string cmdLineWithColor string
cmdLine string cmdLine string
hits float64 score float64
key string key string
// cmdLineRaw string // cmdLineRaw string
@ -33,7 +33,7 @@ type item struct {
func (i item) less(i2 item) bool { func (i item) less(i2 item) bool {
// reversed order // reversed order
return i.hits > i2.hits return i.score > i2.score
} }
func (i item) produceLine(flagLength int) (string, int) { func (i item) produceLine(flagLength int) (string, int) {
@ -105,7 +105,7 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug
// nonZeroExitCodeScorePenalty + differentHostScorePenalty // nonZeroExitCodeScorePenalty + differentHostScorePenalty
hits := 0.0 score := 0.0
anyHit := false anyHit := false
cmd := record.CmdLine cmd := record.CmdLine
for _, term := range query.terms { for _, term := range query.terms {
@ -113,13 +113,13 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug
if strings.Contains(record.CmdLine, term) { if strings.Contains(record.CmdLine, term) {
anyHit = true anyHit = true
if termHit == false { if termHit == false {
hits += hitScore score += hitScore
} else { } else {
hits += hitScoreConsecutive score += hitScoreConsecutive
} }
termHit = true termHit = true
if properMatch(cmd, term, " ") { if properMatch(cmd, term, " ") {
hits += properMatchScore score += properMatchScore
} }
cmd = strings.ReplaceAll(cmd, term, highlightMatch(term)) cmd = strings.ReplaceAll(cmd, term, highlightMatch(term))
// NO continue // NO continue
@ -138,32 +138,32 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug
if record.Pwd == query.pwd { if record.Pwd == query.pwd {
anyHit = true anyHit = true
samePwd = true samePwd = true
hits += actualPwdScore score += actualPwdScore
} else if sameGitRepo { } else if sameGitRepo {
anyHit = true anyHit = true
hits += sameGitRepoScore score += sameGitRepoScore
} }
differentHost := false differentHost := false
if record.Host != query.host { if record.Host != query.host {
differentHost = true differentHost = true
hits -= differentHostScorePenalty score -= differentHostScorePenalty
} }
errorExitStatus := false errorExitStatus := false
if record.ExitCode != 0 { if record.ExitCode != 0 {
errorExitStatus = true 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") return item{}, errors.New("no match for given record and query")
} }
// KEY for deduplication // KEY for deduplication
unlikelySeparator := "|||||" unlikelySeparator := "|||||"
key := record.CmdLine + unlikelySeparator + record.Pwd + key := record.CmdLine + unlikelySeparator + record.Pwd + unlikelySeparator +
unlikelySeparator + strconv.Itoa(record.ExitCode) + unlikelySeparator +
record.GitOriginRemote + unlikelySeparator + record.Host record.GitOriginRemote + unlikelySeparator + record.Host
// + strconv.Itoa(record.ExitCode) + unlikelySeparator
// DISPLAY // DISPLAY
// DISPLAY > date // DISPLAY > date
@ -176,7 +176,8 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug
location += record.Host + ":" location += record.Host + ":"
locationWithColor += highlightHost(record.Host) + ":" locationWithColor += highlightHost(record.Host) + ":"
} }
const locationLenght = 30 const locationLenght = 35
// const locationLenght = 20 // small screenshots
pwdLength := locationLenght - len(location) pwdLength := locationLenght - len(location)
pwdTilde := strings.Replace(record.Pwd, record.Home, "~", 1) pwdTilde := strings.Replace(record.Pwd, record.Home, "~", 1)
location += leftCutPadString(pwdTilde, pwdLength) location += leftCutPadString(pwdTilde, pwdLength)
@ -190,7 +191,7 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug
flags := "" flags := ""
flagsWithColor := "" flagsWithColor := ""
if debug { if debug {
hitsStr := fmt.Sprintf("%.1f", hits) hitsStr := fmt.Sprintf("%.1f", score)
flags += " S" + hitsStr flags += " S" + hitsStr
} }
if sameGitRepo { if sameGitRepo {
@ -215,7 +216,7 @@ func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug
flagsWithColor: flagsWithColor, flagsWithColor: flagsWithColor,
cmdLine: cmdLine, cmdLine: cmdLine,
cmdLineWithColor: cmdLineWithColor, cmdLineWithColor: cmdLineWithColor,
hits: hits, score: score,
key: key, key: key,
} }
return it, nil return it, nil

@ -92,7 +92,7 @@ func runReshCli() (string, int) {
defer g.Close() defer g.Close()
g.Cursor = true g.Cursor = true
g.SelFgColor = gocui.ColorGreen // g.SelFgColor = gocui.ColorGreen
// g.SelBgColor = gocui.ColorGreen // g.SelBgColor = gocui.ColorGreen
g.Highlight = true g.Highlight = true
@ -202,6 +202,11 @@ func (m manager) SelectPaste(g *gocui.Gui, v *gocui.View) error {
return nil return nil
} }
type dedupRecord struct {
dataIndex int
score float32
}
func (m manager) UpdateData(input string) { func (m manager) UpdateData(input string) {
if debug { if debug {
log.Println("EDIT start") log.Println("EDIT start")
@ -210,7 +215,7 @@ func (m manager) UpdateData(input string) {
} }
query := newQueryFromString(input, m.host, m.pwd, m.gitOriginRemote) query := newQueryFromString(input, m.host, m.pwd, m.gitOriginRemote)
var data []item var data []item
itemSet := make(map[string]bool) itemSet := make(map[string]int)
m.s.lock.Lock() m.s.lock.Lock()
defer m.s.lock.Unlock() defer m.s.lock.Unlock()
for _, rec := range m.s.fullRecords { for _, rec := range m.s.fullRecords {
@ -220,19 +225,25 @@ func (m manager) UpdateData(input string) {
// log.Println(" * continue (no match)", rec.Pwd) // log.Println(" * continue (no match)", rec.Pwd)
continue continue
} }
if itemSet[itm.key] { if idx, ok := itemSet[itm.key]; ok {
// log.Println(" * continue (already present)", itm.key(), itm.pwd) // duplicate found
if data[idx].score >= itm.score {
// skip duplicate item
continue
}
// update duplicate item
data[idx] = itm
continue continue
} }
itemSet[itm.key] = true // add new item
itemSet[itm.key] = len(data)
data = append(data, itm) data = append(data, itm)
// log.Println("DATA =", itm.display)
} }
if debug { if debug {
log.Println("len(tmpdata) =", len(data)) log.Println("len(tmpdata) =", len(data))
} }
sort.SliceStable(data, func(p, q int) bool { 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 m.s.data = nil
for _, itm := range data { for _, itm := range data {
@ -348,9 +359,9 @@ func (m manager) Layout(g *gocui.Gui) error {
v.Editable = true v.Editable = true
v.Editor = m v.Editor = m
if m.s.rawMode { 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 { } else {
v.Title = " RESH CLI - CONTEXTUAL MODE " v.Title = " RESH CLI - CONTEXTUAL MODE - (CTRL+R to switch to RAW MODE) "
} }
g.SetCurrentView("input") g.SetCurrentView("input")

Loading…
Cancel
Save