mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-29 11:44:53 +01: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:
parent
be3ad76e68
commit
c8643d5a2b
@ -157,19 +157,25 @@ func DisplayResults(ctx context.Context, results []*data.HistoryEntry, numResult
|
|||||||
tbl := table.New(columns...)
|
tbl := table.New(columns...)
|
||||||
tbl.WithHeaderFormatter(headerFmt)
|
tbl.WithHeaderFormatter(headerFmt)
|
||||||
|
|
||||||
lastCommand := ""
|
|
||||||
numRows := 0
|
numRows := 0
|
||||||
|
|
||||||
|
var seenCommands = make(map[string]bool)
|
||||||
|
|
||||||
for _, entry := range results {
|
for _, entry := range results {
|
||||||
if entry != nil && strings.TrimSpace(entry.Command) == strings.TrimSpace(lastCommand) && config.FilterDuplicateCommands {
|
if config.FilterDuplicateCommands && entry != nil {
|
||||||
continue
|
cmd := strings.TrimSpace(entry.Command)
|
||||||
|
if seenCommands[cmd] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seenCommands[cmd] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
row, err := BuildTableRow(ctx, config.DisplayedColumns, *entry)
|
row, err := BuildTableRow(ctx, config.DisplayedColumns, *entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tbl.AddRow(stringArrayToAnyArray(row)...)
|
tbl.AddRow(stringArrayToAnyArray(row)...)
|
||||||
numRows += 1
|
numRows += 1
|
||||||
lastCommand = entry.Command
|
|
||||||
if numRows >= numResults {
|
if numRows >= numResults {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -498,13 +498,20 @@ func getRows(ctx context.Context, columnNames []string, query string, numEntries
|
|||||||
}
|
}
|
||||||
var rows []table.Row
|
var rows []table.Row
|
||||||
var filteredData []*data.HistoryEntry
|
var filteredData []*data.HistoryEntry
|
||||||
lastCommand := ""
|
var seenCommands = make(map[string]bool)
|
||||||
|
|
||||||
for i := 0; i < numEntries; i++ {
|
for i := 0; i < numEntries; i++ {
|
||||||
if i < len(searchResults) {
|
if i < len(searchResults) {
|
||||||
entry := searchResults[i]
|
entry := searchResults[i]
|
||||||
if 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
|
||||||
}
|
}
|
||||||
|
|
||||||
entry.Command = strings.ReplaceAll(entry.Command, "\n", "\\n")
|
entry.Command = strings.ReplaceAll(entry.Command, "\n", "\\n")
|
||||||
row, err := lib.BuildTableRow(ctx, columnNames, *entry)
|
row, err := lib.BuildTableRow(ctx, columnNames, *entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -512,7 +519,6 @@ func getRows(ctx context.Context, columnNames []string, query string, numEntries
|
|||||||
}
|
}
|
||||||
rows = append(rows, row)
|
rows = append(rows, row)
|
||||||
filteredData = append(filteredData, entry)
|
filteredData = append(filteredData, entry)
|
||||||
lastCommand = entry.Command
|
|
||||||
} else {
|
} else {
|
||||||
rows = append(rows, table.Row{})
|
rows = append(rows, table.Row{})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user