Skip to content

Commit

Permalink
Fix execute and paste in raw mode
Browse files Browse the repository at this point in the history
  • Loading branch information
curusarn committed Mar 15, 2023
1 parent 7da16a6 commit 9320e90
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions internal/searchapp/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type ItemColumns struct {
FlagsWithColor string
Flags string

// Shown in TUI
CmdLineWithColor string
CmdLine string

Expand Down Expand Up @@ -334,6 +335,14 @@ func properMatch(str, term, padChar string) bool {
return strings.Contains(padChar+str+padChar, padChar+term+padChar)
}

func trimCmdLine(cmdLine string) string {
return strings.TrimRightFunc(cmdLine, unicode.IsSpace)
}

func replaceNewLines(cmdLine string) string {
return strings.ReplaceAll(cmdLine, "\n", "\\n ")
}

// NewItemFromRecordForQuery creates new item from record based on given query
//
// returns error if the query doesn't match the record
Expand Down Expand Up @@ -363,7 +372,7 @@ func NewItemFromRecordForQuery(record recordint.SearchApp, query Query, debug bo
// nonZeroExitCodeScorePenalty + differentHostScorePenalty

// Trim trailing whitespace before highlighting
trimmedCmdLine := strings.TrimRightFunc(record.CmdLine, unicode.IsSpace)
trimmedCmdLine := trimCmdLine(record.CmdLine)

// KEY for deduplication
key := trimmedCmdLine
Expand All @@ -385,8 +394,8 @@ func NewItemFromRecordForQuery(record recordint.SearchApp, query Query, debug bo
// DISPLAY > cmdline

// cmd := "<" + strings.ReplaceAll(record.CmdLine, "\n", ";") + ">"
cmdLine := strings.ReplaceAll(trimmedCmdLine, "\n", "\\n ")
cmdLineWithColor := strings.ReplaceAll(cmd, "\n", "\\n ")
cmdLine := replaceNewLines(trimmedCmdLine)
cmdLineWithColor := replaceNewLines(cmd)

if record.IsRaw {
return Item{
Expand Down Expand Up @@ -483,7 +492,8 @@ func GetHeader(compactRendering bool) ItemColumns {
type RawItem struct {
CmdLineWithColor string
CmdLine string
CmdLineOut string
// Unchanged cmdline to paste to command line
CmdLineOut string

Score float64

Expand All @@ -501,8 +511,14 @@ func NewRawItemFromRecordForQuery(record recordint.SearchApp, terms []string, de

const timeScoreCoef = 1e-13

// Trim trailing whitespace before highlighting
trimmedCmdLine := strings.TrimRightFunc(record.CmdLine, unicode.IsSpace)

// KEY for deduplication
key := trimmedCmdLine

score := 0.0
cmd := record.CmdLine
cmd := trimmedCmdLine
for _, term := range terms {
c := strings.Count(record.CmdLine, term)
if c > 0 {
Expand All @@ -514,16 +530,14 @@ func NewRawItemFromRecordForQuery(record recordint.SearchApp, terms []string, de
}
}
score += record.Time * timeScoreCoef
// KEY for deduplication
key := record.CmdLine

// DISPLAY > cmdline

// cmd := "<" + strings.ReplaceAll(record.CmdLine, "\n", ";") + ">"
cmdLine := strings.ReplaceAll(record.CmdLine, "\n", ";")
cmdLineWithColor := strings.ReplaceAll(cmd, "\n", ";")
cmdLine := replaceNewLines(trimmedCmdLine)
cmdLineWithColor := replaceNewLines(cmd)

it := RawItem{
CmdLineOut: record.CmdLine,
CmdLine: cmdLine,
CmdLineWithColor: cmdLineWithColor,
Score: score,
Expand Down

0 comments on commit 9320e90

Please sign in to comment.