Skip to content

Commit

Permalink
Fix handling of new lines in commands for #163
Browse files Browse the repository at this point in the history
  • Loading branch information
ddworken committed Feb 5, 2024
1 parent 08598f4 commit fed8b10
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
6 changes: 3 additions & 3 deletions client/lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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 {
Expand All @@ -109,7 +109,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:
Expand Down Expand Up @@ -170,7 +170,7 @@ func DisplayResults(ctx context.Context, results []*data.HistoryEntry, numResult
seenCommands[cmd] = true
}

row, err := BuildTableRow(ctx, config.DisplayedColumns, *entry)
row, err := BuildTableRow(ctx, config.DisplayedColumns, *entry, func(s string) string { return s })
if err != nil {
return err
}
Expand Down
15 changes: 11 additions & 4 deletions client/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func getRowsFromAiSuggestions(ctx context.Context, columnNames []string, query s
EntryId: "OpenAI",
}
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 {
return nil, nil, fmt.Errorf("failed to build row for entry=%#v: %w", entry, err)
}
Expand Down Expand Up @@ -550,8 +550,7 @@ func getRows(ctx context.Context, columnNames []string, defaultFilter, query str
seenCommands[cmd] = true
}

entry.Command = strings.ReplaceAll(entry.Command, "\n", "\\n")
row, err := lib.BuildTableRow(ctx, columnNames, *entry)
row, err := lib.BuildTableRow(ctx, columnNames, *entry, commandEscaper)
if err != nil {
return nil, nil, fmt.Errorf("failed to build row for entry=%#v: %w", entry, err)
}
Expand All @@ -564,6 +563,14 @@ func getRows(ctx context.Context, columnNames []string, defaultFilter, query str
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 {
neededColumnWidth := make([]int, numColumns)
for _, row := range rows {
Expand Down Expand Up @@ -934,7 +941,7 @@ func TuiQuery(ctx context.Context, initialQuery string) error {
// Print out the initialQuery instead so that we don't clear the terminal
SELECTED_COMMAND = initialQuery
}
fmt.Printf("%s\n", strings.ReplaceAll(SELECTED_COMMAND, "\\n", "\n"))
fmt.Printf("%s\n", SELECTED_COMMAND)
return nil
}

Expand Down

0 comments on commit fed8b10

Please sign in to comment.