mirror of
https://github.com/ddworken/hishtory.git
synced 2025-03-29 00:57:12 +01:00
* 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:
parent
0ba7725607
commit
82f819d369
@ -7,10 +7,13 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ddworken/hishtory/client/data"
|
||||||
"github.com/ddworken/hishtory/client/hctx"
|
"github.com/ddworken/hishtory/client/hctx"
|
||||||
"github.com/ddworken/hishtory/client/lib"
|
"github.com/ddworken/hishtory/client/lib"
|
||||||
"github.com/ddworken/hishtory/client/tui"
|
"github.com/ddworken/hishtory/client/tui"
|
||||||
|
"github.com/fatih/color"
|
||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
|
"github.com/rodaine/table"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -148,7 +151,54 @@ func query(ctx context.Context, query string) {
|
|||||||
numResults := 25
|
numResults := 25
|
||||||
data, err := lib.Search(ctx, db, query, numResults*5)
|
data, err := lib.Search(ctx, db, query, numResults*5)
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
lib.CheckFatalError(lib.DisplayResults(ctx, data, numResults))
|
lib.CheckFatalError(DisplayResults(ctx, data, numResults))
|
||||||
|
}
|
||||||
|
|
||||||
|
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 := lib.BuildTableRow(ctx, config.DisplayedColumns, *entry, func(s string) string { return s })
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tbl.AddRow(stringArrayToAnyArray(row)...)
|
||||||
|
numRows += 1
|
||||||
|
if numRows >= numResults {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tbl.Print()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringArrayToAnyArray(arr []string) []any {
|
||||||
|
ret := make([]any, 0)
|
||||||
|
for _, item := range arr {
|
||||||
|
ret = append(ret, item)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func displayBannerIfSet(ctx context.Context) error {
|
func displayBannerIfSet(ctx context.Context) error {
|
||||||
|
@ -27,9 +27,7 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
"github.com/fatih/color"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/rodaine/table"
|
|
||||||
"github.com/schollz/progressbar/v3"
|
"github.com/schollz/progressbar/v3"
|
||||||
|
|
||||||
"github.com/ddworken/hishtory/client/data"
|
"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)
|
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)
|
row := make([]string, 0)
|
||||||
for _, header := range columnNames {
|
for _, header := range columnNames {
|
||||||
switch header {
|
switch header {
|
||||||
@ -109,7 +107,7 @@ func BuildTableRow(ctx context.Context, columnNames []string, entry data.History
|
|||||||
case "Exit Code", "Exit_Code", "ExitCode", "exitcode":
|
case "Exit Code", "Exit_Code", "ExitCode", "exitcode":
|
||||||
row = append(row, fmt.Sprintf("%d", entry.ExitCode))
|
row = append(row, fmt.Sprintf("%d", entry.ExitCode))
|
||||||
case "Command", "command":
|
case "Command", "command":
|
||||||
row = append(row, entry.Command)
|
row = append(row, commandRenderer(entry.Command))
|
||||||
case "User", "user":
|
case "User", "user":
|
||||||
row = append(row, entry.LocalUsername)
|
row = append(row, entry.LocalUsername)
|
||||||
default:
|
default:
|
||||||
@ -138,53 +136,6 @@ func MakeRegexFromQuery(query string) string {
|
|||||||
return r
|
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) {
|
func CheckFatalError(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, filename, line, _ := runtime.Caller(1)
|
_, filename, line, _ := runtime.Caller(1)
|
||||||
|
@ -1,27 +1,27 @@
|
|||||||
Search Query: > Slah
|
Search Query: > Slah
|
||||||
|
|
||||||
┌─────────────────────────────────────────────────────────────────────────────┐
|
┌───────────────────────────────────────────────────────────────────────────────┐
|
||||||
│ Hostname Exit Code Command foo │
|
│ Hostname Exit Code Command foo │
|
||||||
│─────────────────────────────────────────────────────────────────────────────│
|
│───────────────────────────────────────────────────────────────────────────────│
|
||||||
│ ghaction-runner-hostname 0 ls \\n-Slah \\n/ foo │
|
│ ghaction-runner-hostname 0 "ls \\\n-Slah \\\n/" foo │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
│ │
|
│ │
|
||||||
└─────────────────────────────────────────────────────────────────────────────┘
|
└───────────────────────────────────────────────────────────────────────────────┘
|
||||||
hiSHtory: Search your shell history • ctrl+h help
|
hiSHtory: Search your shell history • ctrl+h help
|
@ -515,7 +515,7 @@ func getRowsFromAiSuggestions(ctx context.Context, columnNames []string, query s
|
|||||||
EntryId: "OpenAI",
|
EntryId: "OpenAI",
|
||||||
}
|
}
|
||||||
entries = append(entries, &entry)
|
entries = append(entries, &entry)
|
||||||
row, err := lib.BuildTableRow(ctx, columnNames, entry)
|
row, err := lib.BuildTableRow(ctx, columnNames, entry, func(s string) string { return s })
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("failed to build row for entry=%#v: %w", entry, err)
|
return nil, nil, fmt.Errorf("failed to build row for entry=%#v: %w", entry, err)
|
||||||
}
|
}
|
||||||
@ -551,8 +551,7 @@ func getRows(ctx context.Context, columnNames []string, defaultFilter, query str
|
|||||||
seenCommands[cmd] = true
|
seenCommands[cmd] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
entry.Command = strings.ReplaceAll(entry.Command, "\n", "\\n")
|
row, err := lib.BuildTableRow(ctx, columnNames, *entry, commandEscaper)
|
||||||
row, err := lib.BuildTableRow(ctx, columnNames, *entry)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("failed to build row for entry=%#v: %w", entry, err)
|
return nil, nil, fmt.Errorf("failed to build row for entry=%#v: %w", entry, err)
|
||||||
}
|
}
|
||||||
@ -565,6 +564,14 @@ func getRows(ctx context.Context, columnNames []string, defaultFilter, query str
|
|||||||
return rows, filteredData, nil
|
return rows, filteredData, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func commandEscaper(cmd string) string {
|
||||||
|
if !strings.Contains(cmd, "\n") {
|
||||||
|
// No special escaping necessary
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%#v", cmd)
|
||||||
|
}
|
||||||
|
|
||||||
func calculateColumnWidths(rows []table.Row, numColumns int) []int {
|
func calculateColumnWidths(rows []table.Row, numColumns int) []int {
|
||||||
neededColumnWidth := make([]int, numColumns)
|
neededColumnWidth := make([]int, numColumns)
|
||||||
for _, row := range rows {
|
for _, row := range rows {
|
||||||
@ -935,7 +942,7 @@ func TuiQuery(ctx context.Context, initialQuery string) error {
|
|||||||
// Print out the initialQuery instead so that we don't clear the terminal
|
// Print out the initialQuery instead so that we don't clear the terminal
|
||||||
SELECTED_COMMAND = initialQuery
|
SELECTED_COMMAND = initialQuery
|
||||||
}
|
}
|
||||||
fmt.Printf("%s\n", strings.ReplaceAll(SELECTED_COMMAND, "\\n", "\n"))
|
fmt.Printf("%s\n", SELECTED_COMMAND)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user