mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-26 18:23:27 +01:00
Add better handling for invalid searches in the TUI + better error messages in a couple other places to improve #40
This commit is contained in:
parent
3985010a17
commit
5249ee6709
@ -1840,6 +1840,22 @@ func TestTui(t *testing.T) {
|
||||
t.Fatalf("hishtory export mismatch (-expected +got):\n%s", diff)
|
||||
}
|
||||
|
||||
// Check the output when the initial search is invalid
|
||||
out = captureTerminalOutput(t, tester, []string{
|
||||
"hishtory SPACE tquery SPACE foo: ENTER",
|
||||
"ls",
|
||||
})
|
||||
out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1])
|
||||
compareGoldens(t, out, "TestTui-InitialInvalidSearch")
|
||||
|
||||
// Check the output when the initial search is invalid
|
||||
out = captureTerminalOutput(t, tester, []string{
|
||||
"hishtory SPACE tquery ENTER",
|
||||
"ls:",
|
||||
})
|
||||
out = strings.TrimSpace(strings.Split(out, "hishtory tquery")[1])
|
||||
compareGoldens(t, out, "TestTui-InvalidSearch")
|
||||
|
||||
// Check the output when the size is adjusted
|
||||
out = captureTerminalOutputWithShellNameAndDimensions(t, tester, tester.ShellName(), 100, 20, []string{
|
||||
"hishtory SPACE tquery ENTER",
|
||||
|
30
client/lib/goldens/TestTui-InitialInvalidSearch
Normal file
30
client/lib/goldens/TestTui-InitialInvalidSearch
Normal file
@ -0,0 +1,30 @@
|
||||
foo:
|
||||
|
||||
|
||||
|
||||
Search Query: > ls
|
||||
|
||||
┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐
|
||||
│ Hostname CWD Timestamp Runtime Exit Code Command │
|
||||
│────────────────────────────────────────────────────────────────────────────────────────────────────────│
|
||||
│ localhost /tmp/ Oct 17 2022 21:43:16 PDT 3s 2 ls ~/ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└────────────────────────────────────────────────────────────────────────────────────────────────────────┘
|
29
client/lib/goldens/TestTui-InvalidSearch
Normal file
29
client/lib/goldens/TestTui-InvalidSearch
Normal file
@ -0,0 +1,29 @@
|
||||
Warning: failed to search: search query contains unknown search atom 'ls' that doesn't match any column names
|
||||
|
||||
|
||||
Search Query: > ls:
|
||||
|
||||
┌────────────────────────────────────────────────────────────────────────────────────────────────────────┐
|
||||
│ Hostname CWD Timestamp Runtime Exit Code Command │
|
||||
│────────────────────────────────────────────────────────────────────────────────────────────────────────│
|
||||
│ localhost /tmp/ Oct 17 2022 21:43:16 PDT 3s 2 ls ~/ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└────────────────────────────────────────────────────────────────────────────────────────────────────────┘
|
@ -495,7 +495,7 @@ func IsEnabled(ctx *context.Context) (bool, error) {
|
||||
func CheckFatalError(err error) {
|
||||
if err != nil {
|
||||
_, filename, line, _ := runtime.Caller(1)
|
||||
log.Fatalf("hishtory fatal error at %s:%d: %v", filename, line, err)
|
||||
log.Fatalf("hishtory v0.%s fatal error at %s:%d: %v", Version, filename, line, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1199,7 +1199,7 @@ func parseAtomizedToken(ctx *context.Context, token string) (string, interface{}
|
||||
}
|
||||
}
|
||||
if !isCustomColumn {
|
||||
return "", nil, nil, fmt.Errorf("search query contains unknown search atom %s", field)
|
||||
return "", nil, nil, fmt.Errorf("search query contains unknown search atom '%s' that doesn't match any column names", field)
|
||||
}
|
||||
// Build the where clause for the custom column
|
||||
return "EXISTS (SELECT 1 FROM json_each(custom_columns) WHERE json_extract(value, '$.name') = ? and instr(json_extract(value, '$.value'), ?) > 0)", field, val, nil
|
||||
|
@ -57,7 +57,7 @@ type model struct {
|
||||
lastQuery string
|
||||
|
||||
// Unrecoverable error.
|
||||
err error
|
||||
fatalErr error
|
||||
// An error while searching. Recoverable and displayed as a warning message.
|
||||
searchErr error
|
||||
// Whether the device is offline. If so, a warning will be displayed.
|
||||
@ -98,17 +98,15 @@ func runQueryAndUpdateTable(m model, updateTable bool) model {
|
||||
m.runQuery = &m.lastQuery
|
||||
}
|
||||
rows, numEntries, err := getRows(m.ctx, hctx.GetConf(m.ctx).DisplayedColumns, *m.runQuery, PADDED_NUM_ENTRIES)
|
||||
if err != nil {
|
||||
m.searchErr = err
|
||||
if err != nil {
|
||||
return m
|
||||
} else {
|
||||
m.searchErr = nil
|
||||
}
|
||||
m.numEntries = numEntries
|
||||
if updateTable {
|
||||
t, err := makeTable(m.ctx, rows)
|
||||
if err != nil {
|
||||
m.err = err
|
||||
m.fatalErr = err
|
||||
return m
|
||||
}
|
||||
m.table = t
|
||||
@ -154,7 +152,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m = runQueryAndUpdateTable(m, true)
|
||||
return m, nil
|
||||
case errMsg:
|
||||
m.err = msg
|
||||
m.fatalErr = msg
|
||||
return m, nil
|
||||
case offlineMsg:
|
||||
m.isOffline = true
|
||||
@ -178,8 +176,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
|
||||
func (m model) View() string {
|
||||
if m.err != nil {
|
||||
return fmt.Sprintf("An unrecoverable error occured: %v\n", m.err)
|
||||
if m.fatalErr != nil {
|
||||
return fmt.Sprintf("An unrecoverable error occured: %v\n", m.fatalErr)
|
||||
}
|
||||
if m.selected {
|
||||
indexOfCommand := -1
|
||||
@ -412,6 +410,11 @@ func TuiQuery(ctx *context.Context, initialQuery string) error {
|
||||
lipgloss.SetColorProfile(termenv.ANSI)
|
||||
rows, numEntries, err := getRows(ctx, hctx.GetConf(ctx).DisplayedColumns, initialQuery, PADDED_NUM_ENTRIES)
|
||||
if err != nil {
|
||||
if initialQuery != "" {
|
||||
// initialQuery is likely invalid in some way, let's just drop it
|
||||
return TuiQuery(ctx, "")
|
||||
}
|
||||
// Something else has gone wrong, crash
|
||||
return err
|
||||
}
|
||||
t, err := makeTable(ctx, rows)
|
||||
|
Loading…
Reference in New Issue
Block a user