mirror of
https://github.com/taigrr/teaqlite.git
synced 2026-04-02 04:59:03 -07:00
92 lines
1.8 KiB
Go
92 lines
1.8 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
type EditCellModel struct {
|
|
Shared *SharedData
|
|
rowIndex int
|
|
colIndex int
|
|
value string
|
|
cursor int
|
|
}
|
|
|
|
func NewEditCellModel(shared *SharedData, rowIndex, colIndex int) *EditCellModel {
|
|
value := ""
|
|
if rowIndex < len(shared.FilteredData) && colIndex < len(shared.FilteredData[rowIndex]) {
|
|
value = shared.FilteredData[rowIndex][colIndex]
|
|
}
|
|
|
|
return &EditCellModel{
|
|
Shared: shared,
|
|
rowIndex: rowIndex,
|
|
colIndex: colIndex,
|
|
value: value,
|
|
cursor: len(value),
|
|
}
|
|
}
|
|
|
|
func (m *EditCellModel) Init() tea.Cmd {
|
|
return nil
|
|
}
|
|
|
|
func (m *EditCellModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
switch msg := msg.(type) {
|
|
case tea.KeyMsg:
|
|
switch msg.String() {
|
|
case "esc":
|
|
return m, func() tea.Msg { return SwitchToRowDetailMsg{RowIndex: m.rowIndex} }
|
|
|
|
case "enter":
|
|
return m, func() tea.Msg {
|
|
return UpdateCellMsg{
|
|
RowIndex: m.rowIndex,
|
|
ColIndex: m.colIndex,
|
|
Value: m.value,
|
|
}
|
|
}
|
|
|
|
case "backspace":
|
|
if m.cursor > 0 {
|
|
m.value = m.value[:m.cursor-1] + m.value[m.cursor:]
|
|
m.cursor--
|
|
}
|
|
|
|
case "left":
|
|
if m.cursor > 0 {
|
|
m.cursor--
|
|
}
|
|
|
|
case "right":
|
|
if m.cursor < len(m.value) {
|
|
m.cursor++
|
|
}
|
|
|
|
default:
|
|
if len(msg.String()) == 1 {
|
|
m.value = m.value[:m.cursor] + msg.String() + m.value[m.cursor:]
|
|
m.cursor++
|
|
}
|
|
}
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func (m *EditCellModel) View() string {
|
|
columnName := ""
|
|
if m.colIndex < len(m.Shared.Columns) {
|
|
columnName = m.Shared.Columns[m.colIndex]
|
|
}
|
|
|
|
content := TitleStyle.Render(fmt.Sprintf("Edit Cell: %s", columnName)) + "\n\n"
|
|
content += fmt.Sprintf("Value: %s\n", m.value)
|
|
content += fmt.Sprintf("Cursor: %d\n\n", m.cursor)
|
|
content += HelpStyle.Render("enter: save • esc: cancel")
|
|
|
|
return content
|
|
}
|
|
|