Skip to content

Commit

Permalink
feat: add sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerojas26 committed Sep 7, 2024
1 parent 7cd8dd2 commit f943e48
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
43 changes: 42 additions & 1 deletion components/ResultsTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ResultsTableState struct {
isEditing bool
isFiltering bool
isLoading bool
setShowSidebar bool
}

type ResultsTable struct {
Expand All @@ -47,6 +48,7 @@ type ResultsTable struct {
EditorPages *tview.Pages
ResultsInfo *tview.TextView
Tree *Tree
Sidebar *Sidebar
DBDriver drivers.Driver
}

Expand All @@ -67,6 +69,7 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver
isEditing: false,
isLoading: false,
listOfDbChanges: listOfDbChanges,
setShowSidebar: true,
}

wrapper := tview.NewFlex()
Expand Down Expand Up @@ -103,13 +106,40 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver
Editor: nil,
Tree: tree,
DBDriver: dbdriver,
Sidebar: NewSidebar(),
}

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.Sidebar.AddItem(label, 1, 0, false)
table.Sidebar.AddItem(field, 3, 0, false)
}

table.ShowSidebar(true)
})

go table.subscribeToTreeChanges()

Expand All @@ -126,7 +156,6 @@ func (table *ResultsTable) WithFilter() *ResultsTable {
table.Wrapper.AddItem(menu.Flex, 3, 0, false)
table.Wrapper.AddItem(filter.Flex, 3, 0, false)
table.Wrapper.AddItem(table, 0, 1, true)
table.Wrapper.AddItem(table.Pagination, 3, 0, false)

go table.subscribeToFilterChanges()

Expand Down Expand Up @@ -1224,3 +1253,15 @@ func (table *ResultsTable) search() {

table.SetInputCapture(nil)
}

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

if show {
table.Page.ShowPage("sidebar")
} else {
table.Page.HidePage("sidebar")
}
}
}
15 changes: 15 additions & 0 deletions components/Sidebar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package components

import "github.com/rivo/tview"

type Sidebar struct {
*tview.Flex
}

func NewSidebar() *Sidebar {
sidebar := tview.NewFlex().SetDirection(tview.FlexColumnCSS)
sidebar.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor)
sidebar.SetBorder(true)

return &Sidebar{sidebar}
}

0 comments on commit f943e48

Please sign in to comment.