mirror of
https://github.com/taigrr/teaqlite.git
synced 2026-04-02 04:59:03 -07:00
update layout
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"strings"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/bubbles/help"
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
)
|
||||
|
||||
type RowDetailModel struct {
|
||||
@@ -13,14 +15,60 @@ type RowDetailModel struct {
|
||||
selectedCol int
|
||||
FromQuery bool
|
||||
gPressed bool
|
||||
keyMap RowDetailKeyMap
|
||||
help help.Model
|
||||
focused bool
|
||||
id int
|
||||
}
|
||||
|
||||
func NewRowDetailModel(shared *SharedData, rowIndex int) *RowDetailModel {
|
||||
return &RowDetailModel{
|
||||
// RowDetailOption is a functional option for configuring RowDetailModel
|
||||
type RowDetailOption func(*RowDetailModel)
|
||||
|
||||
// WithRowDetailKeyMap sets the key map
|
||||
func WithRowDetailKeyMap(km RowDetailKeyMap) RowDetailOption {
|
||||
return func(m *RowDetailModel) {
|
||||
m.keyMap = km
|
||||
}
|
||||
}
|
||||
|
||||
func NewRowDetailModel(shared *SharedData, rowIndex int, opts ...RowDetailOption) *RowDetailModel {
|
||||
m := &RowDetailModel{
|
||||
Shared: shared,
|
||||
rowIndex: rowIndex,
|
||||
selectedCol: 0,
|
||||
FromQuery: false,
|
||||
keyMap: DefaultRowDetailKeyMap(),
|
||||
help: help.New(),
|
||||
focused: true,
|
||||
id: nextID(),
|
||||
}
|
||||
|
||||
// Apply options
|
||||
for _, opt := range opts {
|
||||
opt(m)
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// ID returns the unique ID of the model
|
||||
func (m RowDetailModel) ID() int {
|
||||
return m.id
|
||||
}
|
||||
|
||||
// Focus sets the focus state
|
||||
func (m *RowDetailModel) Focus() {
|
||||
m.focused = true
|
||||
}
|
||||
|
||||
// Blur removes focus
|
||||
func (m *RowDetailModel) Blur() {
|
||||
m.focused = false
|
||||
}
|
||||
|
||||
// Focused returns the focus state
|
||||
func (m RowDetailModel) Focused() bool {
|
||||
return m.focused
|
||||
}
|
||||
|
||||
func (m *RowDetailModel) Init() tea.Cmd {
|
||||
@@ -28,59 +76,66 @@ func (m *RowDetailModel) Init() tea.Cmd {
|
||||
}
|
||||
|
||||
func (m *RowDetailModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if !m.focused {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "q", "esc":
|
||||
m.gPressed = false
|
||||
if m.FromQuery {
|
||||
return m, func() tea.Msg { return ReturnToQueryMsg{} }
|
||||
}
|
||||
return m, func() tea.Msg { return SwitchToTableDataMsg{TableIndex: m.Shared.SelectedTable} }
|
||||
return m.handleNavigation(msg)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
case "g":
|
||||
if m.gPressed {
|
||||
// Second g - go to beginning
|
||||
m.selectedCol = 0
|
||||
m.gPressed = false
|
||||
} else {
|
||||
// First g - wait for second g
|
||||
m.gPressed = true
|
||||
}
|
||||
return m, nil
|
||||
|
||||
case "G":
|
||||
// Go to end
|
||||
if len(m.Shared.Columns) > 0 {
|
||||
m.selectedCol = len(m.Shared.Columns) - 1
|
||||
}
|
||||
m.gPressed = false
|
||||
return m, nil
|
||||
|
||||
case "e":
|
||||
m.gPressed = false
|
||||
if len(m.Shared.FilteredData) > m.rowIndex && len(m.Shared.Columns) > m.selectedCol {
|
||||
return m, func() tea.Msg {
|
||||
return SwitchToEditCellMsg{RowIndex: m.rowIndex, ColIndex: m.selectedCol}
|
||||
}
|
||||
}
|
||||
|
||||
case "up", "k":
|
||||
m.gPressed = false
|
||||
if m.selectedCol > 0 {
|
||||
m.selectedCol--
|
||||
}
|
||||
|
||||
case "down", "j":
|
||||
m.gPressed = false
|
||||
if m.selectedCol < len(m.Shared.Columns)-1 {
|
||||
m.selectedCol++
|
||||
}
|
||||
|
||||
default:
|
||||
// Any other key resets the g state
|
||||
m.gPressed = false
|
||||
func (m *RowDetailModel) handleNavigation(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
switch {
|
||||
case key.Matches(msg, m.keyMap.Escape), key.Matches(msg, m.keyMap.Back):
|
||||
m.gPressed = false
|
||||
if m.FromQuery {
|
||||
return m, func() tea.Msg { return ReturnToQueryMsg{} }
|
||||
}
|
||||
return m, func() tea.Msg { return SwitchToTableDataMsg{TableIndex: m.Shared.SelectedTable} }
|
||||
|
||||
case key.Matches(msg, m.keyMap.GoToStart):
|
||||
if m.gPressed {
|
||||
// Second g - go to beginning
|
||||
m.selectedCol = 0
|
||||
m.gPressed = false
|
||||
} else {
|
||||
// First g - wait for second g
|
||||
m.gPressed = true
|
||||
}
|
||||
return m, nil
|
||||
|
||||
case key.Matches(msg, m.keyMap.GoToEnd):
|
||||
// Go to end
|
||||
if len(m.Shared.Columns) > 0 {
|
||||
m.selectedCol = len(m.Shared.Columns) - 1
|
||||
}
|
||||
m.gPressed = false
|
||||
return m, nil
|
||||
|
||||
case key.Matches(msg, m.keyMap.Enter):
|
||||
m.gPressed = false
|
||||
return m, func() tea.Msg {
|
||||
return SwitchToEditCellMsg{RowIndex: m.rowIndex, ColIndex: m.selectedCol}
|
||||
}
|
||||
|
||||
case key.Matches(msg, m.keyMap.Up):
|
||||
m.gPressed = false
|
||||
if m.selectedCol > 0 {
|
||||
m.selectedCol--
|
||||
}
|
||||
|
||||
case key.Matches(msg, m.keyMap.Down):
|
||||
m.gPressed = false
|
||||
if m.selectedCol < len(m.Shared.Columns)-1 {
|
||||
m.selectedCol++
|
||||
}
|
||||
|
||||
default:
|
||||
// Any other key resets the g state
|
||||
m.gPressed = false
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
@@ -92,27 +147,36 @@ func (m *RowDetailModel) View() string {
|
||||
content.WriteString("\n\n")
|
||||
|
||||
if m.rowIndex >= len(m.Shared.FilteredData) {
|
||||
content.WriteString("Invalid row index")
|
||||
content.WriteString("Row not found")
|
||||
return content.String()
|
||||
}
|
||||
|
||||
// Show current row position
|
||||
content.WriteString(fmt.Sprintf("Row %d of %d\n\n", m.rowIndex+1, len(m.Shared.FilteredData)))
|
||||
|
||||
row := m.Shared.FilteredData[m.rowIndex]
|
||||
|
||||
// Show each column and its value
|
||||
for i, col := range m.Shared.Columns {
|
||||
if i < len(row) {
|
||||
if i == m.selectedCol {
|
||||
content.WriteString(SelectedStyle.Render(fmt.Sprintf("> %s: %s", col, row[i])))
|
||||
} else {
|
||||
content.WriteString(NormalStyle.Render(fmt.Sprintf(" %s: %s", col, row[i])))
|
||||
}
|
||||
content.WriteString("\n")
|
||||
if i >= len(row) {
|
||||
break
|
||||
}
|
||||
|
||||
value := row[i]
|
||||
if len(value) > 50 {
|
||||
// Wrap long values
|
||||
lines := WrapText(value, 50)
|
||||
value = strings.Join(lines, "\n ")
|
||||
}
|
||||
|
||||
line := fmt.Sprintf("%s: %s", col, value)
|
||||
if i == m.selectedCol {
|
||||
content.WriteString(SelectedStyle.Render("> " + line))
|
||||
} else {
|
||||
content.WriteString(NormalStyle.Render(" " + line))
|
||||
}
|
||||
content.WriteString("\n")
|
||||
}
|
||||
|
||||
content.WriteString("\n")
|
||||
content.WriteString(HelpStyle.Render("↑/↓: navigate columns • e: edit • gg/G: first/last • q: back"))
|
||||
content.WriteString(m.help.View(m.keyMap))
|
||||
|
||||
return content.String()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user