Skip to content

Commit

Permalink
feat: add set value menu to the sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerojas26 committed Oct 12, 2024
1 parent 517685d commit cabef15
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/Keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ var Keymaps = KeymapSystem{
Bind{Key: Key{Char: 'c'}, Cmd: cmd.Edit, Description: "Edit field"},
Bind{Key: Key{Code: tcell.KeyEnter}, Cmd: cmd.CommitEdit, Description: "Add edit to pending changes"},
Bind{Key: Key{Code: tcell.KeyEscape}, Cmd: cmd.DiscardEdit, Description: "Discard edit"},
Bind{Key: Key{Char: 'C'}, Cmd: cmd.SetValue, Description: "Toggle value menu to put values like NULL, EMPTY or DEFAULT"},
},
},
}
4 changes: 2 additions & 2 deletions components/ResultsTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver

pagination := NewPagination()

sidebar := NewSidebar()
sidebar := NewSidebar(dbdriver.GetProvider())

table := &ResultsTable{
Table: tview.NewTable(),
Expand Down Expand Up @@ -221,7 +221,7 @@ func (table *ResultsTable) subscribeToSidebarChanges() {
tableCell.SetText(params.NewValue)

cellValue := models.CellValue{
Type: models.String,
Type: params.Type,
Column: params.ColumnName,
Value: params.NewValue,
TableColumnIndex: changedColumnIndex,
Expand Down
33 changes: 31 additions & 2 deletions components/Sidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

type SidebarState struct {
dbProvider string
currentFieldIndex int
}

Expand All @@ -28,7 +29,7 @@ type Sidebar struct {
subscribers []chan models.StateChange
}

func NewSidebar() *Sidebar {
func NewSidebar(dbProvider string) *Sidebar {
flex := tview.NewFlex().SetDirection(tview.FlexColumnCSS)
frame := tview.NewFrame(flex)
frame.SetBackgroundColor(app.Styles.PrimitiveBackgroundColor)
Expand All @@ -37,6 +38,7 @@ func NewSidebar() *Sidebar {

sidebarState := &SidebarState{
currentFieldIndex: 0,
dbProvider: dbProvider,
}

newSidebar := &Sidebar{
Expand Down Expand Up @@ -242,7 +244,7 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey {
sidebar.SetDisabledStyles(item)
} else {
sidebar.SetEditedStyles(item)
sidebar.Publish(models.StateChange{Key: eventSidebarCommitEditing, Value: models.SidebarEditingCommitParams{ColumnName: columnName, NewValue: newText}})
sidebar.Publish(models.StateChange{Key: eventSidebarCommitEditing, Value: models.SidebarEditingCommitParams{ColumnName: columnName, Type: models.String, NewValue: newText}})
}

return nil
Expand All @@ -259,6 +261,33 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey {

sidebar.EditTextCurrentField()

return nil
case commands.SetValue:
currentItemIndex := sidebar.GetCurrentFieldIndex()
item := sidebar.Flex.GetItem(currentItemIndex).(*tview.TextArea)
x, y, _, _ := item.GetRect()

columnName := item.GetTitle()
columnNameSplit := strings.Split(columnName, "[")
columnName = columnNameSplit[0]

list := NewSetValueList(sidebar.state.dbProvider)

sidebar.Publish(models.StateChange{Key: eventSidebarEditing, Value: true})

list.OnFinish(func(selection models.CellValueType, value string) {
sidebar.Publish(models.StateChange{Key: eventSidebarEditing, Value: false})
App.SetFocus(item)

if selection >= 0 {
sidebar.SetEditedStyles(item)
item.SetText(value, true)
sidebar.Publish(models.StateChange{Key: eventSidebarCommitEditing, Value: models.SidebarEditingCommitParams{ColumnName: columnName, Type: selection, NewValue: value}})
}
})

list.Show(x, y, 30)

return nil
}
return event
Expand Down
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ type Query struct {
type SidebarEditingCommitParams struct {
ColumnName string
NewValue string
Type CellValueType
}

0 comments on commit cabef15

Please sign in to comment.