mirror of
https://github.com/ddworken/hishtory.git
synced 2025-08-10 07:27:57 +02:00
Filter duplicates with map (#139)
The previous method of filtering duplicates only worked on duplicates that occurred consecutively. Since dupes happen out of order often, this switches the logic to instead use a map of seen commands and filter based on that.
This commit is contained in:
@ -157,19 +157,25 @@ func DisplayResults(ctx context.Context, results []*data.HistoryEntry, numResult
|
||||
tbl := table.New(columns...)
|
||||
tbl.WithHeaderFormatter(headerFmt)
|
||||
|
||||
lastCommand := ""
|
||||
numRows := 0
|
||||
|
||||
var seenCommands = make(map[string]bool)
|
||||
|
||||
for _, entry := range results {
|
||||
if entry != nil && strings.TrimSpace(entry.Command) == strings.TrimSpace(lastCommand) && config.FilterDuplicateCommands {
|
||||
continue
|
||||
if config.FilterDuplicateCommands && entry != nil {
|
||||
cmd := strings.TrimSpace(entry.Command)
|
||||
if seenCommands[cmd] {
|
||||
continue
|
||||
}
|
||||
seenCommands[cmd] = true
|
||||
}
|
||||
|
||||
row, err := BuildTableRow(ctx, config.DisplayedColumns, *entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tbl.AddRow(stringArrayToAnyArray(row)...)
|
||||
numRows += 1
|
||||
lastCommand = entry.Command
|
||||
if numRows >= numResults {
|
||||
break
|
||||
}
|
||||
|
Reference in New Issue
Block a user