From 76bcc8520c31cc24529388d83165a8db6f9e12f0 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sun, 13 Jul 2025 17:50:56 -0700 Subject: [PATCH] cell editing works better --- internal/app/app.go | 17 +++++++++++++- internal/app/edit_cell.go | 4 ++-- internal/app/query.go | 48 +++++++++++++++++++++++--------------- internal/app/table_data.go | 11 ++++++++- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 4d4d8de..3dd8536 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -29,7 +29,12 @@ type ( RowIndex, ColIndex int Value string } - ExecuteQueryMsg struct{ Query string } + ExecuteQueryMsg struct{ Query string } + QueryCompletedMsg struct { + Results [][]string + Columns []string + Error error + } ) // Model is the main application model @@ -506,6 +511,9 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if msg.String() == "ctrl+c" { return m, tea.Quit } + if msg.String() == "ctrl+z" { + return m, tea.Suspend + } case SwitchToTableListMsg: m.currentView = NewTableListModel(m.getSharedData()) @@ -563,6 +571,13 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.err = err } return m, func() tea.Msg { return SwitchToRowDetailMsg{msg.RowIndex} } + + case QueryCompletedMsg: + // Forward the query completion to the query model + if queryModel, ok := m.currentView.(*QueryModel); ok { + queryModel.handleQueryCompletion(msg) + } + return m, nil } if m.err != nil { diff --git a/internal/app/edit_cell.go b/internal/app/edit_cell.go index e55c970..2ee3624 100644 --- a/internal/app/edit_cell.go +++ b/internal/app/edit_cell.go @@ -82,14 +82,14 @@ func (m *EditCellModel) View() string { } content := TitleStyle.Render(fmt.Sprintf("Edit Cell: %s", columnName)) + "\n\n" - + // Display value with visible cursor displayValue := m.value if m.cursor <= len(displayValue) { // Insert cursor character at cursor position displayValue = displayValue[:m.cursor] + "_" + displayValue[m.cursor:] } - + content += fmt.Sprintf("Value: %s\n\n", displayValue) content += HelpStyle.Render("enter: save • esc: cancel") diff --git a/internal/app/query.go b/internal/app/query.go index 0d55407..cf26fd7 100644 --- a/internal/app/query.go +++ b/internal/app/query.go @@ -221,21 +221,18 @@ func (m *QueryModel) executeQuery() tea.Cmd { rows, err := m.Shared.DB.Query(modifiedQuery) if err != nil { - m.err = err - return nil + return QueryCompletedMsg{Error: err} } defer rows.Close() // Get column names columns, err := rows.Columns() if err != nil { - m.err = err - return nil + return QueryCompletedMsg{Error: err} } - m.columns = columns // Get results - m.results = [][]string{} + var results [][]string for rows.Next() { values := make([]any, len(columns)) valuePtrs := make([]any, len(columns)) @@ -244,8 +241,7 @@ func (m *QueryModel) executeQuery() tea.Cmd { } if err := rows.Scan(valuePtrs...); err != nil { - m.err = err - return nil + return QueryCompletedMsg{Error: err} } row := make([]string, len(columns)) @@ -256,22 +252,36 @@ func (m *QueryModel) executeQuery() tea.Cmd { row[i] = fmt.Sprintf("%v", val) } } - m.results = append(m.results, row) + results = append(results, row) } - // Update shared data for row detail view - m.Shared.FilteredData = m.results - m.Shared.Columns = m.columns - m.Shared.IsQueryResult = true - - m.FocusOnInput = false - m.selectedRow = 0 - m.err = nil - - return nil + return QueryCompletedMsg{ + Results: results, + Columns: columns, + Error: nil, + } } } +func (m *QueryModel) handleQueryCompletion(msg QueryCompletedMsg) { + if msg.Error != nil { + m.err = msg.Error + return + } + + m.results = msg.Results + m.columns = msg.Columns + + // Update shared data for row detail view + m.Shared.FilteredData = m.results + m.Shared.Columns = m.columns + m.Shared.IsQueryResult = true + + m.FocusOnInput = false + m.selectedRow = 0 + m.err = nil +} + func (m *QueryModel) View() string { var content strings.Builder diff --git a/internal/app/table_data.go b/internal/app/table_data.go index 163a630..d448c1b 100644 --- a/internal/app/table_data.go +++ b/internal/app/table_data.go @@ -57,7 +57,16 @@ func (m *TableDataModel) handleSearchInput(msg tea.KeyMsg) (tea.Model, tea.Cmd) func (m *TableDataModel) handleNavigation(msg tea.KeyMsg) (tea.Model, tea.Cmd) { switch msg.String() { - case "q", "esc": + case "q": + return m, func() tea.Msg { return SwitchToTableListMsg{} } + + case "esc": + if m.searchInput != "" { + // Clear search filter + m.searchInput = "" + m.filterData() + return m, nil + } return m, func() tea.Msg { return SwitchToTableListMsg{} } case "enter":