(command, description, cwd string, timeout time.Duration)
| 1495 | bestScore = score |
| 1496 | bestIdx = i |
| 1497 | } |
| 1498 | } |
| 1499 | if bestScore < 0.3 { |
| 1500 | return fmt.Sprintf(" No similar lines found. Searched for: '%s'", truncateForMsg(oldFirst, 100)) |
| 1501 | } |
| 1502 | start := max(0, bestIdx-2) |
| 1503 | end := min(len(lines), bestIdx+2) |
| 1504 | out := []string{fmt.Sprintf(" Closest match at line %d (%.0f%% similarity):", bestIdx+1, bestScore*100)} |
| 1505 | for i := start; i < end; i++ { |
| 1506 | marker := " " |
| 1507 | if i == bestIdx { |
| 1508 | marker = ">>>" |
| 1509 | } |
| 1510 | out = append(out, fmt.Sprintf(" %s L%4d %s", marker, i+1, truncateForMsg(lines[i], 120))) |
| 1511 | } |
| 1512 | return strings.Join(out, "\n") |
| 1513 | } |
| 1514 | |
| 1515 | func lineSimilarity(a, b string) float64 { |
| 1516 | if a == b { |
| 1517 | return 1 |
| 1518 | } |
| 1519 | aTokens := strings.Fields(a) |
| 1520 | if len(aTokens) == 0 { |
| 1521 | return 0 |
| 1522 | } |
| 1523 | bSet := map[string]struct{}{} |
| 1524 | for _, tok := range strings.Fields(b) { |
| 1525 | bSet[tok] = struct{}{} |
| 1526 | } |
| 1527 | intersect := 0 |
| 1528 | for _, tok := range aTokens { |
| 1529 | if _, ok := bSet[tok]; ok { |
| 1530 | intersect++ |
| 1531 | } |
| 1532 | } |
no test coverage detected