Skip to content

Commit 9320e90

Browse files
committed
Fix execute and paste in raw mode
1 parent 7da16a6 commit 9320e90

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

internal/searchapp/item.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type ItemColumns struct {
6161
FlagsWithColor string
6262
Flags string
6363

64+
// Shown in TUI
6465
CmdLineWithColor string
6566
CmdLine string
6667

@@ -334,6 +335,14 @@ func properMatch(str, term, padChar string) bool {
334335
return strings.Contains(padChar+str+padChar, padChar+term+padChar)
335336
}
336337

338+
func trimCmdLine(cmdLine string) string {
339+
return strings.TrimRightFunc(cmdLine, unicode.IsSpace)
340+
}
341+
342+
func replaceNewLines(cmdLine string) string {
343+
return strings.ReplaceAll(cmdLine, "\n", "\\n ")
344+
}
345+
337346
// NewItemFromRecordForQuery creates new item from record based on given query
338347
//
339348
// returns error if the query doesn't match the record
@@ -363,7 +372,7 @@ func NewItemFromRecordForQuery(record recordint.SearchApp, query Query, debug bo
363372
// nonZeroExitCodeScorePenalty + differentHostScorePenalty
364373

365374
// Trim trailing whitespace before highlighting
366-
trimmedCmdLine := strings.TrimRightFunc(record.CmdLine, unicode.IsSpace)
375+
trimmedCmdLine := trimCmdLine(record.CmdLine)
367376

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

387396
// cmd := "<" + strings.ReplaceAll(record.CmdLine, "\n", ";") + ">"
388-
cmdLine := strings.ReplaceAll(trimmedCmdLine, "\n", "\\n ")
389-
cmdLineWithColor := strings.ReplaceAll(cmd, "\n", "\\n ")
397+
cmdLine := replaceNewLines(trimmedCmdLine)
398+
cmdLineWithColor := replaceNewLines(cmd)
390399

391400
if record.IsRaw {
392401
return Item{
@@ -483,7 +492,8 @@ func GetHeader(compactRendering bool) ItemColumns {
483492
type RawItem struct {
484493
CmdLineWithColor string
485494
CmdLine string
486-
CmdLineOut string
495+
// Unchanged cmdline to paste to command line
496+
CmdLineOut string
487497

488498
Score float64
489499

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

502512
const timeScoreCoef = 1e-13
503513

514+
// Trim trailing whitespace before highlighting
515+
trimmedCmdLine := strings.TrimRightFunc(record.CmdLine, unicode.IsSpace)
516+
517+
// KEY for deduplication
518+
key := trimmedCmdLine
519+
504520
score := 0.0
505-
cmd := record.CmdLine
521+
cmd := trimmedCmdLine
506522
for _, term := range terms {
507523
c := strings.Count(record.CmdLine, term)
508524
if c > 0 {
@@ -514,16 +530,14 @@ func NewRawItemFromRecordForQuery(record recordint.SearchApp, terms []string, de
514530
}
515531
}
516532
score += record.Time * timeScoreCoef
517-
// KEY for deduplication
518-
key := record.CmdLine
519-
520533
// DISPLAY > cmdline
521534

522535
// cmd := "<" + strings.ReplaceAll(record.CmdLine, "\n", ";") + ">"
523-
cmdLine := strings.ReplaceAll(record.CmdLine, "\n", ";")
524-
cmdLineWithColor := strings.ReplaceAll(cmd, "\n", ";")
536+
cmdLine := replaceNewLines(trimmedCmdLine)
537+
cmdLineWithColor := replaceNewLines(cmd)
525538

526539
it := RawItem{
540+
CmdLineOut: record.CmdLine,
527541
CmdLine: cmdLine,
528542
CmdLineWithColor: cmdLineWithColor,
529543
Score: score,

0 commit comments

Comments
 (0)