Skip to content

Commit

Permalink
feat: edit sidebar input fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerojas26 committed Sep 7, 2024
1 parent f943e48 commit 2085cd9
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 37 deletions.
14 changes: 14 additions & 0 deletions app/Keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
TableGroup = "table"
EditorGroup = "editor"
ConnectionGroup = "connection"
SidebarGroup = "sidebar"
)

// Define a global KeymapSystem object with default keybinds
Expand Down Expand Up @@ -110,11 +111,24 @@ var Keymaps = KeymapSystem{
Bind{Key: Key{Char: '3'}, Cmd: cmd.ConstraintsMenu, Description: "Switch to constraints menu"},
Bind{Key: Key{Char: '4'}, Cmd: cmd.ForeignKeysMenu, Description: "Switch to foreign keys menu"},
Bind{Key: Key{Char: '5'}, Cmd: cmd.IndexesMenu, Description: "Switch to indexes menu"},
// Sidebar
Bind{Key: Key{Char: 'S'}, Cmd: cmd.ToggleSidebar, Description: "Toggle sidebar"},
Bind{Key: Key{Char: 's'}, Cmd: cmd.FocusSidebar, Description: "Focus sidebar"},
},
EditorGroup: {
Bind{Key: Key{Code: tcell.KeyCtrlR}, Cmd: cmd.Execute, Description: "Execute query"},
Bind{Key: Key{Code: tcell.KeyEscape}, Cmd: cmd.UnfocusEditor, Description: "Unfocus editor"},
Bind{Key: Key{Code: tcell.KeyCtrlSpace}, Cmd: cmd.OpenInExternalEditor, Description: "Open in external editor"},
},
SidebarGroup: {
Bind{Key: Key{Char: 's'}, Cmd: cmd.UnfocusSidebar, Description: "Focus table"},
Bind{Key: Key{Char: 'j'}, Cmd: cmd.MoveDown, Description: "Focus next field"},
Bind{Key: Key{Char: 'k'}, Cmd: cmd.MoveUp, Description: "Focus previous field"},
Bind{Key: Key{Char: 'g'}, Cmd: cmd.GotoStart, Description: "Focus first field"},
Bind{Key: Key{Char: 'G'}, Cmd: cmd.GotoEnd, Description: "Focus last field"},
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"},
},
},
}
15 changes: 15 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
UnfocusEditor
Copy
Edit
CommitEdit
DiscardEdit
Save
Delete
Search
Expand All @@ -60,6 +62,9 @@ const (
PreviousFoundNode
TreeCollapseAll
ExpandAll
FocusSidebar
UnfocusSidebar
ToggleSidebar

// Connection
NewConnection
Expand Down Expand Up @@ -179,6 +184,16 @@ func (c Command) String() string {
return "TreeCollapseAll"
case ExpandAll:
return "ExpandAll"
case FocusSidebar:
return "FocusSidebar"
case ToggleSidebar:
return "ToggleSidebar"
case UnfocusSidebar:
return "UnfocusSidebar"
case CommitEdit:
return "CommitEdit"
case DiscardEdit:
return "DiscardEdit"
}
return "Unknown"
}
6 changes: 5 additions & 1 deletion components/Home.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ func (home *Home) subscribeToTreeChanges() {

}

table.FetchRecords(func() {
results := table.FetchRecords(func() {
home.focusLeftWrapper()
})

if len(results) > 1 && !table.GetShowSidebar() { // 1 because the row 0 is the column names
table.ShowSidebar(true)
}

if table.state.error == "" {
home.focusRightWrapper()
}
Expand Down
92 changes: 60 additions & 32 deletions components/ResultsTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ResultsTableState struct {
isEditing bool
isFiltering bool
isLoading bool
setShowSidebar bool
showSidebar bool
}

type ResultsTable struct {
Expand Down Expand Up @@ -69,7 +69,7 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver
isEditing: false,
isLoading: false,
listOfDbChanges: listOfDbChanges,
setShowSidebar: true,
showSidebar: false,
}

wrapper := tview.NewFlex()
Expand All @@ -95,6 +95,8 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver

pagination := NewPagination()

sidebar := NewSidebar()

table := &ResultsTable{
Table: tview.NewTable(),
state: state,
Expand All @@ -106,42 +108,24 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver
Editor: nil,
Tree: tree,
DBDriver: dbdriver,
Sidebar: NewSidebar(),
Sidebar: sidebar,
}

table.SetSelectable(true, true)
table.SetBorders(true)
table.SetFixed(1, 0)
table.SetInputCapture(table.tableInputCapture)
table.SetSelectedStyle(tcell.StyleDefault.Background(tview.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor))
table.Page.AddPage("sidebar", table.Sidebar, false, false)

table.SetSelectionChangedFunc(func(row, _ int) {
columnCount := table.GetColumnCount()

tableX, tableY, tableWidth, tableHeight := table.GetRect()

sidebarWidth := tableWidth / 3
table.Sidebar.SetRect(tableX+tableWidth-sidebarWidth, tableY, sidebarWidth, tableHeight)
table.Sidebar.Clear()

for i := 0; i < columnCount; i++ {
label := tview.NewTextView()
field := tview.NewInputField()
field.SetBorder(true)
field.SetFieldStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.SecondaryTextColor))

label.SetText(table.GetColumnNameByIndex(i))
field.SetText(table.GetCell(row, i).Text)
table.Page.AddPage(SidebarPageName, table.Sidebar, false, false)

table.Sidebar.AddItem(label, 1, 0, false)
table.Sidebar.AddItem(field, 3, 0, false)
table.SetSelectionChangedFunc(func(_, _ int) {
if table.GetShowSidebar() {
table.UpdateSidebar()
}

table.ShowSidebar(true)
})

go table.subscribeToTreeChanges()
go table.subscribeToSidebarChanges()

return table
}
Expand Down Expand Up @@ -215,6 +199,21 @@ func (table *ResultsTable) subscribeToTreeChanges() {
}
}

func (table *ResultsTable) subscribeToSidebarChanges() {
ch := table.Sidebar.Subscribe()

for stateChange := range ch {
switch stateChange.Key {
case "Editing":
editing := stateChange.Value.(bool)
table.SetIsEditing(editing)
case "Unfocusing":
App.SetFocus(table)
App.ForceDraw()
}
}
}

func (table *ResultsTable) AddRows(rows [][]string) {
for i, row := range rows {
for j, cell := range row {
Expand Down Expand Up @@ -396,6 +395,10 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event
}

}
} else if command == commands.ToggleSidebar {
table.ShowSidebar(!table.GetShowSidebar())
} else if command == commands.FocusSidebar {
App.SetFocus(table.Sidebar)
}

if len(table.GetRecords()) > 0 {
Expand Down Expand Up @@ -655,6 +658,10 @@ func (table *ResultsTable) GetIsFiltering() bool {
return table.state.isFiltering
}

func (table *ResultsTable) GetShowSidebar() bool {
return table.state.showSidebar
}

// Setters

func (table *ResultsTable) SetRecords(rows [][]string) {
Expand Down Expand Up @@ -1255,13 +1262,34 @@ func (table *ResultsTable) search() {
}

func (table *ResultsTable) ShowSidebar(show bool) {
if table.state.setShowSidebar != show {
table.state.setShowSidebar = show
table.state.showSidebar = show

if show {
table.Page.ShowPage("sidebar")
} else {
table.Page.HidePage("sidebar")
if show {
table.UpdateSidebar()
table.Page.SendToFront(SidebarPageName)
table.Page.ShowPage(SidebarPageName)
} else {
table.Page.HidePage(SidebarPageName)
}
}

func (table *ResultsTable) UpdateSidebar() {
columnCount := table.GetColumnCount()
selectedRow, _ := table.GetSelection()

if selectedRow > 0 {
tableX, tableY, tableWidth, tableHeight := table.GetInnerRect()

sidebarWidth := (tableWidth / 3)

table.Sidebar.SetRect(tableX+tableWidth-sidebarWidth, tableY, sidebarWidth, tableHeight)
table.Sidebar.Clear()

for i := 0; i < columnCount; i++ {
title := table.GetColumnNameByIndex(i)
text := table.GetCell(selectedRow, i).Text

table.Sidebar.AddField(title, text, sidebarWidth)
}
}
}
Loading

0 comments on commit 2085cd9

Please sign in to comment.