Fix handling of new lines in commands for #163 (#170)

* Fix handling of new lines in commands for #163

* Move code for table from lib.go to query.go

* Update goldens
This commit is contained in:
David Dworken
2024-02-06 20:04:21 -08:00
committed by GitHub
parent cafc08e1ea
commit 6575c8ae42
4 changed files with 88 additions and 80 deletions

View File

@ -27,9 +27,7 @@ import (
"gorm.io/gorm"
"github.com/araddon/dateparse"
"github.com/fatih/color"
"github.com/google/uuid"
"github.com/rodaine/table"
"github.com/schollz/progressbar/v3"
"github.com/ddworken/hishtory/client/data"
@ -85,7 +83,7 @@ func getCustomColumnValue(ctx context.Context, header string, entry data.History
return "", fmt.Errorf("failed to find a column matching the column name %#v (is there a typo?)", header)
}
func BuildTableRow(ctx context.Context, columnNames []string, entry data.HistoryEntry) ([]string, error) {
func BuildTableRow(ctx context.Context, columnNames []string, entry data.HistoryEntry, commandRenderer func(string) string) ([]string, error) {
row := make([]string, 0)
for _, header := range columnNames {
switch header {
@ -109,7 +107,7 @@ func BuildTableRow(ctx context.Context, columnNames []string, entry data.History
case "Exit Code", "Exit_Code", "ExitCode", "exitcode":
row = append(row, fmt.Sprintf("%d", entry.ExitCode))
case "Command", "command":
row = append(row, entry.Command)
row = append(row, commandRenderer(entry.Command))
case "User", "user":
row = append(row, entry.LocalUsername)
default:
@ -138,53 +136,6 @@ func MakeRegexFromQuery(query string) string {
return r
}
func stringArrayToAnyArray(arr []string) []any {
ret := make([]any, 0)
for _, item := range arr {
ret = append(ret, item)
}
return ret
}
func DisplayResults(ctx context.Context, results []*data.HistoryEntry, numResults int) error {
config := hctx.GetConf(ctx)
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columns := make([]any, 0)
for _, c := range config.DisplayedColumns {
columns = append(columns, c)
}
tbl := table.New(columns...)
tbl.WithHeaderFormatter(headerFmt)
numRows := 0
var seenCommands = make(map[string]bool)
for _, entry := range results {
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
if numRows >= numResults {
break
}
}
tbl.Print()
return nil
}
func CheckFatalError(err error) {
if err != nil {
_, filename, line, _ := runtime.Caller(1)