From f943e48bc5de099a9b97a02cf044adb34eaac031 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sat, 7 Sep 2024 01:38:28 -0400 Subject: [PATCH 01/19] feat: add sidebar --- components/ResultsTable.go | 43 +++++++++++++++++++++++++++++++++++++- components/Sidebar.go | 15 +++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 components/Sidebar.go diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 6b0b652..096b6e3 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -31,6 +31,7 @@ type ResultsTableState struct { isEditing bool isFiltering bool isLoading bool + setShowSidebar bool } type ResultsTable struct { @@ -47,6 +48,7 @@ type ResultsTable struct { EditorPages *tview.Pages ResultsInfo *tview.TextView Tree *Tree + Sidebar *Sidebar DBDriver drivers.Driver } @@ -67,6 +69,7 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver isEditing: false, isLoading: false, listOfDbChanges: listOfDbChanges, + setShowSidebar: true, } wrapper := tview.NewFlex() @@ -103,6 +106,7 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver Editor: nil, Tree: tree, DBDriver: dbdriver, + Sidebar: NewSidebar(), } table.SetSelectable(true, true) @@ -110,6 +114,32 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver 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() @@ -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() @@ -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") + } + } +} diff --git a/components/Sidebar.go b/components/Sidebar.go new file mode 100644 index 0000000..43a9301 --- /dev/null +++ b/components/Sidebar.go @@ -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} +} From 2085cd9de8efefa801a6ab26368387e0ec4707ea Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sat, 7 Sep 2024 15:08:39 -0400 Subject: [PATCH 02/19] feat: edit sidebar input fields --- app/Keymap.go | 14 +++ commands/commands.go | 15 +++ components/Home.go | 6 +- components/ResultsTable.go | 92 ++++++++++------ components/Sidebar.go | 214 ++++++++++++++++++++++++++++++++++++- components/constants.go | 5 +- 6 files changed, 309 insertions(+), 37 deletions(-) diff --git a/app/Keymap.go b/app/Keymap.go index f80a57d..1a3803d 100644 --- a/app/Keymap.go +++ b/app/Keymap.go @@ -44,6 +44,7 @@ const ( TableGroup = "table" EditorGroup = "editor" ConnectionGroup = "connection" + SidebarGroup = "sidebar" ) // Define a global KeymapSystem object with default keybinds @@ -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"}, + }, }, } diff --git a/commands/commands.go b/commands/commands.go index 7929380..6773810 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -45,6 +45,8 @@ const ( UnfocusEditor Copy Edit + CommitEdit + DiscardEdit Save Delete Search @@ -60,6 +62,9 @@ const ( PreviousFoundNode TreeCollapseAll ExpandAll + FocusSidebar + UnfocusSidebar + ToggleSidebar // Connection NewConnection @@ -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" } diff --git a/components/Home.go b/components/Home.go index cc8f07c..cdca7a5 100644 --- a/components/Home.go +++ b/components/Home.go @@ -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() } diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 096b6e3..490b4ac 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -31,7 +31,7 @@ type ResultsTableState struct { isEditing bool isFiltering bool isLoading bool - setShowSidebar bool + showSidebar bool } type ResultsTable struct { @@ -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() @@ -95,6 +95,8 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver pagination := NewPagination() + sidebar := NewSidebar() + table := &ResultsTable{ Table: tview.NewTable(), state: state, @@ -106,7 +108,7 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver Editor: nil, Tree: tree, DBDriver: dbdriver, - Sidebar: NewSidebar(), + Sidebar: sidebar, } table.SetSelectable(true, true) @@ -114,34 +116,16 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver 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 } @@ -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 { @@ -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 { @@ -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) { @@ -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) } } } diff --git a/components/Sidebar.go b/components/Sidebar.go index 43a9301..9432a46 100644 --- a/components/Sidebar.go +++ b/components/Sidebar.go @@ -1,9 +1,23 @@ package components -import "github.com/rivo/tview" +import ( + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" + + "github.com/jorgerojas26/lazysql/app" + "github.com/jorgerojas26/lazysql/commands" + "github.com/jorgerojas26/lazysql/models" +) + +type SidebarState struct { + currentFieldIndex int +} type Sidebar struct { *tview.Flex + state *SidebarState + Fields []*tview.TextArea + subscribers []chan models.StateChange } func NewSidebar() *Sidebar { @@ -11,5 +25,201 @@ func NewSidebar() *Sidebar { sidebar.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) sidebar.SetBorder(true) - return &Sidebar{sidebar} + sidebarState := &SidebarState{ + currentFieldIndex: 0, + } + + newSidebar := &Sidebar{ + Flex: sidebar, + state: sidebarState, + Fields: []*tview.TextArea{}, + subscribers: []chan models.StateChange{}, + } + + newSidebar.SetInputCapture(newSidebar.inputCapture) + + newSidebar.SetBlurFunc(func() { + newSidebar.SetCurrentFieldIndex(0) + }) + + return newSidebar +} + +func (sidebar *Sidebar) AddField(title, text string, fieldWidth int) { + field := tview.NewTextArea() + field.SetWrap(true) + field.SetDisabled(true) + + field.SetBorder(true) + field.SetTitle(title) + field.SetTitleAlign(tview.AlignLeft) + field.SetTitleColor(tview.Styles.PrimaryTextColor) + field.SetText(text, true) + field.SetTextStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.SecondaryTextColor)) + + textLength := len(field.GetText()) + + itemFixedSize := 3 + + if textLength >= fieldWidth*3 { + itemFixedSize = 5 + } else if textLength >= fieldWidth { + itemFixedSize = 4 + } else { + field.SetWrap(false) + } + + sidebar.Fields = append(sidebar.Fields, field) + sidebar.AddItem(field, itemFixedSize, 0, true) +} + +func (sidebar *Sidebar) FocusNextField() { + newIndex := sidebar.GetCurrentFieldIndex() + 1 + + if newIndex < sidebar.GetItemCount() { + item := sidebar.Fields[newIndex] + + if item != nil { + sidebar.SetCurrentFieldIndex(newIndex) + App.SetFocus(item) + App.ForceDraw() + return + } + + } +} + +func (sidebar *Sidebar) FocusPreviousField() { + newIndex := sidebar.GetCurrentFieldIndex() - 1 + + if newIndex >= 0 { + item := sidebar.Fields[newIndex] + + if item != nil { + sidebar.SetCurrentFieldIndex(newIndex) + App.SetFocus(item) + App.ForceDraw() + return + } + } +} + +func (sidebar *Sidebar) FocusFirstField() { + sidebar.SetCurrentFieldIndex(0) + App.SetFocus(sidebar.Fields[0]) +} + +func (sidebar *Sidebar) FocusLastField() { + newIndex := sidebar.GetItemCount() - 1 + sidebar.SetCurrentFieldIndex(newIndex) + App.SetFocus(sidebar.Fields[newIndex]) +} + +func (sidebar *Sidebar) FocusField(index int) { + sidebar.SetCurrentFieldIndex(index) + App.SetFocus(sidebar.Fields[index]) +} + +func (sidebar *Sidebar) Clear() { + sidebar.Fields = make([]*tview.TextArea, 0) + sidebar.Flex.Clear() +} + +func (sidebar *Sidebar) EditTextCurrentField() { + index := sidebar.GetCurrentFieldIndex() + item := sidebar.Fields[index] + + sidebar.SetEditingStyles(item) +} + +func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { + command := app.Keymaps.Group(app.SidebarGroup).Resolve(event) + + switch command { + case commands.UnfocusSidebar: + sidebar.Publish(models.StateChange{Key: "Unfocusing", Value: nil}) + case commands.MoveDown: + sidebar.FocusNextField() + case commands.MoveUp: + sidebar.FocusPreviousField() + case commands.GotoStart: + sidebar.FocusFirstField() + case commands.GotoEnd: + sidebar.FocusLastField() + case commands.Edit: + sidebar.Publish(models.StateChange{Key: "Editing", Value: true}) + + currentItemIndex := sidebar.GetCurrentFieldIndex() + item := sidebar.Fields[currentItemIndex] + text := item.GetText() + + sidebar.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + command := app.Keymaps.Group(app.SidebarGroup).Resolve(event) + + switch command { + case commands.CommitEdit: + sidebar.SetInputCapture(sidebar.inputCapture) + sidebar.SetDisabledStyles(item) + sidebar.Publish(models.StateChange{Key: "Editing", Value: false}) + return nil + case commands.DiscardEdit: + sidebar.SetInputCapture(sidebar.inputCapture) + sidebar.SetDisabledStyles(item) + item.SetText(text, true) + sidebar.Publish(models.StateChange{Key: "Editing", Value: false}) + return nil + } + + return event + }) + + sidebar.EditTextCurrentField() + + return nil + } + return event +} + +func (sidebar *Sidebar) SetEditingStyles(item *tview.TextArea) { + item.SetBackgroundColor(tview.Styles.SecondaryTextColor) + item.SetTextStyle(tcell.StyleDefault.Background(tview.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) + item.SetTitleColor(tview.Styles.ContrastSecondaryTextColor) + item.SetBorderColor(tview.Styles.ContrastSecondaryTextColor) + + item.SetWrap(true) + item.SetDisabled(false) +} + +func (sidebar *Sidebar) SetDisabledStyles(item *tview.TextArea) { + item.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) + item.SetTextStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.SecondaryTextColor)) + item.SetTitleColor(tview.Styles.PrimaryTextColor) + item.SetBorderColor(tview.Styles.BorderColor) + + item.SetWrap(true) + item.SetDisabled(true) +} + +// Getters +func (sidebar *Sidebar) GetCurrentFieldIndex() int { + return sidebar.state.currentFieldIndex +} + +// Setters +func (sidebar *Sidebar) SetCurrentFieldIndex(index int) { + sidebar.state.currentFieldIndex = index +} + +// Subscribe to changes in the sidebar state +func (sidebar *Sidebar) Subscribe() chan models.StateChange { + subscriber := make(chan models.StateChange) + sidebar.subscribers = append(sidebar.subscribers, subscriber) + return subscriber +} + +// Publish subscribers of changes in the sidebar state +func (sidebar *Sidebar) Publish(change models.StateChange) { + for _, subscriber := range sidebar.subscribers { + subscriber <- change + } } diff --git a/components/constants.go b/components/constants.go index 6422737..101f2f2 100644 --- a/components/constants.go +++ b/components/constants.go @@ -5,6 +5,7 @@ import "github.com/jorgerojas26/lazysql/app" var App = app.App const ( - EditorTabName string = "Editor" - HelpPageName string = "Help" + EditorTabName string = "Editor" + HelpPageName string = "Help" + SidebarPageName string = "Sidebar" ) From 3c68acb264925b5d5cf7a4f597ae3d647abae87f Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sat, 7 Sep 2024 15:24:03 -0400 Subject: [PATCH 03/19] feat: makes sidebar taller --- components/ResultsTable.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 490b4ac..85f373c 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -1278,11 +1278,14 @@ func (table *ResultsTable) UpdateSidebar() { selectedRow, _ := table.GetSelection() if selectedRow > 0 { - tableX, tableY, tableWidth, tableHeight := table.GetInnerRect() + tableX, _, _, tableHeight := table.GetRect() + _, _, tableInnerWidth, _ := table.GetInnerRect() + _, tableMenuY, _, tableMenuHeight := table.Menu.GetRect() + _, _, _, tableFilterHeight := table.Filter.GetRect() - sidebarWidth := (tableWidth / 3) + sidebarWidth := (tableInnerWidth / 3) - table.Sidebar.SetRect(tableX+tableWidth-sidebarWidth, tableY, sidebarWidth, tableHeight) + table.Sidebar.SetRect(tableX+tableInnerWidth-sidebarWidth, tableMenuY, sidebarWidth, tableHeight+tableMenuHeight+tableFilterHeight) table.Sidebar.Clear() for i := 0; i < columnCount; i++ { From 856be0a385ae39b1e871eaba42ab5ca0b3cbf0aa Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sat, 7 Sep 2024 16:28:08 -0400 Subject: [PATCH 04/19] fix: makes sidebar a tview frame to fix bg color --- components/Sidebar.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/components/Sidebar.go b/components/Sidebar.go index 9432a46..a7b069d 100644 --- a/components/Sidebar.go +++ b/components/Sidebar.go @@ -14,23 +14,26 @@ type SidebarState struct { } type Sidebar struct { - *tview.Flex + *tview.Frame + Flex *tview.Flex state *SidebarState Fields []*tview.TextArea subscribers []chan models.StateChange } func NewSidebar() *Sidebar { - sidebar := tview.NewFlex().SetDirection(tview.FlexColumnCSS) - sidebar.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) - sidebar.SetBorder(true) + flex := tview.NewFlex().SetDirection(tview.FlexColumnCSS) + frame := tview.NewFrame(flex) + frame.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) + frame.SetBorder(true) sidebarState := &SidebarState{ currentFieldIndex: 0, } newSidebar := &Sidebar{ - Flex: sidebar, + Frame: frame, + Flex: flex, state: sidebarState, Fields: []*tview.TextArea{}, subscribers: []chan models.StateChange{}, @@ -70,13 +73,13 @@ func (sidebar *Sidebar) AddField(title, text string, fieldWidth int) { } sidebar.Fields = append(sidebar.Fields, field) - sidebar.AddItem(field, itemFixedSize, 0, true) + sidebar.Flex.AddItem(field, itemFixedSize, 0, true) } func (sidebar *Sidebar) FocusNextField() { newIndex := sidebar.GetCurrentFieldIndex() + 1 - if newIndex < sidebar.GetItemCount() { + if newIndex < sidebar.Flex.GetItemCount() { item := sidebar.Fields[newIndex] if item != nil { @@ -110,7 +113,7 @@ func (sidebar *Sidebar) FocusFirstField() { } func (sidebar *Sidebar) FocusLastField() { - newIndex := sidebar.GetItemCount() - 1 + newIndex := sidebar.Flex.GetItemCount() - 1 sidebar.SetCurrentFieldIndex(newIndex) App.SetFocus(sidebar.Fields[newIndex]) } From a5e6a4f7888057d1ab86a1c5baa01f82ac880ea5 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sat, 7 Sep 2024 16:33:59 -0400 Subject: [PATCH 05/19] feat: makes sidebar toggleable when focused --- app/Keymap.go | 1 + components/Sidebar.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/app/Keymap.go b/app/Keymap.go index 1a3803d..f4176f0 100644 --- a/app/Keymap.go +++ b/app/Keymap.go @@ -122,6 +122,7 @@ var Keymaps = KeymapSystem{ }, SidebarGroup: { Bind{Key: Key{Char: 's'}, Cmd: cmd.UnfocusSidebar, Description: "Focus table"}, + Bind{Key: Key{Char: 'S'}, Cmd: cmd.ToggleSidebar, 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"}, diff --git a/components/Sidebar.go b/components/Sidebar.go index a7b069d..91edd66 100644 --- a/components/Sidebar.go +++ b/components/Sidebar.go @@ -141,6 +141,8 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { switch command { case commands.UnfocusSidebar: sidebar.Publish(models.StateChange{Key: "Unfocusing", Value: nil}) + case commands.ToggleSidebar: + sidebar.Publish(models.StateChange{Key: "Toggling", Value: nil}) case commands.MoveDown: sidebar.FocusNextField() case commands.MoveUp: From 9c693f534222049aa370f1705a9c8407d1c63bad Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sat, 7 Sep 2024 16:34:15 -0400 Subject: [PATCH 06/19] fix: focusing when is hide --- components/ResultsTable.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 85f373c..6391f8d 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -210,6 +210,9 @@ func (table *ResultsTable) subscribeToSidebarChanges() { case "Unfocusing": App.SetFocus(table) App.ForceDraw() + case "Toggling": + table.ShowSidebar(false) + App.ForceDraw() } } } @@ -398,7 +401,9 @@ 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 table.GetShowSidebar() { + App.SetFocus(table.Sidebar) + } } if len(table.GetRecords()) > 0 { @@ -1270,6 +1275,7 @@ func (table *ResultsTable) ShowSidebar(show bool) { table.Page.ShowPage(SidebarPageName) } else { table.Page.HidePage(SidebarPageName) + App.SetFocus(table) } } From 7c8e50166438b5990e42a4a974f64b62c8eb88de Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sat, 7 Sep 2024 19:45:21 -0400 Subject: [PATCH 07/19] make sidebar a bit smaller --- components/ResultsTable.go | 1 + 1 file changed, 1 insertion(+) diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 6391f8d..20a1fdc 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -1290,6 +1290,7 @@ func (table *ResultsTable) UpdateSidebar() { _, _, _, tableFilterHeight := table.Filter.GetRect() sidebarWidth := (tableInnerWidth / 3) + sidebarWidth := (tableInnerWidth / 4) table.Sidebar.SetRect(tableX+tableInnerWidth-sidebarWidth, tableMenuY, sidebarWidth, tableHeight+tableMenuHeight+tableFilterHeight) table.Sidebar.Clear() From acefcbb5a5664361d0bb1c281467facb402de780 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sat, 7 Sep 2024 19:47:37 -0400 Subject: [PATCH 08/19] feat: add column type to the input title --- components/ResultsTable.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 20a1fdc..ebf04af 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -1280,7 +1280,7 @@ func (table *ResultsTable) ShowSidebar(show bool) { } func (table *ResultsTable) UpdateSidebar() { - columnCount := table.GetColumnCount() + columns := table.GetColumns() selectedRow, _ := table.GetSelection() if selectedRow > 0 { @@ -1289,15 +1289,26 @@ func (table *ResultsTable) UpdateSidebar() { _, tableMenuY, _, tableMenuHeight := table.Menu.GetRect() _, _, _, tableFilterHeight := table.Filter.GetRect() - sidebarWidth := (tableInnerWidth / 3) sidebarWidth := (tableInnerWidth / 4) table.Sidebar.SetRect(tableX+tableInnerWidth-sidebarWidth, tableMenuY, sidebarWidth, tableHeight+tableMenuHeight+tableFilterHeight) table.Sidebar.Clear() - for i := 0; i < columnCount; i++ { - title := table.GetColumnNameByIndex(i) - text := table.GetCell(selectedRow, i).Text + for i := 1; i < len(columns); i++ { + name := columns[i][0] + colType := columns[i][1] + + text := table.GetCell(selectedRow, i-1).Text + title := name + + logger.Info("string repeat", map[string]any{"sidebarWidth": sidebarWidth, "name": len(name), "colType": len(colType)}) + repeatCount := sidebarWidth - len(name) - len(colType) - 6 // 2 for spaces added below and idk why 6 is needed, but it works. + + if repeatCount <= 0 { + repeatCount = 1 + } + title += "[gray]" + strings.Repeat("-", repeatCount) + title += colType table.Sidebar.AddField(title, text, sidebarWidth) } From 9e8fd4912a5d93ac31244777a986b578f7fb8ac6 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sun, 8 Sep 2024 12:29:43 -0400 Subject: [PATCH 09/19] chore: converts magic strings into constants --- components/ConnectionForm.go | 14 +++---- components/ConnectionPage.go | 4 +- components/ConnectionSelection.go | 18 ++++----- components/Home.go | 20 +++++----- components/Pages.go | 2 +- components/ResultTableFilter.go | 2 +- components/ResultsTable.go | 48 +++++++++++------------ components/ResultsTableMenu.go | 16 ++++---- components/SQLEditor.go | 4 +- components/Sidebar.go | 10 ++--- components/Tree.go | 6 +-- components/constants.go | 63 ++++++++++++++++++++++++++++++- drivers/constants.go | 7 ++++ drivers/mysql.go | 2 +- drivers/postgres.go | 2 +- drivers/sqlite.go | 2 +- 16 files changed, 143 insertions(+), 77 deletions(-) diff --git a/components/ConnectionForm.go b/components/ConnectionForm.go index 36d6c26..127769c 100644 --- a/components/ConnectionForm.go +++ b/components/ConnectionForm.go @@ -77,7 +77,7 @@ func NewConnectionForm(connectionPages *models.ConnectionPages) *ConnectionForm func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages) func(event *tcell.EventKey) *tcell.EventKey { return func(event *tcell.EventKey) *tcell.EventKey { if event.Key() == tcell.KeyEsc { - connectionPages.SwitchToPage("Connections") + connectionPages.SwitchToPage(ConnectionsPageName) } else if event.Key() == tcell.KeyF1 || event.Key() == tcell.KeyEnter { connectionName := form.GetFormItem(0).(*tview.InputField).GetText() @@ -111,7 +111,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages } switch form.Action { - case "create": + case NewConnection: newDatabases = append(databases, parsedDatabaseData) err := helpers.SaveConnectionConfig(newDatabases) @@ -120,7 +120,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages return event } - case "edit": + case EditConnection: newDatabases = make([]models.Connection, len(databases)) row, _ := ConnectionListTable.GetSelection() @@ -151,7 +151,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages } ConnectionListTable.SetConnections(newDatabases) - connectionPages.SwitchToPage("Connections") + connectionPages.SwitchToPage(ConnectionsPageName) } else if event.Key() == tcell.KeyF2 { connectionString := form.GetFormItem(1).(*tview.InputField).GetText() @@ -173,11 +173,11 @@ func (form *ConnectionForm) testConnection(connectionString string) { var db drivers.Driver switch parsed.Driver { - case "mysql": + case drivers.MySQLDriver: db = &drivers.MySQL{} - case "postgres": + case drivers.PostgresDriver: db = &drivers.Postgres{} - case "sqlite3": + case drivers.SQLiteDriver: db = &drivers.SQLite{} } diff --git a/components/ConnectionPage.go b/components/ConnectionPage.go index 1e13431..aca3c33 100644 --- a/components/ConnectionPage.go +++ b/components/ConnectionPage.go @@ -32,8 +32,8 @@ func NewConnectionPages() *models.ConnectionPages { connectionForm := NewConnectionForm(cp) connectionSelection := NewConnectionSelection(connectionForm, cp) - cp.AddPage("Connections", connectionSelection.Flex, true, true) - cp.AddPage("ConnectionForm", connectionForm.Flex, true, false) + cp.AddPage(ConnectionsSelectionPageName, connectionSelection.Flex, true, true) + cp.AddPage(ConnectionsFormPageName, connectionForm.Flex, true, false) return cp } diff --git a/components/ConnectionSelection.go b/components/ConnectionSelection.go index 53f34d0..6c9232b 100644 --- a/components/ConnectionSelection.go +++ b/components/ConnectionSelection.go @@ -88,18 +88,18 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod case commands.Connect: go cs.Connect(selectedConnection) case commands.EditConnection: - connectionPages.SwitchToPage("ConnectionForm") + connectionPages.SwitchToPage(ConnectionsFormPageName) connectionForm.GetFormItemByLabel("Name").(*tview.InputField).SetText(selectedConnection.Name) connectionForm.GetFormItemByLabel("URL").(*tview.InputField).SetText(selectedConnection.URL) connectionForm.StatusText.SetText("") - connectionForm.SetAction("edit") + connectionForm.SetAction(EditConnection) return nil case commands.DeleteConnection: confirmationModal := NewConfirmationModal("") confirmationModal.SetDoneFunc(func(_ int, buttonLabel string) { - MainPages.RemovePage("Confirmation") + MainPages.RemovePage(ConfirmationPageName) confirmationModal = nil if buttonLabel == "Yes" { @@ -115,7 +115,7 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod } }) - MainPages.AddPage("Confirmation", confirmationModal, true, true) + MainPages.AddPage(ConfirmationPageName, confirmationModal, true, true) return nil } @@ -123,11 +123,11 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod switch command { case commands.NewConnection: - connectionForm.SetAction("create") + connectionForm.SetAction(NewConnection) connectionForm.GetFormItemByLabel("Name").(*tview.InputField).SetText("") connectionForm.GetFormItemByLabel("URL").(*tview.InputField).SetText("") connectionForm.StatusText.SetText("") - connectionPages.SwitchToPage("ConnectionForm") + connectionPages.SwitchToPage(ConnectionsFormPageName) case commands.Quit: if wrapper.HasFocus() { app.App.Stop() @@ -151,11 +151,11 @@ func (cs *ConnectionSelection) Connect(connection models.Connection) { var newDbDriver drivers.Driver switch connection.Provider { - case "mysql": + case drivers.MySQLDriver: newDbDriver = &drivers.MySQL{} - case "postgres": + case drivers.PostgresDriver: newDbDriver = &drivers.Postgres{} - case "sqlite3": + case drivers.SQLiteDriver: newDbDriver = &drivers.SQLite{} } diff --git a/components/Home.go b/components/Home.go index cdca7a5..1226b98 100644 --- a/components/Home.go +++ b/components/Home.go @@ -66,7 +66,7 @@ func NewHomePage(connection models.Connection, dbdriver drivers.Driver) *Home { home.SetInputCapture(home.homeInputCapture) home.SetFocusFunc(func() { - if home.FocusedWrapper == "left" || home.FocusedWrapper == "" { + if home.FocusedWrapper == FocusedWrapperLeft || home.FocusedWrapper == "" { home.focusLeftWrapper() } else { home.focusRightWrapper() @@ -82,7 +82,7 @@ func (home *Home) subscribeToTreeChanges() { for stateChange := range ch { switch stateChange.Key { - case "SelectedTable": + case SelectedTableTree: databaseName := home.Tree.GetSelectedDatabase() tableName := stateChange.Value.(string) @@ -117,7 +117,7 @@ func (home *Home) subscribeToTreeChanges() { } app.App.ForceDraw() - case "IsFiltering": + case IsFilteringTree: isFiltering := stateChange.Value.(bool) if isFiltering { home.SetInputCapture(nil) @@ -140,7 +140,7 @@ func (home *Home) focusRightWrapper() { home.focusTab(tab) } - home.FocusedWrapper = "right" + home.FocusedWrapper = FocusedWrapperRight } func (home *Home) focusTab(tab *Tab) { @@ -193,7 +193,7 @@ func (home *Home) focusLeftWrapper() { App.SetFocus(home.Tree) - home.FocusedWrapper = "left" + home.FocusedWrapper = FocusedWrapperLeft } func (home *Home) rightWrapperInputCapture(event *tcell.EventKey) *tcell.EventKey { @@ -272,11 +272,11 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { switch command { case commands.MoveLeft: - if table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && home.FocusedWrapper == "right" { + if table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && home.FocusedWrapper == FocusedWrapperRight { home.focusLeftWrapper() } case commands.MoveRight: - if table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && home.FocusedWrapper == "left" { + if table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && home.FocusedWrapper == FocusedWrapperLeft { home.focusRightWrapper() } case commands.SwitchToEditorView: @@ -295,7 +295,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { App.ForceDraw() case commands.SwitchToConnectionsView: if (table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && !table.GetIsLoading()) || table == nil { - MainPages.SwitchToPage("Connections") + MainPages.SwitchToPage(ConnectionsPageName) } case commands.Quit: if tab != nil { @@ -312,7 +312,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { confirmationModal := NewConfirmationModal("") confirmationModal.SetDoneFunc(func(_ int, buttonLabel string) { - MainPages.RemovePage("Confirmation") + MainPages.RemovePage(ConfirmationPageName) confirmationModal = nil if buttonLabel == "Yes" { @@ -332,7 +332,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { } }) - MainPages.AddPage("Confirmation", confirmationModal, true, true) + MainPages.AddPage(ConfirmationPageName, confirmationModal, true, true) } case commands.HelpPopup: if table == nil || !table.GetIsEditing() { diff --git a/components/Pages.go b/components/Pages.go index 257cf76..2055685 100644 --- a/components/Pages.go +++ b/components/Pages.go @@ -8,5 +8,5 @@ var MainPages = tview.NewPages() func init() { MainPages.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) - MainPages.AddPage("Connections", NewConnectionPages().Flex, true, true) + MainPages.AddPage(ConnectionsPageName, NewConnectionPages().Flex, true, true) } diff --git a/components/ResultTableFilter.go b/components/ResultTableFilter.go index bf526d5..f9e8bf5 100644 --- a/components/ResultTableFilter.go +++ b/components/ResultTableFilter.go @@ -67,7 +67,7 @@ func (filter *ResultsTableFilter) Subscribe() chan models.StateChange { func (filter *ResultsTableFilter) Publish(message string) { for _, sub := range filter.subscribers { sub <- models.StateChange{ - Key: "Filter", + Key: FilteringResultsTable, Value: message, } } diff --git a/components/ResultsTable.go b/components/ResultsTable.go index ebf04af..c547518 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -89,9 +89,9 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver loadingModal.SetTextColor(tview.Styles.SecondaryTextColor) pages := tview.NewPages() - pages.AddPage("table", wrapper, true, true) - pages.AddPage("error", errorModal, true, false) - pages.AddPage("loading", loadingModal, false, false) + pages.AddPage(TablePageName, wrapper, true, true) + pages.AddPage(TableErrorPageName, errorModal, true, false) + pages.AddPage(TableLoadingPageName, loadingModal, false, false) pagination := NewPagination() @@ -176,8 +176,8 @@ func (table *ResultsTable) WithEditor() *ResultsTable { resultsInfoText.SetTextColor(tview.Styles.PrimaryTextColor) resultsInfoWrapper.AddItem(resultsInfoText, 3, 0, false) - editorPages.AddPage("Table", tableWrapper, true, false) - editorPages.AddPage("ResultsInfo", resultsInfoWrapper, true, true) + editorPages.AddPage(TableEditorTablePageName, tableWrapper, true, false) + editorPages.AddPage(TableEditorResultsInfoPageName, resultsInfoWrapper, true, true) table.EditorPages = editorPages table.ResultsInfo = resultsInfoText @@ -193,7 +193,7 @@ func (table *ResultsTable) subscribeToTreeChanges() { ch := table.Tree.Subscribe() for stateChange := range ch { - if stateChange.Key == "SelectedDatabase" { + if stateChange.Key == SelectedDatabaseTree { table.SetDatabaseName(stateChange.Value.(string)) } } @@ -204,13 +204,13 @@ func (table *ResultsTable) subscribeToSidebarChanges() { for stateChange := range ch { switch stateChange.Key { - case "Editing": + case EditingSidebar: editing := stateChange.Value.(bool) table.SetIsEditing(editing) - case "Unfocusing": + case UnfocusingSidebar: App.SetFocus(table) App.ForceDraw() - case "Toggling": + case TogglingSidebar: table.ShowSidebar(false) App.ForceDraw() } @@ -493,7 +493,7 @@ func (table *ResultsTable) subscribeToFilterChanges() { for stateChange := range ch { switch stateChange.Key { - case "Filter": + case FilteringResultsTable: if stateChange.Value != "" { rows := table.FetchRecords(nil) @@ -532,7 +532,7 @@ func (table *ResultsTable) subscribeToEditorChanges() { for stateChange := range ch { switch stateChange.Key { - case "Query": + case QuerySQLEditor: query := stateChange.Value.(string) if query != "" { queryLower := strings.ToLower(query) @@ -568,7 +568,7 @@ func (table *ResultsTable) subscribeToEditorChanges() { } table.SetLoading(false) } - table.EditorPages.SwitchToPage("Table") + table.EditorPages.SwitchToPage(TablePageName) App.Draw() } else { table.SetRecords([][]string{}) @@ -584,13 +584,13 @@ func (table *ResultsTable) subscribeToEditorChanges() { } else { table.SetResultsInfo(result) table.SetLoading(false) - table.EditorPages.SwitchToPage("ResultsInfo") + table.EditorPages.SwitchToPage(TableEditorResultsInfoPageName) App.SetFocus(table.Editor) App.Draw() } } } - case "Escape": + case EscapeSQLEditor: table.SetIsFiltering(false) App.SetFocus(table) table.HighlightTable() @@ -704,7 +704,7 @@ func (table *ResultsTable) SetError(err string, done func()) { table.Error.SetText(err) table.Error.SetDoneFunc(func(_ int, _ string) { table.state.error = "" - table.Page.HidePage("error") + table.Page.HidePage(TableErrorPageName) if table.GetIsFiltering() { if table.Editor != nil { App.SetFocus(table.Editor) @@ -718,7 +718,7 @@ func (table *ResultsTable) SetError(err string, done func()) { done() } }) - table.Page.ShowPage("error") + table.Page.ShowPage(TableErrorPageName) App.SetFocus(table.Error) App.ForceDraw() } @@ -731,7 +731,7 @@ func (table *ResultsTable) SetLoading(show bool) { defer func() { if r := recover(); r != nil { logger.Error("ResultsTable.go:800 => Recovered from panic", map[string]any{"error": r}) - _ = table.Page.HidePage("loading") + _ = table.Page.HidePage(TableLoadingPageName) if table.state.error != "" { App.SetFocus(table.Error) } else { @@ -742,11 +742,11 @@ func (table *ResultsTable) SetLoading(show bool) { table.state.isLoading = show if show { - table.Page.ShowPage("loading") + table.Page.ShowPage(TableLoadingPageName) App.SetFocus(table.Loading) App.ForceDraw() } else { - table.Page.HidePage("loading") + table.Page.HidePage(TableLoadingPageName) if table.state.error != "" { App.SetFocus(table.Error) } else { @@ -909,7 +909,7 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV if key == tcell.KeyEnter || key == tcell.KeyEscape { table.SetInputCapture(table.tableInputCapture) - table.Page.RemovePage("edit") + table.Page.RemovePage(TableEditCellPageName) App.SetFocus(table) } @@ -920,7 +920,7 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV x, y, width := cell.GetLastPosition() inputField.SetRect(x, y, width+1, 1) - table.Page.AddPage("edit", inputField, false, true) + table.Page.AddPage(TableEditCellPageName, inputField, false, true) App.SetFocus(inputField) } @@ -1038,7 +1038,7 @@ func (table *ResultsTable) GetPrimaryKeyValue(rowIndex int) (string, string) { primaryKeyValue := "" switch provider { - case "mysql": + case drivers.MySQLDriver: keyColumnIndex := -1 primaryKeyColumnIndex := -1 @@ -1059,7 +1059,7 @@ func (table *ResultsTable) GetPrimaryKeyValue(rowIndex int) (string, string) { primaryKeyValue = table.GetRecords()[rowIndex][primaryKeyColumnIndex] } - case "postgres": + case drivers.PostgresDriver: keyColumnIndex := -1 constraintTypeColumnIndex := -1 constraintNameColumnIndex := -1 @@ -1101,7 +1101,7 @@ func (table *ResultsTable) GetPrimaryKeyValue(rowIndex int) (string, string) { primaryKeyValue = table.GetRecords()[rowIndex][primaryKeyColumnIndex] } - case "sqlite3": + case drivers.SQLiteDriver: keyColumnIndex := -1 primaryKeyColumnIndex := -1 diff --git a/components/ResultsTableMenu.go b/components/ResultsTableMenu.go index c907fca..858d270 100644 --- a/components/ResultsTableMenu.go +++ b/components/ResultsTableMenu.go @@ -17,11 +17,11 @@ type ResultsTableMenu struct { } var menuItems = []string{ - "Records", - "Columns", - "Constraints", - "Foreign Keys", - "Indexes", + RecordsMenu, + ColumnsMenu, + ConstraintsMenu, + ForeignKeysMenu, + IndexesMenu, } func NewResultsTableMenu() *ResultsTableMenu { @@ -52,11 +52,11 @@ func NewResultsTableMenu() *ResultsTableMenu { size := 15 switch item { - case "Constraints": + case ConstraintsMenu: size = 19 - case "Foreign Keys": + case ForeignKeysMenu: size = 20 - case "Indexes": + case IndexesMenu: size = 16 } diff --git a/components/SQLEditor.go b/components/SQLEditor.go index b3e4389..dc18426 100644 --- a/components/SQLEditor.go +++ b/components/SQLEditor.go @@ -38,10 +38,10 @@ func NewSQLEditor() *SQLEditor { command := app.Keymaps.Group(app.EditorGroup).Resolve(event) if command == commands.Execute { - sqlEditor.Publish("Query", sqlEditor.GetText()) + sqlEditor.Publish(QuerySQLEditor, sqlEditor.GetText()) return nil } else if command == commands.UnfocusEditor { - sqlEditor.Publish("Escape", "") + sqlEditor.Publish(EscapeSQLEditor, "") } else if command == commands.OpenInExternalEditor && runtime.GOOS == "linux" { // ----- THIS IS A LINUX-ONLY FEATURE, for now diff --git a/components/Sidebar.go b/components/Sidebar.go index 91edd66..2fe5ebb 100644 --- a/components/Sidebar.go +++ b/components/Sidebar.go @@ -140,9 +140,9 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { switch command { case commands.UnfocusSidebar: - sidebar.Publish(models.StateChange{Key: "Unfocusing", Value: nil}) + sidebar.Publish(models.StateChange{Key: UnfocusingSidebar, Value: nil}) case commands.ToggleSidebar: - sidebar.Publish(models.StateChange{Key: "Toggling", Value: nil}) + sidebar.Publish(models.StateChange{Key: TogglingSidebar, Value: nil}) case commands.MoveDown: sidebar.FocusNextField() case commands.MoveUp: @@ -152,7 +152,7 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { case commands.GotoEnd: sidebar.FocusLastField() case commands.Edit: - sidebar.Publish(models.StateChange{Key: "Editing", Value: true}) + sidebar.Publish(models.StateChange{Key: EditingSidebar, Value: true}) currentItemIndex := sidebar.GetCurrentFieldIndex() item := sidebar.Fields[currentItemIndex] @@ -165,13 +165,13 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { case commands.CommitEdit: sidebar.SetInputCapture(sidebar.inputCapture) sidebar.SetDisabledStyles(item) - sidebar.Publish(models.StateChange{Key: "Editing", Value: false}) + sidebar.Publish(models.StateChange{Key: EditingSidebar, Value: false}) return nil case commands.DiscardEdit: sidebar.SetInputCapture(sidebar.inputCapture) sidebar.SetDisabledStyles(item) item.SetText(text, true) - sidebar.Publish(models.StateChange{Key: "Editing", Value: false}) + sidebar.Publish(models.StateChange{Key: EditingSidebar, Value: false}) return nil } diff --git a/components/Tree.go b/components/Tree.go index 8d1e743..0c5148e 100644 --- a/components/Tree.go +++ b/components/Tree.go @@ -388,7 +388,7 @@ func (tree *Tree) GetIsFiltering() bool { func (tree *Tree) SetSelectedDatabase(database string) { tree.state.selectedDatabase = database tree.Publish(models.StateChange{ - Key: "SelectedDatabase", + Key: SelectedDatabaseTree, Value: database, }) } @@ -396,7 +396,7 @@ func (tree *Tree) SetSelectedDatabase(database string) { func (tree *Tree) SetSelectedTable(table string) { tree.state.selectedTable = table tree.Publish(models.StateChange{ - Key: "SelectedTable", + Key: SelectedTableTree, Value: table, }) } @@ -404,7 +404,7 @@ func (tree *Tree) SetSelectedTable(table string) { func (tree *Tree) SetIsFiltering(isFiltering bool) { tree.state.isFiltering = isFiltering tree.Publish(models.StateChange{ - Key: "IsFiltering", + Key: IsFilteringTree, Value: isFiltering, }) } diff --git a/components/constants.go b/components/constants.go index 101f2f2..334ca8b 100644 --- a/components/constants.go +++ b/components/constants.go @@ -4,8 +4,67 @@ import "github.com/jorgerojas26/lazysql/app" var App = app.App +// Pages const ( - EditorTabName string = "Editor" - HelpPageName string = "Help" + // General + HelpPageName string = "Help" + ConfirmationPageName string = "Confirmation" + ConnectionsPageName string = "Connections" + + // Results table + TablePageName string = "Table" + TableErrorPageName string = "TableError" + TableLoadingPageName string = "TableLoading" + TableEditorTablePageName string = "TableEditorTable" + TableEditorResultsInfoPageName string = "TableEditorResultsInfo" + TableEditCellPageName string = "TableEditCell" + + // Sidebar SidebarPageName string = "Sidebar" + + // Connections + ConnectionsSelectionPageName string = "ConnectionsSelection" + ConnectionsFormPageName string = "ConnectionsForm" +) + +// Tabs +const ( + EditorTabName string = "Editor" +) + +// Events +const ( + EditingSidebar string = "EditingSidebar" + UnfocusingSidebar string = "UnfocusingSidebar" + TogglingSidebar string = "TogglingSidebar" + + QuerySQLEditor string = "Query" + EscapeSQLEditor string = "Escape" + + FilteringResultsTable string = "FilteringResultsTable" + + SelectedDatabaseTree string = "SelectedDatabase" + SelectedTableTree string = "SelectedTable" + IsFilteringTree string = "IsFiltering" +) + +// Results table menu items +const ( + RecordsMenu string = "Records" + ColumnsMenu string = "Columns" + ConstraintsMenu string = "Constraints" + ForeignKeysMenu string = "Foreign Keys" + IndexesMenu string = "Indexes" +) + +// Actions +const ( + NewConnection string = "NewConnection" + EditConnection string = "EditConnection" +) + +// Misc (until i find a better name) +const ( + FocusedWrapperLeft string = "left" + FocusedWrapperRight string = "right" ) diff --git a/drivers/constants.go b/drivers/constants.go index 5f46aaa..86c7691 100644 --- a/drivers/constants.go +++ b/drivers/constants.go @@ -3,3 +3,10 @@ package drivers const ( DefaultRowLimit = 300 ) + +// Drivers +const ( + MySQLDriver string = "mysql" + PostgresDriver string = "postgres" + SQLiteDriver string = "sqlite3" +) diff --git a/drivers/mysql.go b/drivers/mysql.go index 1c47412..1b27bdd 100644 --- a/drivers/mysql.go +++ b/drivers/mysql.go @@ -22,7 +22,7 @@ func (db *MySQL) TestConnection(urlstr string) (err error) { } func (db *MySQL) Connect(urlstr string) (err error) { - db.SetProvider("mysql") + db.SetProvider(MySQLDriver) db.Connection, err = dburl.Open(urlstr) if err != nil { diff --git a/drivers/postgres.go b/drivers/postgres.go index a3fede9..609492f 100644 --- a/drivers/postgres.go +++ b/drivers/postgres.go @@ -31,7 +31,7 @@ func (db *Postgres) TestConnection(urlstr string) error { } func (db *Postgres) Connect(urlstr string) (err error) { - db.SetProvider("postgres") + db.SetProvider(PostgresDriver) db.Connection, err = dburl.Open(urlstr) if err != nil { diff --git a/drivers/sqlite.go b/drivers/sqlite.go index 24fc476..9e674b8 100644 --- a/drivers/sqlite.go +++ b/drivers/sqlite.go @@ -23,7 +23,7 @@ func (db *SQLite) TestConnection(urlstr string) (err error) { } func (db *SQLite) Connect(urlstr string) (err error) { - db.SetProvider("sqlite3") + db.SetProvider(SQLiteDriver) db.Connection, err = sql.Open("sqlite", urlstr) if err != nil { From 9792cca7a81dbe31b12b776d836ba69f4530fe95 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sun, 8 Sep 2024 12:34:06 -0400 Subject: [PATCH 10/19] chore: make constants private --- components/ConnectionForm.go | 8 ++-- components/ConnectionPage.go | 4 +- components/ConnectionSelection.go | 12 +++--- components/HelpModal.go | 2 +- components/Home.go | 30 +++++++-------- components/Pages.go | 2 +- components/ResultTableFilter.go | 2 +- components/ResultsTable.go | 50 ++++++++++++------------- components/ResultsTableMenu.go | 16 ++++---- components/SQLEditor.go | 4 +- components/Sidebar.go | 10 ++--- components/Tree.go | 6 +-- components/constants.go | 62 +++++++++++++++---------------- 13 files changed, 104 insertions(+), 104 deletions(-) diff --git a/components/ConnectionForm.go b/components/ConnectionForm.go index 127769c..a6a1ac3 100644 --- a/components/ConnectionForm.go +++ b/components/ConnectionForm.go @@ -77,7 +77,7 @@ func NewConnectionForm(connectionPages *models.ConnectionPages) *ConnectionForm func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages) func(event *tcell.EventKey) *tcell.EventKey { return func(event *tcell.EventKey) *tcell.EventKey { if event.Key() == tcell.KeyEsc { - connectionPages.SwitchToPage(ConnectionsPageName) + connectionPages.SwitchToPage(connectionsPageName) } else if event.Key() == tcell.KeyF1 || event.Key() == tcell.KeyEnter { connectionName := form.GetFormItem(0).(*tview.InputField).GetText() @@ -111,7 +111,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages } switch form.Action { - case NewConnection: + case newConnection: newDatabases = append(databases, parsedDatabaseData) err := helpers.SaveConnectionConfig(newDatabases) @@ -120,7 +120,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages return event } - case EditConnection: + case editConnection: newDatabases = make([]models.Connection, len(databases)) row, _ := ConnectionListTable.GetSelection() @@ -151,7 +151,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages } ConnectionListTable.SetConnections(newDatabases) - connectionPages.SwitchToPage(ConnectionsPageName) + connectionPages.SwitchToPage(connectionsPageName) } else if event.Key() == tcell.KeyF2 { connectionString := form.GetFormItem(1).(*tview.InputField).GetText() diff --git a/components/ConnectionPage.go b/components/ConnectionPage.go index aca3c33..6656dd5 100644 --- a/components/ConnectionPage.go +++ b/components/ConnectionPage.go @@ -32,8 +32,8 @@ func NewConnectionPages() *models.ConnectionPages { connectionForm := NewConnectionForm(cp) connectionSelection := NewConnectionSelection(connectionForm, cp) - cp.AddPage(ConnectionsSelectionPageName, connectionSelection.Flex, true, true) - cp.AddPage(ConnectionsFormPageName, connectionForm.Flex, true, false) + cp.AddPage(connectionsSelectionPageName, connectionSelection.Flex, true, true) + cp.AddPage(connectionsFormPageName, connectionForm.Flex, true, false) return cp } diff --git a/components/ConnectionSelection.go b/components/ConnectionSelection.go index 6c9232b..9de76d2 100644 --- a/components/ConnectionSelection.go +++ b/components/ConnectionSelection.go @@ -88,18 +88,18 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod case commands.Connect: go cs.Connect(selectedConnection) case commands.EditConnection: - connectionPages.SwitchToPage(ConnectionsFormPageName) + connectionPages.SwitchToPage(connectionsFormPageName) connectionForm.GetFormItemByLabel("Name").(*tview.InputField).SetText(selectedConnection.Name) connectionForm.GetFormItemByLabel("URL").(*tview.InputField).SetText(selectedConnection.URL) connectionForm.StatusText.SetText("") - connectionForm.SetAction(EditConnection) + connectionForm.SetAction(editConnection) return nil case commands.DeleteConnection: confirmationModal := NewConfirmationModal("") confirmationModal.SetDoneFunc(func(_ int, buttonLabel string) { - MainPages.RemovePage(ConfirmationPageName) + MainPages.RemovePage(confirmationPageName) confirmationModal = nil if buttonLabel == "Yes" { @@ -115,7 +115,7 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod } }) - MainPages.AddPage(ConfirmationPageName, confirmationModal, true, true) + MainPages.AddPage(confirmationPageName, confirmationModal, true, true) return nil } @@ -123,11 +123,11 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod switch command { case commands.NewConnection: - connectionForm.SetAction(NewConnection) + connectionForm.SetAction(newConnection) connectionForm.GetFormItemByLabel("Name").(*tview.InputField).SetText("") connectionForm.GetFormItemByLabel("URL").(*tview.InputField).SetText("") connectionForm.StatusText.SetText("") - connectionPages.SwitchToPage(ConnectionsFormPageName) + connectionPages.SwitchToPage(connectionsFormPageName) case commands.Quit: if wrapper.HasFocus() { app.App.Stop() diff --git a/components/HelpModal.go b/components/HelpModal.go index f6e905f..baec49f 100644 --- a/components/HelpModal.go +++ b/components/HelpModal.go @@ -75,7 +75,7 @@ func NewHelpModal() *HelpModal { table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { command := app.Keymaps.Group(app.HomeGroup).Resolve(event) if command == commands.Quit || command == commands.HelpPopup { - MainPages.RemovePage(HelpPageName) + MainPages.RemovePage(helpPageName) } return event }) diff --git a/components/Home.go b/components/Home.go index 1226b98..9e6da5e 100644 --- a/components/Home.go +++ b/components/Home.go @@ -66,7 +66,7 @@ func NewHomePage(connection models.Connection, dbdriver drivers.Driver) *Home { home.SetInputCapture(home.homeInputCapture) home.SetFocusFunc(func() { - if home.FocusedWrapper == FocusedWrapperLeft || home.FocusedWrapper == "" { + if home.FocusedWrapper == focusedWrapperLeft || home.FocusedWrapper == "" { home.focusLeftWrapper() } else { home.focusRightWrapper() @@ -82,7 +82,7 @@ func (home *Home) subscribeToTreeChanges() { for stateChange := range ch { switch stateChange.Key { - case SelectedTableTree: + case selectedTableTree: databaseName := home.Tree.GetSelectedDatabase() tableName := stateChange.Value.(string) @@ -117,7 +117,7 @@ func (home *Home) subscribeToTreeChanges() { } app.App.ForceDraw() - case IsFilteringTree: + case isFilteringTree: isFiltering := stateChange.Value.(bool) if isFiltering { home.SetInputCapture(nil) @@ -140,7 +140,7 @@ func (home *Home) focusRightWrapper() { home.focusTab(tab) } - home.FocusedWrapper = FocusedWrapperRight + home.FocusedWrapper = focusedWrapperRight } func (home *Home) focusTab(tab *Tab) { @@ -166,7 +166,7 @@ func (home *Home) focusTab(tab *Tab) { App.SetFocus(table) } - if tab.Name == EditorTabName { + if tab.Name == editorTabName { home.HelpStatus.SetStatusOnEditorView() } else { home.HelpStatus.SetStatusOnTableView() @@ -193,7 +193,7 @@ func (home *Home) focusLeftWrapper() { App.SetFocus(home.Tree) - home.FocusedWrapper = FocusedWrapperLeft + home.FocusedWrapper = focusedWrapperLeft } func (home *Home) rightWrapperInputCapture(event *tcell.EventKey) *tcell.EventKey { @@ -272,22 +272,22 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { switch command { case commands.MoveLeft: - if table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && home.FocusedWrapper == FocusedWrapperRight { + if table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && home.FocusedWrapper == focusedWrapperRight { home.focusLeftWrapper() } case commands.MoveRight: - if table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && home.FocusedWrapper == FocusedWrapperLeft { + if table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && home.FocusedWrapper == focusedWrapperLeft { home.focusRightWrapper() } case commands.SwitchToEditorView: - tab := home.TabbedPane.GetTabByName(EditorTabName) + tab := home.TabbedPane.GetTabByName(editorTabName) if tab != nil { - home.TabbedPane.SwitchToTabByName(EditorTabName) + home.TabbedPane.SwitchToTabByName(editorTabName) tab.Content.SetIsFiltering(true) } else { tableWithEditor := NewResultsTable(&home.ListOfDbChanges, home.Tree, home.DBDriver).WithEditor() - home.TabbedPane.AppendTab(EditorTabName, tableWithEditor, EditorTabName) + home.TabbedPane.AppendTab(editorTabName, tableWithEditor, editorTabName) tableWithEditor.SetIsFiltering(true) } home.HelpStatus.SetStatusOnEditorView() @@ -295,7 +295,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { App.ForceDraw() case commands.SwitchToConnectionsView: if (table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && !table.GetIsLoading()) || table == nil { - MainPages.SwitchToPage(ConnectionsPageName) + MainPages.SwitchToPage(connectionsPageName) } case commands.Quit: if tab != nil { @@ -312,7 +312,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { confirmationModal := NewConfirmationModal("") confirmationModal.SetDoneFunc(func(_ int, buttonLabel string) { - MainPages.RemovePage(ConfirmationPageName) + MainPages.RemovePage(confirmationPageName) confirmationModal = nil if buttonLabel == "Yes" { @@ -332,7 +332,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { } }) - MainPages.AddPage(ConfirmationPageName, confirmationModal, true, true) + MainPages.AddPage(confirmationPageName, confirmationModal, true, true) } case commands.HelpPopup: if table == nil || !table.GetIsEditing() { @@ -345,7 +345,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { // } // return event // }) - MainPages.AddPage(HelpPageName, home.HelpModal, true, true) + MainPages.AddPage(helpPageName, home.HelpModal, true, true) } } diff --git a/components/Pages.go b/components/Pages.go index 2055685..c09aa87 100644 --- a/components/Pages.go +++ b/components/Pages.go @@ -8,5 +8,5 @@ var MainPages = tview.NewPages() func init() { MainPages.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) - MainPages.AddPage(ConnectionsPageName, NewConnectionPages().Flex, true, true) + MainPages.AddPage(connectionsPageName, NewConnectionPages().Flex, true, true) } diff --git a/components/ResultTableFilter.go b/components/ResultTableFilter.go index f9e8bf5..1a1dcdb 100644 --- a/components/ResultTableFilter.go +++ b/components/ResultTableFilter.go @@ -67,7 +67,7 @@ func (filter *ResultsTableFilter) Subscribe() chan models.StateChange { func (filter *ResultsTableFilter) Publish(message string) { for _, sub := range filter.subscribers { sub <- models.StateChange{ - Key: FilteringResultsTable, + Key: filteringResultsTable, Value: message, } } diff --git a/components/ResultsTable.go b/components/ResultsTable.go index c547518..4c06654 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -89,9 +89,9 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver loadingModal.SetTextColor(tview.Styles.SecondaryTextColor) pages := tview.NewPages() - pages.AddPage(TablePageName, wrapper, true, true) - pages.AddPage(TableErrorPageName, errorModal, true, false) - pages.AddPage(TableLoadingPageName, loadingModal, false, false) + pages.AddPage(tablePageName, wrapper, true, true) + pages.AddPage(tableErrorPageName, errorModal, true, false) + pages.AddPage(tableLoadingPageName, loadingModal, false, false) pagination := NewPagination() @@ -116,7 +116,7 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver table.SetFixed(1, 0) table.SetInputCapture(table.tableInputCapture) table.SetSelectedStyle(tcell.StyleDefault.Background(tview.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) - table.Page.AddPage(SidebarPageName, table.Sidebar, false, false) + table.Page.AddPage(sidebarPageName, table.Sidebar, false, false) table.SetSelectionChangedFunc(func(_, _ int) { if table.GetShowSidebar() { @@ -176,8 +176,8 @@ func (table *ResultsTable) WithEditor() *ResultsTable { resultsInfoText.SetTextColor(tview.Styles.PrimaryTextColor) resultsInfoWrapper.AddItem(resultsInfoText, 3, 0, false) - editorPages.AddPage(TableEditorTablePageName, tableWrapper, true, false) - editorPages.AddPage(TableEditorResultsInfoPageName, resultsInfoWrapper, true, true) + editorPages.AddPage(tableEditorTablePageName, tableWrapper, true, false) + editorPages.AddPage(tableEditorResultsInfoPageName, resultsInfoWrapper, true, true) table.EditorPages = editorPages table.ResultsInfo = resultsInfoText @@ -193,7 +193,7 @@ func (table *ResultsTable) subscribeToTreeChanges() { ch := table.Tree.Subscribe() for stateChange := range ch { - if stateChange.Key == SelectedDatabaseTree { + if stateChange.Key == selectedDatabaseTree { table.SetDatabaseName(stateChange.Value.(string)) } } @@ -204,13 +204,13 @@ func (table *ResultsTable) subscribeToSidebarChanges() { for stateChange := range ch { switch stateChange.Key { - case EditingSidebar: + case editingSidebar: editing := stateChange.Value.(bool) table.SetIsEditing(editing) - case UnfocusingSidebar: + case unfocusingSidebar: App.SetFocus(table) App.ForceDraw() - case TogglingSidebar: + case togglingSidebar: table.ShowSidebar(false) App.ForceDraw() } @@ -493,7 +493,7 @@ func (table *ResultsTable) subscribeToFilterChanges() { for stateChange := range ch { switch stateChange.Key { - case FilteringResultsTable: + case filteringResultsTable: if stateChange.Value != "" { rows := table.FetchRecords(nil) @@ -532,7 +532,7 @@ func (table *ResultsTable) subscribeToEditorChanges() { for stateChange := range ch { switch stateChange.Key { - case QuerySQLEditor: + case querySQLEditor: query := stateChange.Value.(string) if query != "" { queryLower := strings.ToLower(query) @@ -568,7 +568,7 @@ func (table *ResultsTable) subscribeToEditorChanges() { } table.SetLoading(false) } - table.EditorPages.SwitchToPage(TablePageName) + table.EditorPages.SwitchToPage(tablePageName) App.Draw() } else { table.SetRecords([][]string{}) @@ -584,13 +584,13 @@ func (table *ResultsTable) subscribeToEditorChanges() { } else { table.SetResultsInfo(result) table.SetLoading(false) - table.EditorPages.SwitchToPage(TableEditorResultsInfoPageName) + table.EditorPages.SwitchToPage(tableEditorResultsInfoPageName) App.SetFocus(table.Editor) App.Draw() } } } - case EscapeSQLEditor: + case escapeSQLEditor: table.SetIsFiltering(false) App.SetFocus(table) table.HighlightTable() @@ -704,7 +704,7 @@ func (table *ResultsTable) SetError(err string, done func()) { table.Error.SetText(err) table.Error.SetDoneFunc(func(_ int, _ string) { table.state.error = "" - table.Page.HidePage(TableErrorPageName) + table.Page.HidePage(tableErrorPageName) if table.GetIsFiltering() { if table.Editor != nil { App.SetFocus(table.Editor) @@ -718,7 +718,7 @@ func (table *ResultsTable) SetError(err string, done func()) { done() } }) - table.Page.ShowPage(TableErrorPageName) + table.Page.ShowPage(tableErrorPageName) App.SetFocus(table.Error) App.ForceDraw() } @@ -731,7 +731,7 @@ func (table *ResultsTable) SetLoading(show bool) { defer func() { if r := recover(); r != nil { logger.Error("ResultsTable.go:800 => Recovered from panic", map[string]any{"error": r}) - _ = table.Page.HidePage(TableLoadingPageName) + _ = table.Page.HidePage(tableLoadingPageName) if table.state.error != "" { App.SetFocus(table.Error) } else { @@ -742,11 +742,11 @@ func (table *ResultsTable) SetLoading(show bool) { table.state.isLoading = show if show { - table.Page.ShowPage(TableLoadingPageName) + table.Page.ShowPage(tableLoadingPageName) App.SetFocus(table.Loading) App.ForceDraw() } else { - table.Page.HidePage(TableLoadingPageName) + table.Page.HidePage(tableLoadingPageName) if table.state.error != "" { App.SetFocus(table.Error) } else { @@ -909,7 +909,7 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV if key == tcell.KeyEnter || key == tcell.KeyEscape { table.SetInputCapture(table.tableInputCapture) - table.Page.RemovePage(TableEditCellPageName) + table.Page.RemovePage(tableEditCellPageName) App.SetFocus(table) } @@ -920,7 +920,7 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV x, y, width := cell.GetLastPosition() inputField.SetRect(x, y, width+1, 1) - table.Page.AddPage(TableEditCellPageName, inputField, false, true) + table.Page.AddPage(tableEditCellPageName, inputField, false, true) App.SetFocus(inputField) } @@ -1271,10 +1271,10 @@ func (table *ResultsTable) ShowSidebar(show bool) { if show { table.UpdateSidebar() - table.Page.SendToFront(SidebarPageName) - table.Page.ShowPage(SidebarPageName) + table.Page.SendToFront(sidebarPageName) + table.Page.ShowPage(sidebarPageName) } else { - table.Page.HidePage(SidebarPageName) + table.Page.HidePage(sidebarPageName) App.SetFocus(table) } } diff --git a/components/ResultsTableMenu.go b/components/ResultsTableMenu.go index 858d270..2d7f9cc 100644 --- a/components/ResultsTableMenu.go +++ b/components/ResultsTableMenu.go @@ -17,11 +17,11 @@ type ResultsTableMenu struct { } var menuItems = []string{ - RecordsMenu, - ColumnsMenu, - ConstraintsMenu, - ForeignKeysMenu, - IndexesMenu, + recordsMenu, + columnsMenu, + constraintsMenu, + foreignKeysMenu, + indexesMenu, } func NewResultsTableMenu() *ResultsTableMenu { @@ -52,11 +52,11 @@ func NewResultsTableMenu() *ResultsTableMenu { size := 15 switch item { - case ConstraintsMenu: + case constraintsMenu: size = 19 - case ForeignKeysMenu: + case foreignKeysMenu: size = 20 - case IndexesMenu: + case indexesMenu: size = 16 } diff --git a/components/SQLEditor.go b/components/SQLEditor.go index dc18426..2f66f82 100644 --- a/components/SQLEditor.go +++ b/components/SQLEditor.go @@ -38,10 +38,10 @@ func NewSQLEditor() *SQLEditor { command := app.Keymaps.Group(app.EditorGroup).Resolve(event) if command == commands.Execute { - sqlEditor.Publish(QuerySQLEditor, sqlEditor.GetText()) + sqlEditor.Publish(querySQLEditor, sqlEditor.GetText()) return nil } else if command == commands.UnfocusEditor { - sqlEditor.Publish(EscapeSQLEditor, "") + sqlEditor.Publish(escapeSQLEditor, "") } else if command == commands.OpenInExternalEditor && runtime.GOOS == "linux" { // ----- THIS IS A LINUX-ONLY FEATURE, for now diff --git a/components/Sidebar.go b/components/Sidebar.go index 2fe5ebb..eea2558 100644 --- a/components/Sidebar.go +++ b/components/Sidebar.go @@ -140,9 +140,9 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { switch command { case commands.UnfocusSidebar: - sidebar.Publish(models.StateChange{Key: UnfocusingSidebar, Value: nil}) + sidebar.Publish(models.StateChange{Key: unfocusingSidebar, Value: nil}) case commands.ToggleSidebar: - sidebar.Publish(models.StateChange{Key: TogglingSidebar, Value: nil}) + sidebar.Publish(models.StateChange{Key: togglingSidebar, Value: nil}) case commands.MoveDown: sidebar.FocusNextField() case commands.MoveUp: @@ -152,7 +152,7 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { case commands.GotoEnd: sidebar.FocusLastField() case commands.Edit: - sidebar.Publish(models.StateChange{Key: EditingSidebar, Value: true}) + sidebar.Publish(models.StateChange{Key: editingSidebar, Value: true}) currentItemIndex := sidebar.GetCurrentFieldIndex() item := sidebar.Fields[currentItemIndex] @@ -165,13 +165,13 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { case commands.CommitEdit: sidebar.SetInputCapture(sidebar.inputCapture) sidebar.SetDisabledStyles(item) - sidebar.Publish(models.StateChange{Key: EditingSidebar, Value: false}) + sidebar.Publish(models.StateChange{Key: editingSidebar, Value: false}) return nil case commands.DiscardEdit: sidebar.SetInputCapture(sidebar.inputCapture) sidebar.SetDisabledStyles(item) item.SetText(text, true) - sidebar.Publish(models.StateChange{Key: EditingSidebar, Value: false}) + sidebar.Publish(models.StateChange{Key: editingSidebar, Value: false}) return nil } diff --git a/components/Tree.go b/components/Tree.go index 0c5148e..97327dd 100644 --- a/components/Tree.go +++ b/components/Tree.go @@ -388,7 +388,7 @@ func (tree *Tree) GetIsFiltering() bool { func (tree *Tree) SetSelectedDatabase(database string) { tree.state.selectedDatabase = database tree.Publish(models.StateChange{ - Key: SelectedDatabaseTree, + Key: selectedDatabaseTree, Value: database, }) } @@ -396,7 +396,7 @@ func (tree *Tree) SetSelectedDatabase(database string) { func (tree *Tree) SetSelectedTable(table string) { tree.state.selectedTable = table tree.Publish(models.StateChange{ - Key: SelectedTableTree, + Key: selectedTableTree, Value: table, }) } @@ -404,7 +404,7 @@ func (tree *Tree) SetSelectedTable(table string) { func (tree *Tree) SetIsFiltering(isFiltering bool) { tree.state.isFiltering = isFiltering tree.Publish(models.StateChange{ - Key: IsFilteringTree, + Key: isFilteringTree, Value: isFiltering, }) } diff --git a/components/constants.go b/components/constants.go index 334ca8b..4589195 100644 --- a/components/constants.go +++ b/components/constants.go @@ -7,64 +7,64 @@ var App = app.App // Pages const ( // General - HelpPageName string = "Help" - ConfirmationPageName string = "Confirmation" - ConnectionsPageName string = "Connections" + helpPageName string = "Help" + confirmationPageName string = "Confirmation" + connectionsPageName string = "Connections" // Results table - TablePageName string = "Table" - TableErrorPageName string = "TableError" - TableLoadingPageName string = "TableLoading" - TableEditorTablePageName string = "TableEditorTable" - TableEditorResultsInfoPageName string = "TableEditorResultsInfo" - TableEditCellPageName string = "TableEditCell" + tablePageName string = "Table" + tableErrorPageName string = "TableError" + tableLoadingPageName string = "TableLoading" + tableEditorTablePageName string = "TableEditorTable" + tableEditorResultsInfoPageName string = "TableEditorResultsInfo" + tableEditCellPageName string = "TableEditCell" // Sidebar - SidebarPageName string = "Sidebar" + sidebarPageName string = "Sidebar" // Connections - ConnectionsSelectionPageName string = "ConnectionsSelection" - ConnectionsFormPageName string = "ConnectionsForm" + connectionsSelectionPageName string = "ConnectionsSelection" + connectionsFormPageName string = "ConnectionsForm" ) // Tabs const ( - EditorTabName string = "Editor" + editorTabName string = "Editor" ) // Events const ( - EditingSidebar string = "EditingSidebar" - UnfocusingSidebar string = "UnfocusingSidebar" - TogglingSidebar string = "TogglingSidebar" + editingSidebar string = "EditingSidebar" + unfocusingSidebar string = "UnfocusingSidebar" + togglingSidebar string = "TogglingSidebar" - QuerySQLEditor string = "Query" - EscapeSQLEditor string = "Escape" + querySQLEditor string = "Query" + escapeSQLEditor string = "Escape" - FilteringResultsTable string = "FilteringResultsTable" + filteringResultsTable string = "FilteringResultsTable" - SelectedDatabaseTree string = "SelectedDatabase" - SelectedTableTree string = "SelectedTable" - IsFilteringTree string = "IsFiltering" + selectedDatabaseTree string = "SelectedDatabase" + selectedTableTree string = "SelectedTable" + isFilteringTree string = "IsFiltering" ) // Results table menu items const ( - RecordsMenu string = "Records" - ColumnsMenu string = "Columns" - ConstraintsMenu string = "Constraints" - ForeignKeysMenu string = "Foreign Keys" - IndexesMenu string = "Indexes" + recordsMenu string = "Records" + columnsMenu string = "Columns" + constraintsMenu string = "Constraints" + foreignKeysMenu string = "Foreign Keys" + indexesMenu string = "Indexes" ) // Actions const ( - NewConnection string = "NewConnection" - EditConnection string = "EditConnection" + newConnection string = "NewConnection" + editConnection string = "EditConnection" ) // Misc (until i find a better name) const ( - FocusedWrapperLeft string = "left" - FocusedWrapperRight string = "right" + focusedWrapperLeft string = "left" + focusedWrapperRight string = "right" ) From 5bfeb76e0fa4c6c42e655c5b5e5aed1851d6d649 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sun, 8 Sep 2024 12:42:30 -0400 Subject: [PATCH 11/19] chore: make constants have a prefix --- components/ConnectionForm.go | 14 ++++---- components/ConnectionPage.go | 4 +-- components/ConnectionSelection.go | 18 +++++----- components/HelpModal.go | 2 +- components/Home.go | 20 +++++------ components/Pages.go | 2 +- components/ResultTableFilter.go | 2 +- components/ResultsTable.go | 56 ++++++++++++++--------------- components/ResultsTableMenu.go | 16 ++++----- components/SQLEditor.go | 4 +-- components/Sidebar.go | 10 +++--- components/Tree.go | 6 ++-- components/constants.go | 58 +++++++++++++++---------------- drivers/constants.go | 6 ++-- drivers/mysql.go | 2 +- drivers/postgres.go | 2 +- drivers/sqlite.go | 2 +- 17 files changed, 112 insertions(+), 112 deletions(-) diff --git a/components/ConnectionForm.go b/components/ConnectionForm.go index a6a1ac3..c168017 100644 --- a/components/ConnectionForm.go +++ b/components/ConnectionForm.go @@ -77,7 +77,7 @@ func NewConnectionForm(connectionPages *models.ConnectionPages) *ConnectionForm func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages) func(event *tcell.EventKey) *tcell.EventKey { return func(event *tcell.EventKey) *tcell.EventKey { if event.Key() == tcell.KeyEsc { - connectionPages.SwitchToPage(connectionsPageName) + connectionPages.SwitchToPage(pageNameConnections) } else if event.Key() == tcell.KeyF1 || event.Key() == tcell.KeyEnter { connectionName := form.GetFormItem(0).(*tview.InputField).GetText() @@ -111,7 +111,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages } switch form.Action { - case newConnection: + case actionNewConnection: newDatabases = append(databases, parsedDatabaseData) err := helpers.SaveConnectionConfig(newDatabases) @@ -120,7 +120,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages return event } - case editConnection: + case actionEditConnection: newDatabases = make([]models.Connection, len(databases)) row, _ := ConnectionListTable.GetSelection() @@ -151,7 +151,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages } ConnectionListTable.SetConnections(newDatabases) - connectionPages.SwitchToPage(connectionsPageName) + connectionPages.SwitchToPage(pageNameConnections) } else if event.Key() == tcell.KeyF2 { connectionString := form.GetFormItem(1).(*tview.InputField).GetText() @@ -173,11 +173,11 @@ func (form *ConnectionForm) testConnection(connectionString string) { var db drivers.Driver switch parsed.Driver { - case drivers.MySQLDriver: + case drivers.DriverMySQL: db = &drivers.MySQL{} - case drivers.PostgresDriver: + case drivers.DriverPostgres: db = &drivers.Postgres{} - case drivers.SQLiteDriver: + case drivers.DriverSqlite: db = &drivers.SQLite{} } diff --git a/components/ConnectionPage.go b/components/ConnectionPage.go index 6656dd5..8fc7ad9 100644 --- a/components/ConnectionPage.go +++ b/components/ConnectionPage.go @@ -32,8 +32,8 @@ func NewConnectionPages() *models.ConnectionPages { connectionForm := NewConnectionForm(cp) connectionSelection := NewConnectionSelection(connectionForm, cp) - cp.AddPage(connectionsSelectionPageName, connectionSelection.Flex, true, true) - cp.AddPage(connectionsFormPageName, connectionForm.Flex, true, false) + cp.AddPage(pageNameConnectionSelection, connectionSelection.Flex, true, true) + cp.AddPage(pageNameConnectionForm, connectionForm.Flex, true, false) return cp } diff --git a/components/ConnectionSelection.go b/components/ConnectionSelection.go index 9de76d2..301b37b 100644 --- a/components/ConnectionSelection.go +++ b/components/ConnectionSelection.go @@ -88,18 +88,18 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod case commands.Connect: go cs.Connect(selectedConnection) case commands.EditConnection: - connectionPages.SwitchToPage(connectionsFormPageName) + connectionPages.SwitchToPage(pageNameConnectionForm) connectionForm.GetFormItemByLabel("Name").(*tview.InputField).SetText(selectedConnection.Name) connectionForm.GetFormItemByLabel("URL").(*tview.InputField).SetText(selectedConnection.URL) connectionForm.StatusText.SetText("") - connectionForm.SetAction(editConnection) + connectionForm.SetAction(actionEditConnection) return nil case commands.DeleteConnection: confirmationModal := NewConfirmationModal("") confirmationModal.SetDoneFunc(func(_ int, buttonLabel string) { - MainPages.RemovePage(confirmationPageName) + MainPages.RemovePage(pageNameConfirmation) confirmationModal = nil if buttonLabel == "Yes" { @@ -115,7 +115,7 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod } }) - MainPages.AddPage(confirmationPageName, confirmationModal, true, true) + MainPages.AddPage(pageNameConfirmation, confirmationModal, true, true) return nil } @@ -123,11 +123,11 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod switch command { case commands.NewConnection: - connectionForm.SetAction(newConnection) + connectionForm.SetAction(actionNewConnection) connectionForm.GetFormItemByLabel("Name").(*tview.InputField).SetText("") connectionForm.GetFormItemByLabel("URL").(*tview.InputField).SetText("") connectionForm.StatusText.SetText("") - connectionPages.SwitchToPage(connectionsFormPageName) + connectionPages.SwitchToPage(pageNameConnectionForm) case commands.Quit: if wrapper.HasFocus() { app.App.Stop() @@ -151,11 +151,11 @@ func (cs *ConnectionSelection) Connect(connection models.Connection) { var newDbDriver drivers.Driver switch connection.Provider { - case drivers.MySQLDriver: + case drivers.DriverMySQL: newDbDriver = &drivers.MySQL{} - case drivers.PostgresDriver: + case drivers.DriverPostgres: newDbDriver = &drivers.Postgres{} - case drivers.SQLiteDriver: + case drivers.DriverSqlite: newDbDriver = &drivers.SQLite{} } diff --git a/components/HelpModal.go b/components/HelpModal.go index baec49f..7bbf58e 100644 --- a/components/HelpModal.go +++ b/components/HelpModal.go @@ -75,7 +75,7 @@ func NewHelpModal() *HelpModal { table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { command := app.Keymaps.Group(app.HomeGroup).Resolve(event) if command == commands.Quit || command == commands.HelpPopup { - MainPages.RemovePage(helpPageName) + MainPages.RemovePage(pageNameHelp) } return event }) diff --git a/components/Home.go b/components/Home.go index 9e6da5e..a4400c1 100644 --- a/components/Home.go +++ b/components/Home.go @@ -82,7 +82,7 @@ func (home *Home) subscribeToTreeChanges() { for stateChange := range ch { switch stateChange.Key { - case selectedTableTree: + case eventTreeSelectedTable: databaseName := home.Tree.GetSelectedDatabase() tableName := stateChange.Value.(string) @@ -117,7 +117,7 @@ func (home *Home) subscribeToTreeChanges() { } app.App.ForceDraw() - case isFilteringTree: + case eventTreeIsFiltering: isFiltering := stateChange.Value.(bool) if isFiltering { home.SetInputCapture(nil) @@ -166,7 +166,7 @@ func (home *Home) focusTab(tab *Tab) { App.SetFocus(table) } - if tab.Name == editorTabName { + if tab.Name == tabNameEditor { home.HelpStatus.SetStatusOnEditorView() } else { home.HelpStatus.SetStatusOnTableView() @@ -280,14 +280,14 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { home.focusRightWrapper() } case commands.SwitchToEditorView: - tab := home.TabbedPane.GetTabByName(editorTabName) + tab := home.TabbedPane.GetTabByName(tabNameEditor) if tab != nil { - home.TabbedPane.SwitchToTabByName(editorTabName) + home.TabbedPane.SwitchToTabByName(tabNameEditor) tab.Content.SetIsFiltering(true) } else { tableWithEditor := NewResultsTable(&home.ListOfDbChanges, home.Tree, home.DBDriver).WithEditor() - home.TabbedPane.AppendTab(editorTabName, tableWithEditor, editorTabName) + home.TabbedPane.AppendTab(tabNameEditor, tableWithEditor, tabNameEditor) tableWithEditor.SetIsFiltering(true) } home.HelpStatus.SetStatusOnEditorView() @@ -295,7 +295,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { App.ForceDraw() case commands.SwitchToConnectionsView: if (table != nil && !table.GetIsEditing() && !table.GetIsFiltering() && !table.GetIsLoading()) || table == nil { - MainPages.SwitchToPage(connectionsPageName) + MainPages.SwitchToPage(pageNameConnections) } case commands.Quit: if tab != nil { @@ -312,7 +312,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { confirmationModal := NewConfirmationModal("") confirmationModal.SetDoneFunc(func(_ int, buttonLabel string) { - MainPages.RemovePage(confirmationPageName) + MainPages.RemovePage(pageNameConfirmation) confirmationModal = nil if buttonLabel == "Yes" { @@ -332,7 +332,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { } }) - MainPages.AddPage(confirmationPageName, confirmationModal, true, true) + MainPages.AddPage(pageNameConfirmation, confirmationModal, true, true) } case commands.HelpPopup: if table == nil || !table.GetIsEditing() { @@ -345,7 +345,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey { // } // return event // }) - MainPages.AddPage(helpPageName, home.HelpModal, true, true) + MainPages.AddPage(pageNameHelp, home.HelpModal, true, true) } } diff --git a/components/Pages.go b/components/Pages.go index c09aa87..07222fe 100644 --- a/components/Pages.go +++ b/components/Pages.go @@ -8,5 +8,5 @@ var MainPages = tview.NewPages() func init() { MainPages.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) - MainPages.AddPage(connectionsPageName, NewConnectionPages().Flex, true, true) + MainPages.AddPage(pageNameConnections, NewConnectionPages().Flex, true, true) } diff --git a/components/ResultTableFilter.go b/components/ResultTableFilter.go index 1a1dcdb..97f0a2c 100644 --- a/components/ResultTableFilter.go +++ b/components/ResultTableFilter.go @@ -67,7 +67,7 @@ func (filter *ResultsTableFilter) Subscribe() chan models.StateChange { func (filter *ResultsTableFilter) Publish(message string) { for _, sub := range filter.subscribers { sub <- models.StateChange{ - Key: filteringResultsTable, + Key: eventResultsTableFiltering, Value: message, } } diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 4c06654..77a4acc 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -89,9 +89,9 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver loadingModal.SetTextColor(tview.Styles.SecondaryTextColor) pages := tview.NewPages() - pages.AddPage(tablePageName, wrapper, true, true) - pages.AddPage(tableErrorPageName, errorModal, true, false) - pages.AddPage(tableLoadingPageName, loadingModal, false, false) + pages.AddPage(pageNameTable, wrapper, true, true) + pages.AddPage(pageNameTableError, errorModal, true, false) + pages.AddPage(pageNameTableLoading, loadingModal, false, false) pagination := NewPagination() @@ -116,7 +116,7 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver table.SetFixed(1, 0) table.SetInputCapture(table.tableInputCapture) table.SetSelectedStyle(tcell.StyleDefault.Background(tview.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) - table.Page.AddPage(sidebarPageName, table.Sidebar, false, false) + table.Page.AddPage(pageNameSidebar, table.Sidebar, false, false) table.SetSelectionChangedFunc(func(_, _ int) { if table.GetShowSidebar() { @@ -176,8 +176,8 @@ func (table *ResultsTable) WithEditor() *ResultsTable { resultsInfoText.SetTextColor(tview.Styles.PrimaryTextColor) resultsInfoWrapper.AddItem(resultsInfoText, 3, 0, false) - editorPages.AddPage(tableEditorTablePageName, tableWrapper, true, false) - editorPages.AddPage(tableEditorResultsInfoPageName, resultsInfoWrapper, true, true) + editorPages.AddPage(pageNameTableEditorTable, tableWrapper, true, false) + editorPages.AddPage(pageNameTableEditorResultsInfo, resultsInfoWrapper, true, true) table.EditorPages = editorPages table.ResultsInfo = resultsInfoText @@ -193,7 +193,7 @@ func (table *ResultsTable) subscribeToTreeChanges() { ch := table.Tree.Subscribe() for stateChange := range ch { - if stateChange.Key == selectedDatabaseTree { + if stateChange.Key == eventTreeSelectedDatabase { table.SetDatabaseName(stateChange.Value.(string)) } } @@ -204,13 +204,13 @@ func (table *ResultsTable) subscribeToSidebarChanges() { for stateChange := range ch { switch stateChange.Key { - case editingSidebar: + case eventSidebarEditing: editing := stateChange.Value.(bool) table.SetIsEditing(editing) - case unfocusingSidebar: + case eventSidebarUnfocusing: App.SetFocus(table) App.ForceDraw() - case togglingSidebar: + case eventSidebarToggling: table.ShowSidebar(false) App.ForceDraw() } @@ -493,7 +493,7 @@ func (table *ResultsTable) subscribeToFilterChanges() { for stateChange := range ch { switch stateChange.Key { - case filteringResultsTable: + case eventResultsTableFiltering: if stateChange.Value != "" { rows := table.FetchRecords(nil) @@ -532,7 +532,7 @@ func (table *ResultsTable) subscribeToEditorChanges() { for stateChange := range ch { switch stateChange.Key { - case querySQLEditor: + case eventSqlEditorQuery: query := stateChange.Value.(string) if query != "" { queryLower := strings.ToLower(query) @@ -568,7 +568,7 @@ func (table *ResultsTable) subscribeToEditorChanges() { } table.SetLoading(false) } - table.EditorPages.SwitchToPage(tablePageName) + table.EditorPages.SwitchToPage(pageNameTable) App.Draw() } else { table.SetRecords([][]string{}) @@ -584,13 +584,13 @@ func (table *ResultsTable) subscribeToEditorChanges() { } else { table.SetResultsInfo(result) table.SetLoading(false) - table.EditorPages.SwitchToPage(tableEditorResultsInfoPageName) + table.EditorPages.SwitchToPage(pageNameTableEditorResultsInfo) App.SetFocus(table.Editor) App.Draw() } } } - case escapeSQLEditor: + case eventSqlEditorEscape: table.SetIsFiltering(false) App.SetFocus(table) table.HighlightTable() @@ -704,7 +704,7 @@ func (table *ResultsTable) SetError(err string, done func()) { table.Error.SetText(err) table.Error.SetDoneFunc(func(_ int, _ string) { table.state.error = "" - table.Page.HidePage(tableErrorPageName) + table.Page.HidePage(pageNameTableError) if table.GetIsFiltering() { if table.Editor != nil { App.SetFocus(table.Editor) @@ -718,7 +718,7 @@ func (table *ResultsTable) SetError(err string, done func()) { done() } }) - table.Page.ShowPage(tableErrorPageName) + table.Page.ShowPage(pageNameTableError) App.SetFocus(table.Error) App.ForceDraw() } @@ -731,7 +731,7 @@ func (table *ResultsTable) SetLoading(show bool) { defer func() { if r := recover(); r != nil { logger.Error("ResultsTable.go:800 => Recovered from panic", map[string]any{"error": r}) - _ = table.Page.HidePage(tableLoadingPageName) + _ = table.Page.HidePage(pageNameTableLoading) if table.state.error != "" { App.SetFocus(table.Error) } else { @@ -742,11 +742,11 @@ func (table *ResultsTable) SetLoading(show bool) { table.state.isLoading = show if show { - table.Page.ShowPage(tableLoadingPageName) + table.Page.ShowPage(pageNameTableLoading) App.SetFocus(table.Loading) App.ForceDraw() } else { - table.Page.HidePage(tableLoadingPageName) + table.Page.HidePage(pageNameTableLoading) if table.state.error != "" { App.SetFocus(table.Error) } else { @@ -909,7 +909,7 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV if key == tcell.KeyEnter || key == tcell.KeyEscape { table.SetInputCapture(table.tableInputCapture) - table.Page.RemovePage(tableEditCellPageName) + table.Page.RemovePage(pageNameTableEditCell) App.SetFocus(table) } @@ -920,7 +920,7 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV x, y, width := cell.GetLastPosition() inputField.SetRect(x, y, width+1, 1) - table.Page.AddPage(tableEditCellPageName, inputField, false, true) + table.Page.AddPage(pageNameTableEditCell, inputField, false, true) App.SetFocus(inputField) } @@ -1038,7 +1038,7 @@ func (table *ResultsTable) GetPrimaryKeyValue(rowIndex int) (string, string) { primaryKeyValue := "" switch provider { - case drivers.MySQLDriver: + case drivers.DriverMySQL: keyColumnIndex := -1 primaryKeyColumnIndex := -1 @@ -1059,7 +1059,7 @@ func (table *ResultsTable) GetPrimaryKeyValue(rowIndex int) (string, string) { primaryKeyValue = table.GetRecords()[rowIndex][primaryKeyColumnIndex] } - case drivers.PostgresDriver: + case drivers.DriverPostgres: keyColumnIndex := -1 constraintTypeColumnIndex := -1 constraintNameColumnIndex := -1 @@ -1101,7 +1101,7 @@ func (table *ResultsTable) GetPrimaryKeyValue(rowIndex int) (string, string) { primaryKeyValue = table.GetRecords()[rowIndex][primaryKeyColumnIndex] } - case drivers.SQLiteDriver: + case drivers.DriverSqlite: keyColumnIndex := -1 primaryKeyColumnIndex := -1 @@ -1271,10 +1271,10 @@ func (table *ResultsTable) ShowSidebar(show bool) { if show { table.UpdateSidebar() - table.Page.SendToFront(sidebarPageName) - table.Page.ShowPage(sidebarPageName) + table.Page.SendToFront(pageNameSidebar) + table.Page.ShowPage(pageNameSidebar) } else { - table.Page.HidePage(sidebarPageName) + table.Page.HidePage(pageNameSidebar) App.SetFocus(table) } } diff --git a/components/ResultsTableMenu.go b/components/ResultsTableMenu.go index 2d7f9cc..a2aa0df 100644 --- a/components/ResultsTableMenu.go +++ b/components/ResultsTableMenu.go @@ -17,11 +17,11 @@ type ResultsTableMenu struct { } var menuItems = []string{ - recordsMenu, - columnsMenu, - constraintsMenu, - foreignKeysMenu, - indexesMenu, + menuRecords, + menuColumns, + menuConstraints, + menuForeignKeys, + menuIndexes, } func NewResultsTableMenu() *ResultsTableMenu { @@ -52,11 +52,11 @@ func NewResultsTableMenu() *ResultsTableMenu { size := 15 switch item { - case constraintsMenu: + case menuConstraints: size = 19 - case foreignKeysMenu: + case menuForeignKeys: size = 20 - case indexesMenu: + case menuIndexes: size = 16 } diff --git a/components/SQLEditor.go b/components/SQLEditor.go index 2f66f82..e314811 100644 --- a/components/SQLEditor.go +++ b/components/SQLEditor.go @@ -38,10 +38,10 @@ func NewSQLEditor() *SQLEditor { command := app.Keymaps.Group(app.EditorGroup).Resolve(event) if command == commands.Execute { - sqlEditor.Publish(querySQLEditor, sqlEditor.GetText()) + sqlEditor.Publish(eventSqlEditorQuery, sqlEditor.GetText()) return nil } else if command == commands.UnfocusEditor { - sqlEditor.Publish(escapeSQLEditor, "") + sqlEditor.Publish(eventSqlEditorEscape, "") } else if command == commands.OpenInExternalEditor && runtime.GOOS == "linux" { // ----- THIS IS A LINUX-ONLY FEATURE, for now diff --git a/components/Sidebar.go b/components/Sidebar.go index eea2558..69b593e 100644 --- a/components/Sidebar.go +++ b/components/Sidebar.go @@ -140,9 +140,9 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { switch command { case commands.UnfocusSidebar: - sidebar.Publish(models.StateChange{Key: unfocusingSidebar, Value: nil}) + sidebar.Publish(models.StateChange{Key: eventSidebarUnfocusing, Value: nil}) case commands.ToggleSidebar: - sidebar.Publish(models.StateChange{Key: togglingSidebar, Value: nil}) + sidebar.Publish(models.StateChange{Key: eventSidebarToggling, Value: nil}) case commands.MoveDown: sidebar.FocusNextField() case commands.MoveUp: @@ -152,7 +152,7 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { case commands.GotoEnd: sidebar.FocusLastField() case commands.Edit: - sidebar.Publish(models.StateChange{Key: editingSidebar, Value: true}) + sidebar.Publish(models.StateChange{Key: eventSidebarEditing, Value: true}) currentItemIndex := sidebar.GetCurrentFieldIndex() item := sidebar.Fields[currentItemIndex] @@ -165,13 +165,13 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { case commands.CommitEdit: sidebar.SetInputCapture(sidebar.inputCapture) sidebar.SetDisabledStyles(item) - sidebar.Publish(models.StateChange{Key: editingSidebar, Value: false}) + sidebar.Publish(models.StateChange{Key: eventSidebarEditing, Value: false}) return nil case commands.DiscardEdit: sidebar.SetInputCapture(sidebar.inputCapture) sidebar.SetDisabledStyles(item) item.SetText(text, true) - sidebar.Publish(models.StateChange{Key: editingSidebar, Value: false}) + sidebar.Publish(models.StateChange{Key: eventSidebarEditing, Value: false}) return nil } diff --git a/components/Tree.go b/components/Tree.go index 97327dd..8e15d75 100644 --- a/components/Tree.go +++ b/components/Tree.go @@ -388,7 +388,7 @@ func (tree *Tree) GetIsFiltering() bool { func (tree *Tree) SetSelectedDatabase(database string) { tree.state.selectedDatabase = database tree.Publish(models.StateChange{ - Key: selectedDatabaseTree, + Key: eventTreeSelectedDatabase, Value: database, }) } @@ -396,7 +396,7 @@ func (tree *Tree) SetSelectedDatabase(database string) { func (tree *Tree) SetSelectedTable(table string) { tree.state.selectedTable = table tree.Publish(models.StateChange{ - Key: selectedTableTree, + Key: eventTreeSelectedTable, Value: table, }) } @@ -404,7 +404,7 @@ func (tree *Tree) SetSelectedTable(table string) { func (tree *Tree) SetIsFiltering(isFiltering bool) { tree.state.isFiltering = isFiltering tree.Publish(models.StateChange{ - Key: isFilteringTree, + Key: eventTreeIsFiltering, Value: isFiltering, }) } diff --git a/components/constants.go b/components/constants.go index 4589195..017b788 100644 --- a/components/constants.go +++ b/components/constants.go @@ -7,60 +7,60 @@ var App = app.App // Pages const ( // General - helpPageName string = "Help" - confirmationPageName string = "Confirmation" - connectionsPageName string = "Connections" + pageNameHelp string = "Help" + pageNameConfirmation string = "Confirmation" + pageNameConnections string = "Connections" // Results table - tablePageName string = "Table" - tableErrorPageName string = "TableError" - tableLoadingPageName string = "TableLoading" - tableEditorTablePageName string = "TableEditorTable" - tableEditorResultsInfoPageName string = "TableEditorResultsInfo" - tableEditCellPageName string = "TableEditCell" + pageNameTable string = "Table" + pageNameTableError string = "TableError" + pageNameTableLoading string = "TableLoading" + pageNameTableEditorTable string = "TableEditorTable" + pageNameTableEditorResultsInfo string = "TableEditorResultsInfo" + pageNameTableEditCell string = "TableEditCell" // Sidebar - sidebarPageName string = "Sidebar" + pageNameSidebar string = "Sidebar" // Connections - connectionsSelectionPageName string = "ConnectionsSelection" - connectionsFormPageName string = "ConnectionsForm" + pageNameConnectionSelection string = "ConnectionSelection" + pageNameConnectionForm string = "ConnectionForm" ) // Tabs const ( - editorTabName string = "Editor" + tabNameEditor string = "Editor" ) // Events const ( - editingSidebar string = "EditingSidebar" - unfocusingSidebar string = "UnfocusingSidebar" - togglingSidebar string = "TogglingSidebar" + eventSidebarEditing string = "EditingSidebar" + eventSidebarUnfocusing string = "UnfocusingSidebar" + eventSidebarToggling string = "TogglingSidebar" - querySQLEditor string = "Query" - escapeSQLEditor string = "Escape" + eventSqlEditorQuery string = "Query" + eventSqlEditorEscape string = "Escape" - filteringResultsTable string = "FilteringResultsTable" + eventResultsTableFiltering string = "FilteringResultsTable" - selectedDatabaseTree string = "SelectedDatabase" - selectedTableTree string = "SelectedTable" - isFilteringTree string = "IsFiltering" + eventTreeSelectedDatabase string = "SelectedDatabase" + eventTreeSelectedTable string = "SelectedTable" + eventTreeIsFiltering string = "IsFiltering" ) // Results table menu items const ( - recordsMenu string = "Records" - columnsMenu string = "Columns" - constraintsMenu string = "Constraints" - foreignKeysMenu string = "Foreign Keys" - indexesMenu string = "Indexes" + menuRecords string = "Records" + menuColumns string = "Columns" + menuConstraints string = "Constraints" + menuForeignKeys string = "Foreign Keys" + menuIndexes string = "Indexes" ) // Actions const ( - newConnection string = "NewConnection" - editConnection string = "EditConnection" + actionNewConnection string = "NewConnection" + actionEditConnection string = "EditConnection" ) // Misc (until i find a better name) diff --git a/drivers/constants.go b/drivers/constants.go index 86c7691..31d8d6f 100644 --- a/drivers/constants.go +++ b/drivers/constants.go @@ -6,7 +6,7 @@ const ( // Drivers const ( - MySQLDriver string = "mysql" - PostgresDriver string = "postgres" - SQLiteDriver string = "sqlite3" + DriverMySQL string = "mysql" + DriverPostgres string = "postgres" + DriverSqlite string = "sqlite3" ) diff --git a/drivers/mysql.go b/drivers/mysql.go index 1b27bdd..4e0e4c0 100644 --- a/drivers/mysql.go +++ b/drivers/mysql.go @@ -22,7 +22,7 @@ func (db *MySQL) TestConnection(urlstr string) (err error) { } func (db *MySQL) Connect(urlstr string) (err error) { - db.SetProvider(MySQLDriver) + db.SetProvider(DriverMySQL) db.Connection, err = dburl.Open(urlstr) if err != nil { diff --git a/drivers/postgres.go b/drivers/postgres.go index 609492f..e55c58a 100644 --- a/drivers/postgres.go +++ b/drivers/postgres.go @@ -31,7 +31,7 @@ func (db *Postgres) TestConnection(urlstr string) error { } func (db *Postgres) Connect(urlstr string) (err error) { - db.SetProvider(PostgresDriver) + db.SetProvider(DriverPostgres) db.Connection, err = dburl.Open(urlstr) if err != nil { diff --git a/drivers/sqlite.go b/drivers/sqlite.go index 9e674b8..1938de3 100644 --- a/drivers/sqlite.go +++ b/drivers/sqlite.go @@ -23,7 +23,7 @@ func (db *SQLite) TestConnection(urlstr string) (err error) { } func (db *SQLite) Connect(urlstr string) (err error) { - db.SetProvider(SQLiteDriver) + db.SetProvider(DriverSqlite) db.Connection, err = sql.Open("sqlite", urlstr) if err != nil { From 2305a743b610d320c3d509b08df4853049b033be Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sun, 8 Sep 2024 13:55:53 -0400 Subject: [PATCH 12/19] chore: creates a custom theme --- app/App.go | 13 +++++- components/ConfirmationModal.go | 6 ++- components/ConnectionForm.go | 15 +++--- components/ConnectionSelection.go | 12 ++--- components/ConnectionsTable.go | 3 +- components/HelpModal.go | 8 ++-- components/HelpStatus.go | 2 +- components/Home.go | 12 ++--- components/Pages.go | 4 +- components/ResultTableFilter.go | 35 +++++++------- components/ResultsTable.go | 48 +++++++++---------- components/ResultsTableMenu.go | 18 ++++---- components/SQLEditor.go | 8 ++-- components/Sidebar.go | 22 ++++----- components/TabbedMenu.go | 10 ++-- components/Tree.go | 76 +++++++++++++++---------------- 16 files changed, 156 insertions(+), 136 deletions(-) diff --git a/app/App.go b/app/App.go index 213f09a..a0d2c4c 100644 --- a/app/App.go +++ b/app/App.go @@ -7,6 +7,15 @@ import ( var App = tview.NewApplication() +type Theme struct { + SidebarTitleBorderColor string + tview.Theme +} + +var Styles = Theme{ + SidebarTitleBorderColor: "#666A7E", +} + func init() { theme := tview.Theme{ PrimitiveBackgroundColor: tcell.ColorDefault, @@ -14,12 +23,14 @@ func init() { MoreContrastBackgroundColor: tcell.ColorGreen, BorderColor: tcell.ColorWhite, TitleColor: tcell.ColorWhite, - GraphicsColor: tcell.ColorWhite, + GraphicsColor: tcell.ColorGray, PrimaryTextColor: tcell.ColorDefault.TrueColor(), SecondaryTextColor: tcell.ColorYellow, TertiaryTextColor: tcell.ColorGreen, InverseTextColor: tcell.ColorWhite, ContrastSecondaryTextColor: tcell.ColorBlack, } + + Styles.Theme = theme tview.Styles = theme } diff --git a/components/ConfirmationModal.go b/components/ConfirmationModal.go index c9723b9..d0f6e79 100644 --- a/components/ConfirmationModal.go +++ b/components/ConfirmationModal.go @@ -2,6 +2,8 @@ package components import ( "github.com/rivo/tview" + + "github.com/jorgerojas26/lazysql/app" ) type ConfirmationModal struct { @@ -16,8 +18,8 @@ func NewConfirmationModal(confirmationText string) *ConfirmationModal { modal.SetText("Are you sure?") } modal.AddButtons([]string{"Yes", "No"}) - modal.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) - modal.SetTextColor(tview.Styles.PrimaryTextColor) + modal.SetBackgroundColor(app.Styles.PrimitiveBackgroundColor) + modal.SetTextColor(app.Styles.PrimaryTextColor) return &ConfirmationModal{ Modal: modal, diff --git a/components/ConnectionForm.go b/components/ConnectionForm.go index c168017..30145cb 100644 --- a/components/ConnectionForm.go +++ b/components/ConnectionForm.go @@ -6,6 +6,7 @@ import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" + "github.com/jorgerojas26/lazysql/app" "github.com/jorgerojas26/lazysql/drivers" "github.com/jorgerojas26/lazysql/helpers" "github.com/jorgerojas26/lazysql/models" @@ -23,35 +24,35 @@ func NewConnectionForm(connectionPages *models.ConnectionPages) *ConnectionForm wrapper.SetDirection(tview.FlexColumnCSS) - addForm := tview.NewForm().SetFieldBackgroundColor(tview.Styles.InverseTextColor).SetButtonBackgroundColor(tview.Styles.InverseTextColor).SetLabelColor(tview.Styles.PrimaryTextColor).SetFieldTextColor(tview.Styles.ContrastSecondaryTextColor) + addForm := tview.NewForm().SetFieldBackgroundColor(app.Styles.InverseTextColor).SetButtonBackgroundColor(tview.Styles.InverseTextColor).SetLabelColor(tview.Styles.PrimaryTextColor).SetFieldTextColor(tview.Styles.ContrastSecondaryTextColor) addForm.AddInputField("Name", "", 0, nil, nil) addForm.AddInputField("URL", "", 0, nil, nil) buttonsWrapper := tview.NewFlex().SetDirection(tview.FlexColumn) saveButton := tview.NewButton("[yellow]F1 [dark]Save") - saveButton.SetStyle(tcell.StyleDefault.Background(tview.Styles.PrimaryTextColor)) + saveButton.SetStyle(tcell.StyleDefault.Background(app.Styles.PrimaryTextColor)) saveButton.SetBorder(true) buttonsWrapper.AddItem(saveButton, 0, 1, false) buttonsWrapper.AddItem(nil, 1, 0, false) testButton := tview.NewButton("[yellow]F2 [dark]Test") - testButton.SetStyle(tcell.StyleDefault.Background(tview.Styles.PrimaryTextColor)) + testButton.SetStyle(tcell.StyleDefault.Background(app.Styles.PrimaryTextColor)) testButton.SetBorder(true) buttonsWrapper.AddItem(testButton, 0, 1, false) buttonsWrapper.AddItem(nil, 1, 0, false) connectButton := tview.NewButton("[yellow]F3 [dark]Connect") - connectButton.SetStyle(tcell.StyleDefault.Background(tview.Styles.PrimaryTextColor)) + connectButton.SetStyle(tcell.StyleDefault.Background(app.Styles.PrimaryTextColor)) connectButton.SetBorder(true) buttonsWrapper.AddItem(connectButton, 0, 1, false) buttonsWrapper.AddItem(nil, 1, 0, false) cancelButton := tview.NewButton("[yellow]Esc [dark]Cancel") - cancelButton.SetStyle(tcell.StyleDefault.Background(tcell.Color(tview.Styles.PrimaryTextColor))) + cancelButton.SetStyle(tcell.StyleDefault.Background(tcell.Color(app.Styles.PrimaryTextColor))) cancelButton.SetBorder(true) buttonsWrapper.AddItem(cancelButton, 0, 1, false) @@ -168,7 +169,7 @@ func (form *ConnectionForm) testConnection(connectionString string) { return } - form.StatusText.SetText("Connecting...").SetTextColor(tview.Styles.TertiaryTextColor) + form.StatusText.SetText("Connecting...").SetTextColor(app.Styles.TertiaryTextColor) var db drivers.Driver @@ -186,7 +187,7 @@ func (form *ConnectionForm) testConnection(connectionString string) { if err != nil { form.StatusText.SetText(err.Error()).SetTextStyle(tcell.StyleDefault.Foreground(tcell.ColorRed)) } else { - form.StatusText.SetText("Connection success").SetTextColor(tview.Styles.TertiaryTextColor) + form.StatusText.SetText("Connection success").SetTextColor(app.Styles.TertiaryTextColor) } App.ForceDraw() } diff --git a/components/ConnectionSelection.go b/components/ConnectionSelection.go index 301b37b..c5f50e4 100644 --- a/components/ConnectionSelection.go +++ b/components/ConnectionSelection.go @@ -29,35 +29,35 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod buttonsWrapper := tview.NewFlex().SetDirection(tview.FlexRowCSS) newButton := tview.NewButton("[yellow]N[dark]ew") - newButton.SetStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor)) + newButton.SetStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor)) newButton.SetBorder(true) buttonsWrapper.AddItem(newButton, 0, 1, false) buttonsWrapper.AddItem(nil, 1, 0, false) connectButton := tview.NewButton("[yellow]C[dark]onnect") - connectButton.SetStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor)) + connectButton.SetStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor)) connectButton.SetBorder(true) buttonsWrapper.AddItem(connectButton, 0, 1, false) buttonsWrapper.AddItem(nil, 1, 0, false) editButton := tview.NewButton("[yellow]E[dark]dit") - editButton.SetStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor)) + editButton.SetStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor)) editButton.SetBorder(true) buttonsWrapper.AddItem(editButton, 0, 1, false) buttonsWrapper.AddItem(nil, 1, 0, false) deleteButton := tview.NewButton("[yellow]D[dark]elete") - deleteButton.SetStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor)) + deleteButton.SetStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor)) deleteButton.SetBorder(true) buttonsWrapper.AddItem(deleteButton, 0, 1, false) buttonsWrapper.AddItem(nil, 1, 0, false) quitButton := tview.NewButton("[yellow]Q[dark]uit") - quitButton.SetStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor)) + quitButton.SetStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor)) quitButton.SetBorder(true) buttonsWrapper.AddItem(quitButton, 0, 1, false) @@ -145,7 +145,7 @@ func (cs *ConnectionSelection) Connect(connection models.Connection) { MainPages.SwitchToPage(connection.URL) App.Draw() } else { - cs.StatusText.SetText("Connecting...").SetTextColor(tview.Styles.TertiaryTextColor) + cs.StatusText.SetText("Connecting...").SetTextColor(app.Styles.TertiaryTextColor) App.Draw() var newDbDriver drivers.Driver diff --git a/components/ConnectionsTable.go b/components/ConnectionsTable.go index e4ca4ab..411609d 100644 --- a/components/ConnectionsTable.go +++ b/components/ConnectionsTable.go @@ -4,6 +4,7 @@ import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" + "github.com/jorgerojas26/lazysql/app" "github.com/jorgerojas26/lazysql/helpers" "github.com/jorgerojas26/lazysql/models" ) @@ -29,7 +30,7 @@ func NewConnectionsTable() *ConnectionsTable { } table.SetOffset(5, 0) - table.SetSelectedStyle(tcell.StyleDefault.Foreground(tview.Styles.SecondaryTextColor).Background(tview.Styles.PrimitiveBackgroundColor)) + table.SetSelectedStyle(tcell.StyleDefault.Foreground(app.Styles.SecondaryTextColor).Background(tview.Styles.PrimitiveBackgroundColor)) wrapper.AddItem(table, 0, 1, true) diff --git a/components/HelpModal.go b/components/HelpModal.go index 7bbf58e..27c965b 100644 --- a/components/HelpModal.go +++ b/components/HelpModal.go @@ -31,10 +31,10 @@ func NewHelpModal() *HelpModal { // table.SetBorders(true) table.SetBorder(true) - table.SetBorderColor(tview.Styles.PrimaryTextColor) + table.SetBorderColor(app.Styles.PrimaryTextColor) table.SetTitle(" Keybindings ") table.SetSelectable(true, false) - table.SetSelectedStyle(tcell.StyleDefault.Background(tview.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) + table.SetSelectedStyle(tcell.StyleDefault.Background(app.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) keymapGroups := app.Keymaps.Groups @@ -51,7 +51,7 @@ func NewHelpModal() *HelpModal { for groupName, keys := range keymapGroups { rowCount := table.GetRowCount() groupNameCell := tview.NewTableCell(strings.ToUpper(groupName)) - groupNameCell.SetTextColor(tview.Styles.TertiaryTextColor) + groupNameCell.SetTextColor(app.Styles.TertiaryTextColor) groupNameCell.SetSelectable(rowCount == 0) table.SetCell(rowCount, 0, tview.NewTableCell("").SetSelectable(false)) @@ -64,7 +64,7 @@ func NewHelpModal() *HelpModal { if len(keyText) < len(mostLengthyKey) { keyText = strings.Repeat(" ", len(mostLengthyKey)-len(keyText)) + keyText } - table.SetCell(rowCount+3+i, 0, tview.NewTableCell(keyText).SetAlign(tview.AlignRight).SetTextColor(tview.Styles.SecondaryTextColor)) + table.SetCell(rowCount+3+i, 0, tview.NewTableCell(keyText).SetAlign(tview.AlignRight).SetTextColor(app.Styles.SecondaryTextColor)) table.SetCell(rowCount+3+i, 1, tview.NewTableCell(key.Description).SetAlign(tview.AlignLeft).SetExpansion(1)) } diff --git a/components/HelpStatus.go b/components/HelpStatus.go index dfa03cb..32dec87 100644 --- a/components/HelpStatus.go +++ b/components/HelpStatus.go @@ -12,7 +12,7 @@ type HelpStatus struct { } func NewHelpStatus() HelpStatus { - status := HelpStatus{tview.NewTextView().SetTextColor(tview.Styles.TertiaryTextColor)} + status := HelpStatus{tview.NewTextView().SetTextColor(app.Styles.TertiaryTextColor)} status.SetStatusOnTree() diff --git a/components/Home.go b/components/Home.go index a4400c1..664ca9c 100644 --- a/components/Home.go +++ b/components/Home.go @@ -47,10 +47,10 @@ func NewHomePage(connection models.Connection, dbdriver drivers.Driver) *Home { go home.subscribeToTreeChanges() - leftWrapper.SetBorderColor(tview.Styles.InverseTextColor) + leftWrapper.SetBorderColor(app.Styles.InverseTextColor) leftWrapper.AddItem(tree.Wrapper, 0, 1, true) - rightWrapper.SetBorderColor(tview.Styles.InverseTextColor) + rightWrapper.SetBorderColor(app.Styles.InverseTextColor) rightWrapper.SetBorder(true) rightWrapper.SetDirection(tview.FlexColumnCSS) rightWrapper.SetInputCapture(home.rightWrapperInputCapture) @@ -131,8 +131,8 @@ func (home *Home) subscribeToTreeChanges() { func (home *Home) focusRightWrapper() { home.Tree.RemoveHighlight() - home.RightWrapper.SetBorderColor(tview.Styles.PrimaryTextColor) - home.LeftWrapper.SetBorderColor(tview.Styles.InverseTextColor) + home.RightWrapper.SetBorderColor(app.Styles.PrimaryTextColor) + home.LeftWrapper.SetBorderColor(app.Styles.InverseTextColor) home.TabbedPane.Highlight() tab := home.TabbedPane.GetCurrentTab() @@ -177,8 +177,8 @@ func (home *Home) focusTab(tab *Tab) { func (home *Home) focusLeftWrapper() { home.Tree.Highlight() - home.RightWrapper.SetBorderColor(tview.Styles.InverseTextColor) - home.LeftWrapper.SetBorderColor(tview.Styles.PrimaryTextColor) + home.RightWrapper.SetBorderColor(app.Styles.InverseTextColor) + home.LeftWrapper.SetBorderColor(app.Styles.PrimaryTextColor) tab := home.TabbedPane.GetCurrentTab() diff --git a/components/Pages.go b/components/Pages.go index 07222fe..7bea031 100644 --- a/components/Pages.go +++ b/components/Pages.go @@ -2,11 +2,13 @@ package components import ( "github.com/rivo/tview" + + "github.com/jorgerojas26/lazysql/app" ) var MainPages = tview.NewPages() func init() { - MainPages.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) + MainPages.SetBackgroundColor(app.Styles.PrimitiveBackgroundColor) MainPages.AddPage(pageNameConnections, NewConnectionPages().Flex, true, true) } diff --git a/components/ResultTableFilter.go b/components/ResultTableFilter.go index 97f0a2c..002582d 100644 --- a/components/ResultTableFilter.go +++ b/components/ResultTableFilter.go @@ -4,6 +4,7 @@ import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" + "github.com/jorgerojas26/lazysql/app" "github.com/jorgerojas26/lazysql/models" ) @@ -27,14 +28,14 @@ func NewResultsFilter() *ResultsTableFilter { recordsFilter.SetTitleAlign(tview.AlignCenter) recordsFilter.SetBorderPadding(0, 0, 1, 1) - recordsFilter.Label.SetTextColor(tview.Styles.TertiaryTextColor) + recordsFilter.Label.SetTextColor(app.Styles.TertiaryTextColor) recordsFilter.Label.SetText("WHERE") recordsFilter.Label.SetBorderPadding(0, 0, 0, 1) recordsFilter.Input.SetPlaceholder("Enter a WHERE clause to filter the results") - recordsFilter.Input.SetPlaceholderStyle(tcell.StyleDefault.Foreground(tview.Styles.PrimaryTextColor).Background(tview.Styles.PrimitiveBackgroundColor)) - recordsFilter.Input.SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor) - recordsFilter.Input.SetFieldTextColor(tview.Styles.PrimaryTextColor) + recordsFilter.Input.SetPlaceholderStyle(tcell.StyleDefault.Foreground(app.Styles.PrimaryTextColor).Background(tview.Styles.PrimitiveBackgroundColor)) + recordsFilter.Input.SetFieldBackgroundColor(app.Styles.PrimitiveBackgroundColor) + recordsFilter.Input.SetFieldTextColor(app.Styles.PrimaryTextColor) recordsFilter.Input.SetDoneFunc(func(key tcell.Key) { switch key { case tcell.KeyEnter: @@ -50,7 +51,7 @@ func NewResultsFilter() *ResultsTableFilter { } }) - recordsFilter.Input.SetAutocompleteStyles(tview.Styles.PrimitiveBackgroundColor, tcell.StyleDefault.Foreground(tview.Styles.PrimaryTextColor).Background(tview.Styles.PrimitiveBackgroundColor), tcell.StyleDefault.Foreground(tview.Styles.SecondaryTextColor).Background(tview.Styles.PrimitiveBackgroundColor)) + recordsFilter.Input.SetAutocompleteStyles(app.Styles.PrimitiveBackgroundColor, tcell.StyleDefault.Foreground(tview.Styles.PrimaryTextColor).Background(tview.Styles.PrimitiveBackgroundColor), tcell.StyleDefault.Foreground(tview.Styles.SecondaryTextColor).Background(tview.Styles.PrimitiveBackgroundColor)) recordsFilter.AddItem(recordsFilter.Label, 6, 0, false) recordsFilter.AddItem(recordsFilter.Input, 0, 1, false) @@ -87,29 +88,29 @@ func (filter *ResultsTableFilter) SetIsFiltering(filtering bool) { // Function to blur func (filter *ResultsTableFilter) RemoveHighlight() { - filter.SetBorderColor(tview.Styles.InverseTextColor) - filter.Label.SetTextColor(tview.Styles.InverseTextColor) - filter.Input.SetPlaceholderTextColor(tview.Styles.InverseTextColor) - filter.Input.SetFieldTextColor(tview.Styles.InverseTextColor) + filter.SetBorderColor(app.Styles.InverseTextColor) + filter.Label.SetTextColor(app.Styles.InverseTextColor) + filter.Input.SetPlaceholderTextColor(app.Styles.InverseTextColor) + filter.Input.SetFieldTextColor(app.Styles.InverseTextColor) } func (filter *ResultsTableFilter) RemoveLocalHighlight() { filter.SetBorderColor(tcell.ColorWhite) - filter.Label.SetTextColor(tview.Styles.TertiaryTextColor) - filter.Input.SetPlaceholderTextColor(tview.Styles.InverseTextColor) - filter.Input.SetFieldTextColor(tview.Styles.InverseTextColor) + filter.Label.SetTextColor(app.Styles.TertiaryTextColor) + filter.Input.SetPlaceholderTextColor(app.Styles.InverseTextColor) + filter.Input.SetFieldTextColor(app.Styles.InverseTextColor) } func (filter *ResultsTableFilter) Highlight() { filter.SetBorderColor(tcell.ColorWhite) - filter.Label.SetTextColor(tview.Styles.TertiaryTextColor) + filter.Label.SetTextColor(app.Styles.TertiaryTextColor) filter.Input.SetPlaceholderTextColor(tcell.ColorWhite) - filter.Input.SetFieldTextColor(tview.Styles.PrimaryTextColor) + filter.Input.SetFieldTextColor(app.Styles.PrimaryTextColor) } func (filter *ResultsTableFilter) HighlightLocal() { - filter.SetBorderColor(tview.Styles.PrimaryTextColor) - filter.Label.SetTextColor(tview.Styles.TertiaryTextColor) + filter.SetBorderColor(app.Styles.PrimaryTextColor) + filter.Label.SetTextColor(app.Styles.TertiaryTextColor) filter.Input.SetPlaceholderTextColor(tcell.ColorWhite) - filter.Input.SetFieldTextColor(tview.Styles.PrimaryTextColor) + filter.Input.SetFieldTextColor(app.Styles.PrimaryTextColor) } diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 77a4acc..44aed85 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -79,14 +79,14 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver errorModal.AddButtons([]string{"Ok"}) errorModal.SetText("An error occurred") errorModal.SetBackgroundColor(tcell.ColorRed) - errorModal.SetTextColor(tview.Styles.PrimaryTextColor) - errorModal.SetButtonStyle(tcell.StyleDefault.Foreground(tview.Styles.PrimaryTextColor)) + errorModal.SetTextColor(app.Styles.PrimaryTextColor) + errorModal.SetButtonStyle(tcell.StyleDefault.Foreground(app.Styles.PrimaryTextColor)) errorModal.SetFocus(0) loadingModal := tview.NewModal() loadingModal.SetText("Loading...") - loadingModal.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) - loadingModal.SetTextColor(tview.Styles.SecondaryTextColor) + loadingModal.SetBackgroundColor(app.Styles.PrimitiveBackgroundColor) + loadingModal.SetTextColor(app.Styles.SecondaryTextColor) pages := tview.NewPages() pages.AddPage(pageNameTable, wrapper, true, true) @@ -115,7 +115,7 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver table.SetBorders(true) table.SetFixed(1, 0) table.SetInputCapture(table.tableInputCapture) - table.SetSelectedStyle(tcell.StyleDefault.Background(tview.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) + table.SetSelectedStyle(tcell.StyleDefault.Background(app.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) table.Page.AddPage(pageNameSidebar, table.Sidebar, false, false) table.SetSelectionChangedFunc(func(_, _ int) { @@ -172,8 +172,8 @@ func (table *ResultsTable) WithEditor() *ResultsTable { resultsInfoWrapper := tview.NewFlex().SetDirection(tview.FlexColumnCSS) resultsInfoText := tview.NewTextView() resultsInfoText.SetBorder(true) - resultsInfoText.SetBorderColor(tview.Styles.PrimaryTextColor) - resultsInfoText.SetTextColor(tview.Styles.PrimaryTextColor) + resultsInfoText.SetBorderColor(app.Styles.PrimaryTextColor) + resultsInfoText.SetTextColor(app.Styles.PrimaryTextColor) resultsInfoWrapper.AddItem(resultsInfoText, 3, 0, false) editorPages.AddPage(pageNameTableEditorTable, tableWrapper, true, false) @@ -224,7 +224,7 @@ func (table *ResultsTable) AddRows(rows [][]string) { tableCell.SetSelectable(i > 0) tableCell.SetExpansion(1) - tableCell.SetTextColor(tview.Styles.PrimaryTextColor) + tableCell.SetTextColor(app.Styles.PrimaryTextColor) table.SetCell(i, j, tableCell) } @@ -272,7 +272,7 @@ func (table *ResultsTable) AppendNewRow(cells []models.CellValue, index int, UUI tableCell := tview.NewTableCell(cell.Value.(string)) tableCell.SetExpansion(1) tableCell.SetReference(UUID) - tableCell.SetTextColor(tview.Styles.PrimaryTextColor) + tableCell.SetTextColor(app.Styles.PrimaryTextColor) tableCell.SetBackgroundColor(tcell.ColorDarkGreen) switch cell.Type { @@ -280,7 +280,7 @@ func (table *ResultsTable) AppendNewRow(cells []models.CellValue, index int, UUI case models.Default: case models.String: tableCell.SetText("") - tableCell.SetTextColor(tview.Styles.InverseTextColor) + tableCell.SetTextColor(app.Styles.InverseTextColor) } table.SetCell(index, i, tableCell) @@ -455,10 +455,10 @@ func (table *ResultsTable) UpdateRowsColor(headerColor tcell.Color, rowColor tce } func (table *ResultsTable) RemoveHighlightTable() { - table.SetBorderColor(tview.Styles.InverseTextColor) - table.SetBordersColor(tview.Styles.InverseTextColor) - table.SetTitleColor(tview.Styles.InverseTextColor) - table.UpdateRowsColor(tview.Styles.InverseTextColor, tview.Styles.InverseTextColor) + table.SetBorderColor(app.Styles.InverseTextColor) + table.SetBordersColor(app.Styles.InverseTextColor) + table.SetTitleColor(app.Styles.InverseTextColor) + table.UpdateRowsColor(app.Styles.InverseTextColor, tview.Styles.InverseTextColor) } func (table *ResultsTable) RemoveHighlightAll() { @@ -472,10 +472,10 @@ func (table *ResultsTable) RemoveHighlightAll() { } func (table *ResultsTable) HighlightTable() { - table.SetBorderColor(tview.Styles.PrimaryTextColor) - table.SetBordersColor(tview.Styles.PrimaryTextColor) - table.SetTitleColor(tview.Styles.PrimaryTextColor) - table.UpdateRowsColor(tview.Styles.PrimaryTextColor, tview.Styles.PrimaryTextColor) + table.SetBorderColor(app.Styles.PrimaryTextColor) + table.SetBordersColor(app.Styles.PrimaryTextColor) + table.SetTitleColor(app.Styles.PrimaryTextColor) + table.UpdateRowsColor(app.Styles.PrimaryTextColor, tview.Styles.PrimaryTextColor) } func (table *ResultsTable) HighlightAll() { @@ -801,7 +801,7 @@ func (table *ResultsTable) SetSortedBy(column string, direction string) { tableCell := tview.NewTableCell(col[0]) tableCell.SetSelectable(false) tableCell.SetExpansion(1) - tableCell.SetTextColor(tview.Styles.PrimaryTextColor) + tableCell.SetTextColor(app.Styles.PrimaryTextColor) if col[0] == column { tableCell.SetText(fmt.Sprintf("%s %s", col[0], iconDirection)) @@ -868,8 +868,8 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV cell := table.GetCell(row, col) inputField := tview.NewInputField() inputField.SetText(cell.Text) - inputField.SetFieldBackgroundColor(tview.Styles.PrimaryTextColor) - inputField.SetFieldTextColor(tview.Styles.PrimitiveBackgroundColor) + inputField.SetFieldBackgroundColor(app.Styles.PrimaryTextColor) + inputField.SetFieldTextColor(app.Styles.PrimitiveBackgroundColor) inputField.SetDoneFunc(func(key tcell.Key) { table.SetIsEditing(false) @@ -990,7 +990,7 @@ func (table *ResultsTable) AppendNewChange(changeType models.DmlType, databaseNa } else { (*table.state.listOfDbChanges)[i].Values = append((*table.state.listOfDbChanges)[i].Values[:valueIndex], (*table.state.listOfDbChanges)[i].Values[valueIndex+1:]...) } - table.SetCellColor(rowIndex, colIndex, tview.Styles.PrimitiveBackgroundColor) + table.SetCellColor(rowIndex, colIndex, app.Styles.PrimitiveBackgroundColor) } else { (*table.state.listOfDbChanges)[i].Values[valueIndex] = value } @@ -1001,7 +1001,7 @@ func (table *ResultsTable) AppendNewChange(changeType models.DmlType, databaseNa case models.DmlDeleteType: *table.state.listOfDbChanges = append((*table.state.listOfDbChanges)[:i], (*table.state.listOfDbChanges)[i+1:]...) - table.SetRowColor(rowIndex, tview.Styles.PrimitiveBackgroundColor) + table.SetRowColor(rowIndex, app.Styles.PrimitiveBackgroundColor) } } } @@ -1307,7 +1307,7 @@ func (table *ResultsTable) UpdateSidebar() { if repeatCount <= 0 { repeatCount = 1 } - title += "[gray]" + strings.Repeat("-", repeatCount) + title += fmt.Sprintf("[%s]", app.Styles.SidebarTitleBorderColor) + strings.Repeat("-", repeatCount) title += colType table.Sidebar.AddField(title, text, sidebarWidth) diff --git a/components/ResultsTableMenu.go b/components/ResultsTableMenu.go index a2aa0df..e37ba9b 100644 --- a/components/ResultsTableMenu.go +++ b/components/ResultsTableMenu.go @@ -4,6 +4,8 @@ import ( "fmt" "github.com/rivo/tview" + + "github.com/jorgerojas26/lazysql/app" ) type ResultsTableMenuState struct { @@ -46,7 +48,7 @@ func NewResultsTableMenu() *ResultsTableMenu { textview := tview.NewTextView().SetText(text) if i == 0 { - textview.SetTextColor(tview.Styles.PrimaryTextColor) + textview.SetTextColor(app.Styles.PrimaryTextColor) } size := 15 @@ -80,29 +82,29 @@ func (menu *ResultsTableMenu) SetSelectedOption(option int) { itemCount := menu.GetItemCount() for i := 0; i < itemCount; i++ { - menu.GetItem(i).(*tview.TextView).SetTextColor(tview.Styles.PrimaryTextColor) + menu.GetItem(i).(*tview.TextView).SetTextColor(app.Styles.PrimaryTextColor) } - menu.GetItem(option - 1).(*tview.TextView).SetTextColor(tview.Styles.SecondaryTextColor) + menu.GetItem(option - 1).(*tview.TextView).SetTextColor(app.Styles.SecondaryTextColor) } } func (menu *ResultsTableMenu) SetBlur() { - menu.SetBorderColor(tview.Styles.InverseTextColor) + menu.SetBorderColor(app.Styles.InverseTextColor) for _, item := range menu.MenuItems { - item.SetTextColor(tview.Styles.InverseTextColor) + item.SetTextColor(app.Styles.InverseTextColor) } } func (menu *ResultsTableMenu) SetFocus() { - menu.SetBorderColor(tview.Styles.PrimaryTextColor) + menu.SetBorderColor(app.Styles.PrimaryTextColor) for i, item := range menu.MenuItems { if i+1 == menu.GetSelectedOption() { - item.SetTextColor(tview.Styles.SecondaryTextColor) + item.SetTextColor(app.Styles.SecondaryTextColor) } else { - item.SetTextColor(tview.Styles.PrimaryTextColor) + item.SetTextColor(app.Styles.PrimaryTextColor) } } } diff --git a/components/SQLEditor.go b/components/SQLEditor.go index e314811..dc5e3ba 100644 --- a/components/SQLEditor.go +++ b/components/SQLEditor.go @@ -79,13 +79,13 @@ func (s *SQLEditor) SetIsFocused(isFocused bool) { } func (s *SQLEditor) Highlight() { - s.SetBorderColor(tview.Styles.PrimaryTextColor) - s.SetTextStyle(tcell.StyleDefault.Foreground(tview.Styles.PrimaryTextColor)) + s.SetBorderColor(app.Styles.PrimaryTextColor) + s.SetTextStyle(tcell.StyleDefault.Foreground(app.Styles.PrimaryTextColor)) } func (s *SQLEditor) SetBlur() { - s.SetBorderColor(tview.Styles.InverseTextColor) - s.SetTextStyle(tcell.StyleDefault.Foreground(tview.Styles.InverseTextColor)) + s.SetBorderColor(app.Styles.InverseTextColor) + s.SetTextStyle(tcell.StyleDefault.Foreground(app.Styles.InverseTextColor)) } /* diff --git a/components/Sidebar.go b/components/Sidebar.go index 69b593e..0fba002 100644 --- a/components/Sidebar.go +++ b/components/Sidebar.go @@ -24,7 +24,7 @@ type Sidebar struct { func NewSidebar() *Sidebar { flex := tview.NewFlex().SetDirection(tview.FlexColumnCSS) frame := tview.NewFrame(flex) - frame.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) + frame.SetBackgroundColor(app.Styles.PrimitiveBackgroundColor) frame.SetBorder(true) sidebarState := &SidebarState{ @@ -56,9 +56,9 @@ func (sidebar *Sidebar) AddField(title, text string, fieldWidth int) { field.SetBorder(true) field.SetTitle(title) field.SetTitleAlign(tview.AlignLeft) - field.SetTitleColor(tview.Styles.PrimaryTextColor) + field.SetTitleColor(app.Styles.PrimaryTextColor) field.SetText(text, true) - field.SetTextStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.SecondaryTextColor)) + field.SetTextStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.SecondaryTextColor)) textLength := len(field.GetText()) @@ -186,20 +186,20 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { } func (sidebar *Sidebar) SetEditingStyles(item *tview.TextArea) { - item.SetBackgroundColor(tview.Styles.SecondaryTextColor) - item.SetTextStyle(tcell.StyleDefault.Background(tview.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) - item.SetTitleColor(tview.Styles.ContrastSecondaryTextColor) - item.SetBorderColor(tview.Styles.ContrastSecondaryTextColor) + item.SetBackgroundColor(app.Styles.SecondaryTextColor) + item.SetTextStyle(tcell.StyleDefault.Background(app.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) + item.SetTitleColor(app.Styles.ContrastSecondaryTextColor) + item.SetBorderColor(app.Styles.ContrastSecondaryTextColor) item.SetWrap(true) item.SetDisabled(false) } func (sidebar *Sidebar) SetDisabledStyles(item *tview.TextArea) { - item.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) - item.SetTextStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.SecondaryTextColor)) - item.SetTitleColor(tview.Styles.PrimaryTextColor) - item.SetBorderColor(tview.Styles.BorderColor) + item.SetBackgroundColor(app.Styles.PrimitiveBackgroundColor) + item.SetTextStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.SecondaryTextColor)) + item.SetTitleColor(app.Styles.PrimaryTextColor) + item.SetBorderColor(app.Styles.BorderColor) item.SetWrap(true) item.SetDisabled(true) diff --git a/components/TabbedMenu.go b/components/TabbedMenu.go index 75dfd0f..2ee7bf7 100644 --- a/components/TabbedMenu.go +++ b/components/TabbedMenu.go @@ -241,9 +241,9 @@ func (t *TabbedPane) HighlightTabHeader(tab *Tab) { for i := 0; tabToHighlight != nil && i < t.state.Length; i++ { if tabToHighlight.Header == tab.Header { - tabToHighlight.Header.SetTextColor(tview.Styles.SecondaryTextColor) + tabToHighlight.Header.SetTextColor(app.Styles.SecondaryTextColor) } else { - tabToHighlight.Header.SetTextColor(tview.Styles.PrimaryTextColor) + tabToHighlight.Header.SetTextColor(app.Styles.PrimaryTextColor) } tabToHighlight = tabToHighlight.NextTab } @@ -254,9 +254,9 @@ func (t *TabbedPane) Highlight() { for i := 0; tab != nil && i < t.state.Length; i++ { if tab == t.state.CurrentTab { - tab.Header.SetTextColor(tview.Styles.SecondaryTextColor) + tab.Header.SetTextColor(app.Styles.SecondaryTextColor) } else { - tab.Header.SetTextColor(tview.Styles.PrimaryTextColor) + tab.Header.SetTextColor(app.Styles.PrimaryTextColor) } tab = tab.NextTab } @@ -266,7 +266,7 @@ func (t *TabbedPane) SetBlur() { tab := t.state.FirstTab for i := 0; tab != nil && i < t.state.Length; i++ { - tab.Header.SetTextColor(tview.Styles.InverseTextColor) + tab.Header.SetTextColor(app.Styles.InverseTextColor) tab = tab.NextTab } } diff --git a/components/Tree.go b/components/Tree.go index 8e15d75..17ac075 100644 --- a/components/Tree.go +++ b/components/Tree.go @@ -49,7 +49,7 @@ func NewTree(dbName string, dbdriver drivers.Driver) *Tree { } tree.SetTopLevel(1) - tree.SetGraphicsColor(tview.Styles.PrimaryTextColor) + tree.SetGraphicsColor(app.Styles.PrimaryTextColor) // tree.SetBorder(true) tree.SetTitle("Databases") tree.SetTitleAlign(tview.AlignLeft) @@ -77,7 +77,7 @@ func NewTree(dbName string, dbdriver drivers.Driver) *Tree { childNode := tview.NewTreeNode(database) childNode.SetExpanded(false) childNode.SetReference(database) - childNode.SetColor(tview.Styles.PrimaryTextColor) + childNode.SetColor(app.Styles.PrimaryTextColor) rootNode.AddChild(childNode) go func(database string, node *tview.TreeNode) { @@ -95,7 +95,7 @@ func NewTree(dbName string, dbdriver drivers.Driver) *Tree { tree.SetFocusFunc(nil) }) - selectedNodeTextColor := fmt.Sprintf("[black:%s]", tview.Styles.SecondaryTextColor.Name()) + selectedNodeTextColor := fmt.Sprintf("[black:%s]", app.Styles.SecondaryTextColor.Name()) previouslyFocusedNode := tree.GetCurrentNode() previouslyFocusedNode.SetText(selectedNodeTextColor + previouslyFocusedNode.GetText()) @@ -253,33 +253,33 @@ func NewTree(dbName string, dbdriver drivers.Driver) *Tree { return event }) - tree.Filter.SetFieldStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.PrimaryTextColor)) - tree.Filter.SetPlaceholderStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.InverseTextColor)) + tree.Filter.SetFieldStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.PrimaryTextColor)) + tree.Filter.SetPlaceholderStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.InverseTextColor)) tree.Filter.SetBorderPadding(0, 0, 0, 0) - tree.Filter.SetBorderColor(tview.Styles.PrimaryTextColor) + tree.Filter.SetBorderColor(app.Styles.PrimaryTextColor) tree.Filter.SetLabel("Search: ") - tree.Filter.SetLabelColor(tview.Styles.InverseTextColor) + tree.Filter.SetLabelColor(app.Styles.InverseTextColor) tree.Filter.SetFocusFunc(func() { - tree.Filter.SetLabelColor(tview.Styles.TertiaryTextColor) - tree.Filter.SetFieldTextColor(tview.Styles.PrimaryTextColor) + tree.Filter.SetLabelColor(app.Styles.TertiaryTextColor) + tree.Filter.SetFieldTextColor(app.Styles.PrimaryTextColor) }) tree.Filter.SetBlurFunc(func() { if tree.Filter.GetText() == "" { - tree.Filter.SetLabelColor(tview.Styles.InverseTextColor) + tree.Filter.SetLabelColor(app.Styles.InverseTextColor) } else { - tree.Filter.SetLabelColor(tview.Styles.TertiaryTextColor) + tree.Filter.SetLabelColor(app.Styles.TertiaryTextColor) } - tree.Filter.SetFieldTextColor(tview.Styles.InverseTextColor) + tree.Filter.SetFieldTextColor(app.Styles.InverseTextColor) }) - tree.FoundNodeCountInput.SetFieldStyle(tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.PrimaryTextColor)) + tree.FoundNodeCountInput.SetFieldStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.PrimaryTextColor)) tree.Wrapper.SetDirection(tview.FlexRow) tree.Wrapper.SetBorder(true) tree.Wrapper.SetBorderPadding(0, 0, 1, 1) - tree.Wrapper.SetTitleColor(tview.Styles.PrimaryTextColor) + tree.Wrapper.SetTitleColor(app.Styles.PrimaryTextColor) tree.Wrapper.AddItem(tree.Filter, 1, 0, false) tree.Wrapper.AddItem(tree.FoundNodeCountInput, 1, 0, false) @@ -300,14 +300,14 @@ func (tree *Tree) databasesToNodes(children map[string][]string, node *tview.Tre rootNode = tview.NewTreeNode(key) rootNode.SetExpanded(false) rootNode.SetReference(key) - rootNode.SetColor(tview.Styles.PrimaryTextColor) + rootNode.SetColor(app.Styles.PrimaryTextColor) node.AddChild(rootNode) } for _, child := range values { childNode := tview.NewTreeNode(child) childNode.SetExpanded(defaultExpanded) - childNode.SetColor(tview.Styles.PrimaryTextColor) + childNode.SetColor(app.Styles.PrimaryTextColor) if tree.DBDriver.GetProvider() == "sqlite3" { childNode.SetReference(child) } else if tree.DBDriver.GetProvider() == "postgres" { @@ -411,10 +411,10 @@ func (tree *Tree) SetIsFiltering(isFiltering bool) { // Blur func func (tree *Tree) RemoveHighlight() { - tree.SetBorderColor(tview.Styles.InverseTextColor) - tree.SetGraphicsColor(tview.Styles.InverseTextColor) - tree.SetTitleColor(tview.Styles.InverseTextColor) - // tree.GetRoot().SetColor(tview.Styles.InverseTextColor) + tree.SetBorderColor(app.Styles.InverseTextColor) + tree.SetGraphicsColor(app.Styles.InverseTextColor) + tree.SetTitleColor(app.Styles.InverseTextColor) + // tree.GetRoot().SetColor(app.Styles.InverseTextColor) childrens := tree.GetRoot().GetChildren() @@ -423,8 +423,8 @@ func (tree *Tree) RemoveHighlight() { childrenIsCurrentNode := children.GetReference() == tree.GetCurrentNode().GetReference() - if !childrenIsCurrentNode && currentColor == tview.Styles.PrimaryTextColor { - children.SetColor(tview.Styles.InverseTextColor) + if !childrenIsCurrentNode && currentColor == app.Styles.PrimaryTextColor { + children.SetColor(app.Styles.InverseTextColor) } childrenOfChildren := children.GetChildren() @@ -434,8 +434,8 @@ func (tree *Tree) RemoveHighlight() { childrenIsCurrentNode := children.GetReference() == tree.GetCurrentNode().GetReference() - if !childrenIsCurrentNode && currentColor == tview.Styles.PrimaryTextColor { - children.SetColor(tview.Styles.InverseTextColor) + if !childrenIsCurrentNode && currentColor == app.Styles.PrimaryTextColor { + children.SetColor(app.Styles.InverseTextColor) } } @@ -444,21 +444,21 @@ func (tree *Tree) RemoveHighlight() { } func (tree *Tree) ForceRemoveHighlight() { - tree.SetBorderColor(tview.Styles.InverseTextColor) - tree.SetGraphicsColor(tview.Styles.InverseTextColor) - tree.SetTitleColor(tview.Styles.InverseTextColor) - tree.GetRoot().SetColor(tview.Styles.InverseTextColor) + tree.SetBorderColor(app.Styles.InverseTextColor) + tree.SetGraphicsColor(app.Styles.InverseTextColor) + tree.SetTitleColor(app.Styles.InverseTextColor) + tree.GetRoot().SetColor(app.Styles.InverseTextColor) childrens := tree.GetRoot().GetChildren() for _, children := range childrens { - children.SetColor(tview.Styles.InverseTextColor) + children.SetColor(app.Styles.InverseTextColor) childrenOfChildren := children.GetChildren() for _, children := range childrenOfChildren { - children.SetColor(tview.Styles.InverseTextColor) + children.SetColor(app.Styles.InverseTextColor) } } @@ -466,26 +466,26 @@ func (tree *Tree) ForceRemoveHighlight() { // Focus func func (tree *Tree) Highlight() { - tree.SetBorderColor(tview.Styles.PrimaryTextColor) - tree.SetGraphicsColor(tview.Styles.PrimaryTextColor) - tree.SetTitleColor(tview.Styles.PrimaryTextColor) - tree.GetRoot().SetColor(tview.Styles.PrimaryTextColor) + tree.SetBorderColor(app.Styles.PrimaryTextColor) + tree.SetGraphicsColor(app.Styles.PrimaryTextColor) + tree.SetTitleColor(app.Styles.PrimaryTextColor) + tree.GetRoot().SetColor(app.Styles.PrimaryTextColor) childrens := tree.GetRoot().GetChildren() for _, children := range childrens { currentColor := children.GetColor() - if currentColor == tview.Styles.InverseTextColor { - children.SetColor(tview.Styles.PrimaryTextColor) + if currentColor == app.Styles.InverseTextColor { + children.SetColor(app.Styles.PrimaryTextColor) childrenOfChildren := children.GetChildren() for _, children := range childrenOfChildren { currentColor := children.GetColor() - if currentColor == tview.Styles.InverseTextColor { - children.SetColor(tview.Styles.PrimaryTextColor) + if currentColor == app.Styles.InverseTextColor { + children.SetColor(app.Styles.PrimaryTextColor) } } From 6e055df5b584564e4cc05ed950ac39fc6c960a00 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sun, 8 Sep 2024 13:59:46 -0400 Subject: [PATCH 13/19] chore: changes event name --- components/ResultsTable.go | 4 ++-- components/SQLEditor.go | 4 ++-- components/constants.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 44aed85..51bec91 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -532,7 +532,7 @@ func (table *ResultsTable) subscribeToEditorChanges() { for stateChange := range ch { switch stateChange.Key { - case eventSqlEditorQuery: + case eventSQLEditorQuery: query := stateChange.Value.(string) if query != "" { queryLower := strings.ToLower(query) @@ -590,7 +590,7 @@ func (table *ResultsTable) subscribeToEditorChanges() { } } } - case eventSqlEditorEscape: + case eventSQLEditorEscape: table.SetIsFiltering(false) App.SetFocus(table) table.HighlightTable() diff --git a/components/SQLEditor.go b/components/SQLEditor.go index dc5e3ba..3302259 100644 --- a/components/SQLEditor.go +++ b/components/SQLEditor.go @@ -38,10 +38,10 @@ func NewSQLEditor() *SQLEditor { command := app.Keymaps.Group(app.EditorGroup).Resolve(event) if command == commands.Execute { - sqlEditor.Publish(eventSqlEditorQuery, sqlEditor.GetText()) + sqlEditor.Publish(eventSQLEditorQuery, sqlEditor.GetText()) return nil } else if command == commands.UnfocusEditor { - sqlEditor.Publish(eventSqlEditorEscape, "") + sqlEditor.Publish(eventSQLEditorEscape, "") } else if command == commands.OpenInExternalEditor && runtime.GOOS == "linux" { // ----- THIS IS A LINUX-ONLY FEATURE, for now diff --git a/components/constants.go b/components/constants.go index 017b788..df45d77 100644 --- a/components/constants.go +++ b/components/constants.go @@ -38,8 +38,8 @@ const ( eventSidebarUnfocusing string = "UnfocusingSidebar" eventSidebarToggling string = "TogglingSidebar" - eventSqlEditorQuery string = "Query" - eventSqlEditorEscape string = "Escape" + eventSQLEditorQuery string = "Query" + eventSQLEditorEscape string = "Escape" eventResultsTableFiltering string = "FilteringResultsTable" From 0e19547554bbb12d1a3eda9da33775da1354a404 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sun, 8 Sep 2024 22:24:36 -0400 Subject: [PATCH 14/19] chore: simplify if statements --- components/Sidebar.go | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/components/Sidebar.go b/components/Sidebar.go index 0fba002..424a844 100644 --- a/components/Sidebar.go +++ b/components/Sidebar.go @@ -79,32 +79,37 @@ func (sidebar *Sidebar) AddField(title, text string, fieldWidth int) { func (sidebar *Sidebar) FocusNextField() { newIndex := sidebar.GetCurrentFieldIndex() + 1 - if newIndex < sidebar.Flex.GetItemCount() { - item := sidebar.Fields[newIndex] + if newIndex >= sidebar.Flex.GetItemCount() { + return + } - if item != nil { - sidebar.SetCurrentFieldIndex(newIndex) - App.SetFocus(item) - App.ForceDraw() - return - } + item := sidebar.Fields[newIndex] + if item == nil { + return } + + sidebar.SetCurrentFieldIndex(newIndex) + App.SetFocus(item) + App.ForceDraw() } func (sidebar *Sidebar) FocusPreviousField() { newIndex := sidebar.GetCurrentFieldIndex() - 1 - if newIndex >= 0 { - item := sidebar.Fields[newIndex] + if newIndex < 0 { + return + } - if item != nil { - sidebar.SetCurrentFieldIndex(newIndex) - App.SetFocus(item) - App.ForceDraw() - return - } + item := sidebar.Fields[newIndex] + + if item == nil { + return } + + sidebar.SetCurrentFieldIndex(newIndex) + App.SetFocus(item) + App.ForceDraw() } func (sidebar *Sidebar) FocusFirstField() { From 810d11c88d67fdaa36c6b0718bc5d2da853d80a3 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Mon, 9 Sep 2024 02:15:02 -0400 Subject: [PATCH 15/19] feat: add scrolling behavior --- app/Keymap.go | 2 +- components/ResultsTable.go | 8 +++-- components/Sidebar.go | 74 ++++++++++++++++++++++++++++++-------- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/app/Keymap.go b/app/Keymap.go index f4176f0..9d69bf8 100644 --- a/app/Keymap.go +++ b/app/Keymap.go @@ -122,7 +122,7 @@ var Keymaps = KeymapSystem{ }, SidebarGroup: { Bind{Key: Key{Char: 's'}, Cmd: cmd.UnfocusSidebar, Description: "Focus table"}, - Bind{Key: Key{Char: 'S'}, Cmd: cmd.ToggleSidebar, Description: "Focus table"}, + Bind{Key: Key{Char: 'S'}, Cmd: cmd.ToggleSidebar, Description: "Toggle sidebar"}, 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"}, diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 51bec91..0a8e50b 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -1291,7 +1291,9 @@ func (table *ResultsTable) UpdateSidebar() { sidebarWidth := (tableInnerWidth / 4) - table.Sidebar.SetRect(tableX+tableInnerWidth-sidebarWidth, tableMenuY, sidebarWidth, tableHeight+tableMenuHeight+tableFilterHeight) + sidebarHeight := tableHeight + tableMenuHeight + tableFilterHeight + 1 + + table.Sidebar.SetRect(tableX+tableInnerWidth-sidebarWidth, tableMenuY, sidebarWidth, sidebarHeight) table.Sidebar.Clear() for i := 1; i < len(columns); i++ { @@ -1301,8 +1303,7 @@ func (table *ResultsTable) UpdateSidebar() { text := table.GetCell(selectedRow, i-1).Text title := name - logger.Info("string repeat", map[string]any{"sidebarWidth": sidebarWidth, "name": len(name), "colType": len(colType)}) - repeatCount := sidebarWidth - len(name) - len(colType) - 6 // 2 for spaces added below and idk why 6 is needed, but it works. + repeatCount := sidebarWidth - len(name) - len(colType) - 4 // idk why 4 is needed, but it works. if repeatCount <= 0 { repeatCount = 1 @@ -1312,5 +1313,6 @@ func (table *ResultsTable) UpdateSidebar() { table.Sidebar.AddField(title, text, sidebarWidth) } + } } diff --git a/components/Sidebar.go b/components/Sidebar.go index 424a844..6d8e894 100644 --- a/components/Sidebar.go +++ b/components/Sidebar.go @@ -15,10 +15,10 @@ type SidebarState struct { type Sidebar struct { *tview.Frame - Flex *tview.Flex - state *SidebarState - Fields []*tview.TextArea - subscribers []chan models.StateChange + Flex *tview.Flex + state *SidebarState + FieldHeights []int + subscribers []chan models.StateChange } func NewSidebar() *Sidebar { @@ -26,6 +26,7 @@ func NewSidebar() *Sidebar { frame := tview.NewFrame(flex) frame.SetBackgroundColor(app.Styles.PrimitiveBackgroundColor) frame.SetBorder(true) + frame.SetBorders(0, 0, 0, 0, 0, 0) sidebarState := &SidebarState{ currentFieldIndex: 0, @@ -35,7 +36,6 @@ func NewSidebar() *Sidebar { Frame: frame, Flex: flex, state: sidebarState, - Fields: []*tview.TextArea{}, subscribers: []chan models.StateChange{}, } @@ -72,7 +72,30 @@ func (sidebar *Sidebar) AddField(title, text string, fieldWidth int) { field.SetWrap(false) } - sidebar.Fields = append(sidebar.Fields, field) + field.SetFocusFunc(func() { + _, y, _, h := field.GetRect() + _, _, _, mph := sidebar.GetRect() + + if y >= mph { + hidingFieldIndex := 0 + fieldCount := sidebar.Flex.GetItemCount() + + for i := 0; i < fieldCount; i++ { + f := sidebar.Flex.GetItem(i) + _, _, _, h := f.GetRect() + if h != 0 { + hidingFieldIndex = i + break + } + } + + sidebar.Flex.ResizeItem(sidebar.Flex.GetItem(hidingFieldIndex), 0, 0) + } else if h == 0 { + sidebar.Flex.ResizeItem(field, itemFixedSize, 0) + } + }) + + sidebar.FieldHeights = append(sidebar.FieldHeights, itemFixedSize) sidebar.Flex.AddItem(field, itemFixedSize, 0, true) } @@ -83,7 +106,7 @@ func (sidebar *Sidebar) FocusNextField() { return } - item := sidebar.Fields[newIndex] + item := sidebar.Flex.GetItem(newIndex) if item == nil { return @@ -101,7 +124,7 @@ func (sidebar *Sidebar) FocusPreviousField() { return } - item := sidebar.Fields[newIndex] + item := sidebar.Flex.GetItem(newIndex) if item == nil { return @@ -114,28 +137,51 @@ func (sidebar *Sidebar) FocusPreviousField() { func (sidebar *Sidebar) FocusFirstField() { sidebar.SetCurrentFieldIndex(0) - App.SetFocus(sidebar.Fields[0]) + App.SetFocus(sidebar.Flex.GetItem(0)) + + fieldCount := sidebar.Flex.GetItemCount() + + for i := 0; i < fieldCount; i++ { + field := sidebar.Flex.GetItem(i) + height := sidebar.FieldHeights[i] + sidebar.Flex.ResizeItem(field, height, 0) + } } func (sidebar *Sidebar) FocusLastField() { newIndex := sidebar.Flex.GetItemCount() - 1 sidebar.SetCurrentFieldIndex(newIndex) - App.SetFocus(sidebar.Fields[newIndex]) + App.SetFocus(sidebar.Flex.GetItem(newIndex)) + + _, _, _, ph := sidebar.GetRect() + + hSum := 0 + + for i := sidebar.Flex.GetItemCount() - 1; i >= 0; i-- { + field := sidebar.Flex.GetItem(i).(*tview.TextArea) + _, _, _, h := field.GetRect() + + hSum += h + + if hSum >= ph { + sidebar.Flex.ResizeItem(field, 0, 0) + } + } } func (sidebar *Sidebar) FocusField(index int) { sidebar.SetCurrentFieldIndex(index) - App.SetFocus(sidebar.Fields[index]) + App.SetFocus(sidebar.Flex.GetItem(index)) } func (sidebar *Sidebar) Clear() { - sidebar.Fields = make([]*tview.TextArea, 0) + sidebar.FieldHeights = make([]int, 0) sidebar.Flex.Clear() } func (sidebar *Sidebar) EditTextCurrentField() { index := sidebar.GetCurrentFieldIndex() - item := sidebar.Fields[index] + item := sidebar.Flex.GetItem(index).(*tview.TextArea) sidebar.SetEditingStyles(item) } @@ -160,7 +206,7 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { sidebar.Publish(models.StateChange{Key: eventSidebarEditing, Value: true}) currentItemIndex := sidebar.GetCurrentFieldIndex() - item := sidebar.Fields[currentItemIndex] + item := sidebar.Flex.GetItem(currentItemIndex).(*tview.TextArea) text := item.GetText() sidebar.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { From 4069b0b869591d4371b42cd9372d5179e0ef7043 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sun, 22 Sep 2024 21:08:13 -0400 Subject: [PATCH 16/19] feat: edits are commitable --- components/ResultsTable.go | 93 +++++++++++++++++++++++++++++--------- components/Sidebar.go | 60 +++++++++++++++++++----- components/constants.go | 16 +++++-- models/models.go | 17 +++++-- 4 files changed, 145 insertions(+), 41 deletions(-) diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 0a8e50b..51616b7 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -52,13 +52,6 @@ type ResultsTable struct { DBDriver drivers.Driver } -var ( - ErrorModal = tview.NewModal() - ChangeColor = tcell.ColorDarkOrange - InsertColor = tcell.ColorDarkGreen - DeleteColor = tcell.ColorRed -) - func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver drivers.Driver) *ResultsTable { state := &ResultsTableState{ records: [][]string{}, @@ -118,9 +111,10 @@ func NewResultsTable(listOfDbChanges *[]models.DbDmlChange, tree *Tree, dbdriver table.SetSelectedStyle(tcell.StyleDefault.Background(app.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) table.Page.AddPage(pageNameSidebar, table.Sidebar, false, false) - table.SetSelectionChangedFunc(func(_, _ int) { + table.SetSelectionChangedFunc(func(row, col int) { if table.GetShowSidebar() { - table.UpdateSidebar() + logger.Info("table.SetSelectionChangedFunc", map[string]any{"row": row, "col": col}) + go table.UpdateSidebar() } }) @@ -212,6 +206,30 @@ func (table *ResultsTable) subscribeToSidebarChanges() { App.ForceDraw() case eventSidebarToggling: table.ShowSidebar(false) + App.ForceDraw() + case eventSidebarCommitEditing: + params := stateChange.Value.(models.SidebarEditingCommitParams) + + table.SetInputCapture(table.tableInputCapture) + table.SetIsEditing(false) + + row, _ := table.GetSelection() + changedColumnIndex := table.GetColumnIndexByName(params.ColumnName) + tableCell := table.GetCell(row, changedColumnIndex) + + tableCell.SetText(params.NewValue) + + cellValue := models.CellValue{ + Type: models.String, + Column: params.ColumnName, + Value: params.NewValue, + TableColumnIndex: changedColumnIndex, + TableRowIndex: row, + } + + logger.Info("eventSidebarCommitEditing", map[string]any{"cellValue": cellValue, "params": params, "rowIndex": row, "changedColumnIndex": changedColumnIndex}) + table.AppendNewChange(models.DmlUpdateType, row, changedColumnIndex, cellValue) + App.ForceDraw() } } @@ -260,7 +278,7 @@ func (table *ResultsTable) AddInsertedRows() { tableCell.SetReference(inserts[i].PrimaryKeyValue) tableCell.SetTextColor(tcell.ColorWhite.TrueColor()) - tableCell.SetBackgroundColor(InsertColor) + tableCell.SetBackgroundColor(colorTableInsert) table.SetCell(rowIndex, j, tableCell) } @@ -340,7 +358,11 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event } if command == commands.Edit { - table.StartEditingCell(selectedRowIndex, selectedColumnIndex, nil) + table.StartEditingCell(selectedRowIndex, selectedColumnIndex, func(_ string, _, _ int) { + if table.GetShowSidebar() { + table.UpdateSidebar() + } + }) } else if command == commands.GotoNext { if selectedColumnIndex+1 < colCount { table.Select(selectedRowIndex, selectedColumnIndex+1) @@ -394,7 +416,7 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event } } } else { - table.AppendNewChange(models.DmlDeleteType, table.GetDatabaseName(), table.GetTableName(), selectedRowIndex, -1, models.CellValue{}) + table.AppendNewChange(models.DmlDeleteType, selectedRowIndex, -1, models.CellValue{}) } } @@ -655,6 +677,17 @@ func (table *ResultsTable) GetColumnNameByIndex(index int) string { return "" } +func (table *ResultsTable) GetColumnIndexByName(columnName string) int { + for i := 0; i < table.GetColumnCount(); i++ { + cell := table.GetCell(0, i) + if cell.Text == columnName { + return i + } + } + + return -1 +} + func (table *ResultsTable) GetIsLoading() bool { return table.state.isLoading } @@ -881,7 +914,7 @@ func (table *ResultsTable) StartEditingCell(row int, col int, callback func(newV cell.SetText(newValue) if currentValue != newValue { - table.AppendNewChange(models.DmlUpdateType, table.GetDatabaseName(), table.GetTableName(), row, col, models.CellValue{Type: models.String, Value: newValue, Column: columnName}) + table.AppendNewChange(models.DmlUpdateType, row, col, models.CellValue{Type: models.String, Value: newValue, Column: columnName, TableColumnIndex: col, TableRowIndex: row}) } switch key { @@ -947,7 +980,10 @@ func (table *ResultsTable) MutateInsertedRowCell(rowID string, newValue models.C } } -func (table *ResultsTable) AppendNewChange(changeType models.DmlType, databaseName, tableName string, rowIndex int, colIndex int, value models.CellValue) { +func (table *ResultsTable) AppendNewChange(changeType models.DmlType, rowIndex int, colIndex int, value models.CellValue) { + databaseName := table.GetDatabaseName() + tableName := table.GetTableName() + dmlChangeAlreadyExists := false // If the column has a reference, it means it's an inserted rowIndex @@ -996,7 +1032,7 @@ func (table *ResultsTable) AppendNewChange(changeType models.DmlType, databaseNa } } else { (*table.state.listOfDbChanges)[i].Values = append((*table.state.listOfDbChanges)[i].Values, value) - table.SetCellColor(rowIndex, colIndex, ChangeColor) + table.SetCellColor(rowIndex, colIndex, colorTableChange) } case models.DmlDeleteType: @@ -1009,10 +1045,10 @@ func (table *ResultsTable) AppendNewChange(changeType models.DmlType, databaseNa if !dmlChangeAlreadyExists { switch changeType { - case models.DmlDeleteType: - table.SetRowColor(rowIndex, DeleteColor) case models.DmlUpdateType: - table.SetCellColor(rowIndex, colIndex, ChangeColor) + table.SetCellColor(rowIndex, colIndex, colorTableChange) + case models.DmlDeleteType: + table.SetRowColor(rowIndex, colorTableDelete) } newDmlChange := models.DbDmlChange{ @@ -1027,6 +1063,8 @@ func (table *ResultsTable) AppendNewChange(changeType models.DmlType, databaseNa *table.state.listOfDbChanges = append(*table.state.listOfDbChanges, newDmlChange) } + + logger.Info("AppendNewChange", map[string]any{"listOfDbChanges": *table.state.listOfDbChanges}) } func (table *ResultsTable) GetPrimaryKeyValue(rowIndex int) (string, string) { @@ -1144,7 +1182,7 @@ func (table *ResultsTable) appendNewRow() { for i, column := range dbColumns { if i != 0 { // Skip the first row because they are the column names (e.x "Field", "Type", "Null", "Key", "Default", "Extra") - newRow[i-1] = models.CellValue{Type: models.Default, Column: column[0], Value: "DEFAULT"} + newRow[i-1] = models.CellValue{Type: models.Default, Column: column[0], Value: "DEFAULT", TableRowIndex: newRowTableIndex, TableColumnIndex: i} } } @@ -1290,7 +1328,6 @@ func (table *ResultsTable) UpdateSidebar() { _, _, _, tableFilterHeight := table.Filter.GetRect() sidebarWidth := (tableInnerWidth / 4) - sidebarHeight := tableHeight + tableMenuHeight + tableFilterHeight + 1 table.Sidebar.SetRect(tableX+tableInnerWidth-sidebarWidth, tableMenuY, sidebarWidth, sidebarHeight) @@ -1308,10 +1345,24 @@ func (table *ResultsTable) UpdateSidebar() { if repeatCount <= 0 { repeatCount = 1 } + title += fmt.Sprintf("[%s]", app.Styles.SidebarTitleBorderColor) + strings.Repeat("-", repeatCount) title += colType - table.Sidebar.AddField(title, text, sidebarWidth) + pendingEditExist := false + + for _, dmlChange := range *table.state.listOfDbChanges { + if dmlChange.Type == models.DmlUpdateType { + for _, v := range dmlChange.Values { + if v.Column == name && v.TableRowIndex == selectedRow && v.TableColumnIndex == i-1 { + pendingEditExist = true + break + } + } + } + } + + table.Sidebar.AddField(title, text, sidebarWidth, pendingEditExist) } } diff --git a/components/Sidebar.go b/components/Sidebar.go index 6d8e894..40211df 100644 --- a/components/Sidebar.go +++ b/components/Sidebar.go @@ -1,6 +1,8 @@ package components import ( + "strings" + "github.com/gdamore/tcell/v2" "github.com/rivo/tview" @@ -13,12 +15,17 @@ type SidebarState struct { currentFieldIndex int } +type SidebarFieldParameters struct { + OriginalValue string + Height int +} + type Sidebar struct { *tview.Frame - Flex *tview.Flex - state *SidebarState - FieldHeights []int - subscribers []chan models.StateChange + Flex *tview.Flex + state *SidebarState + FieldParameters []*SidebarFieldParameters + subscribers []chan models.StateChange } func NewSidebar() *Sidebar { @@ -48,7 +55,7 @@ func NewSidebar() *Sidebar { return newSidebar } -func (sidebar *Sidebar) AddField(title, text string, fieldWidth int) { +func (sidebar *Sidebar) AddField(title, text string, fieldWidth int, pendingEdit bool) { field := tview.NewTextArea() field.SetWrap(true) field.SetDisabled(true) @@ -60,6 +67,10 @@ func (sidebar *Sidebar) AddField(title, text string, fieldWidth int) { field.SetText(text, true) field.SetTextStyle(tcell.StyleDefault.Background(app.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.SecondaryTextColor)) + if pendingEdit { + sidebar.SetEditedStyles(field) + } + textLength := len(field.GetText()) itemFixedSize := 3 @@ -95,7 +106,12 @@ func (sidebar *Sidebar) AddField(title, text string, fieldWidth int) { } }) - sidebar.FieldHeights = append(sidebar.FieldHeights, itemFixedSize) + fieldParameters := &SidebarFieldParameters{ + Height: itemFixedSize, + OriginalValue: text, + } + + sidebar.FieldParameters = append(sidebar.FieldParameters, fieldParameters) sidebar.Flex.AddItem(field, itemFixedSize, 0, true) } @@ -143,7 +159,7 @@ func (sidebar *Sidebar) FocusFirstField() { for i := 0; i < fieldCount; i++ { field := sidebar.Flex.GetItem(i) - height := sidebar.FieldHeights[i] + height := sidebar.FieldParameters[i].Height sidebar.Flex.ResizeItem(field, height, 0) } } @@ -175,7 +191,7 @@ func (sidebar *Sidebar) FocusField(index int) { } func (sidebar *Sidebar) Clear() { - sidebar.FieldHeights = make([]int, 0) + sidebar.FieldParameters = make([]*SidebarFieldParameters, 0) sidebar.Flex.Clear() } @@ -209,14 +225,26 @@ func (sidebar *Sidebar) inputCapture(event *tcell.EventKey) *tcell.EventKey { item := sidebar.Flex.GetItem(currentItemIndex).(*tview.TextArea) text := item.GetText() + columnName := item.GetTitle() + columnNameSplit := strings.Split(columnName, "[") + columnName = columnNameSplit[0] + sidebar.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { command := app.Keymaps.Group(app.SidebarGroup).Resolve(event) switch command { case commands.CommitEdit: sidebar.SetInputCapture(sidebar.inputCapture) - sidebar.SetDisabledStyles(item) - sidebar.Publish(models.StateChange{Key: eventSidebarEditing, Value: false}) + originalValue := sidebar.FieldParameters[currentItemIndex].OriginalValue + newText := item.GetText() + + if originalValue == newText { + sidebar.SetDisabledStyles(item) + } else { + sidebar.SetEditedStyles(item) + sidebar.Publish(models.StateChange{Key: eventSidebarCommitEditing, Value: models.SidebarEditingCommitParams{ColumnName: columnName, NewValue: newText}}) + } + return nil case commands.DiscardEdit: sidebar.SetInputCapture(sidebar.inputCapture) @@ -240,7 +268,7 @@ func (sidebar *Sidebar) SetEditingStyles(item *tview.TextArea) { item.SetBackgroundColor(app.Styles.SecondaryTextColor) item.SetTextStyle(tcell.StyleDefault.Background(app.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor)) item.SetTitleColor(app.Styles.ContrastSecondaryTextColor) - item.SetBorderColor(app.Styles.ContrastSecondaryTextColor) + item.SetBorderColor(app.Styles.SecondaryTextColor) item.SetWrap(true) item.SetDisabled(false) @@ -256,6 +284,16 @@ func (sidebar *Sidebar) SetDisabledStyles(item *tview.TextArea) { item.SetDisabled(true) } +func (sidebar *Sidebar) SetEditedStyles(item *tview.TextArea) { + item.SetBackgroundColor(colorTableChange) + item.SetTextStyle(tcell.StyleDefault.Background(colorTableChange).Foreground(tview.Styles.ContrastSecondaryTextColor)) + item.SetTitleColor(app.Styles.ContrastSecondaryTextColor) + item.SetBorderColor(app.Styles.ContrastSecondaryTextColor) + + item.SetWrap(true) + item.SetDisabled(true) +} + // Getters func (sidebar *Sidebar) GetCurrentFieldIndex() int { return sidebar.state.currentFieldIndex diff --git a/components/constants.go b/components/constants.go index df45d77..571b852 100644 --- a/components/constants.go +++ b/components/constants.go @@ -1,6 +1,9 @@ package components -import "github.com/jorgerojas26/lazysql/app" +import ( + "github.com/gdamore/tcell/v2" + "github.com/jorgerojas26/lazysql/app" +) var App = app.App @@ -34,9 +37,10 @@ const ( // Events const ( - eventSidebarEditing string = "EditingSidebar" - eventSidebarUnfocusing string = "UnfocusingSidebar" - eventSidebarToggling string = "TogglingSidebar" + eventSidebarEditing string = "EditingSidebar" + eventSidebarUnfocusing string = "UnfocusingSidebar" + eventSidebarToggling string = "TogglingSidebar" + eventSidebarCommitEditing string = "CommitEditingSidebar" eventSQLEditorQuery string = "Query" eventSQLEditorEscape string = "Escape" @@ -67,4 +71,8 @@ const ( const ( focusedWrapperLeft string = "left" focusedWrapperRight string = "right" + + colorTableChange = tcell.ColorOrange + colorTableInsert = tcell.ColorDarkGreen + colorTableDelete = tcell.ColorRed ) diff --git a/models/models.go b/models/models.go index e8eb5ea..e0af427 100644 --- a/models/models.go +++ b/models/models.go @@ -37,9 +37,11 @@ const ( ) type CellValue struct { - Type CellValueType - Column string - Value interface{} + Value interface{} + Column string + TableColumnIndex int + TableRowIndex int + Type CellValueType } const ( @@ -49,12 +51,12 @@ const ( ) type DbDmlChange struct { - Type DmlType Database string Table string - Values []CellValue PrimaryKeyColumnName string PrimaryKeyValue string + Values []CellValue + Type DmlType } type DatabaseTableColumn struct { @@ -70,3 +72,8 @@ type Query struct { Query string Args []interface{} } + +type SidebarEditingCommitParams struct { + ColumnName string + NewValue string +} From ddd10225f78f873bf812526a6abd1f93cf0e15a1 Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sun, 22 Sep 2024 21:11:23 -0400 Subject: [PATCH 17/19] chore: linter --- components/constants.go | 1 + 1 file changed, 1 insertion(+) diff --git a/components/constants.go b/components/constants.go index 571b852..2bd13b5 100644 --- a/components/constants.go +++ b/components/constants.go @@ -2,6 +2,7 @@ package components import ( "github.com/gdamore/tcell/v2" + "github.com/jorgerojas26/lazysql/app" ) From fdd6e9b5f356e61238e07793663f964cb111551c Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Sun, 22 Sep 2024 21:29:58 -0400 Subject: [PATCH 18/19] fix: return back the pagination --- components/ResultsTable.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/ResultsTable.go b/components/ResultsTable.go index 51616b7..05a08a5 100644 --- a/components/ResultsTable.go +++ b/components/ResultsTable.go @@ -134,6 +134,7 @@ 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() @@ -1326,9 +1327,10 @@ func (table *ResultsTable) UpdateSidebar() { _, _, tableInnerWidth, _ := table.GetInnerRect() _, tableMenuY, _, tableMenuHeight := table.Menu.GetRect() _, _, _, tableFilterHeight := table.Filter.GetRect() + _, _, _, tablePaginationHeight := table.Pagination.GetRect() sidebarWidth := (tableInnerWidth / 4) - sidebarHeight := tableHeight + tableMenuHeight + tableFilterHeight + 1 + sidebarHeight := tableHeight + tableMenuHeight + tableFilterHeight + tablePaginationHeight + 1 table.Sidebar.SetRect(tableX+tableInnerWidth-sidebarWidth, tableMenuY, sidebarWidth, sidebarHeight) table.Sidebar.Clear() From 765cafc679ed76a5b5c2a7391c2eb5393831d53c Mon Sep 17 00:00:00 2001 From: Jorge Rojas Date: Fri, 11 Oct 2024 23:44:51 -0400 Subject: [PATCH 19/19] chore: fix lint errors --- drivers/postgres.go | 4 ++++ drivers/utils_test.go | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/postgres.go b/drivers/postgres.go index f2d9961..4cdb131 100644 --- a/drivers/postgres.go +++ b/drivers/postgres.go @@ -196,6 +196,10 @@ func (db *Postgres) GetTableColumns(database, table string) (results [][]string, results = append(results, row) } + if err := rows.Err(); err != nil { + return nil, err + } + return } diff --git a/drivers/utils_test.go b/drivers/utils_test.go index 8a7550f..847f486 100644 --- a/drivers/utils_test.go +++ b/drivers/utils_test.go @@ -6,15 +6,16 @@ import ( "testing" gomock "github.com/DATA-DOG/go-sqlmock" + "github.com/jorgerojas26/lazysql/models" ) func Test_queriesInTransaction(t *testing.T) { tests := []struct { - name string - queries []models.Query setMockExpectations func(mock gomock.Sqlmock) assertErr func(t *testing.T, err error) + name string + queries []models.Query }{ { name: "successful transaction", @@ -38,6 +39,7 @@ func Test_queriesInTransaction(t *testing.T) { mock.ExpectCommit().WillReturnError(errors.New("commit error")) }, assertErr: func(t *testing.T, err error) { + t.Helper() if !strings.Contains(err.Error(), "commit error") { t.Errorf("expected error to contain 'commit error', got %v", err) } @@ -54,6 +56,7 @@ func Test_queriesInTransaction(t *testing.T) { mock.ExpectRollback() }, assertErr: func(t *testing.T, err error) { + t.Helper() if !strings.Contains(err.Error(), "query error") { t.Errorf("expected error to contain 'commit error', got %v", err) } @@ -73,6 +76,7 @@ func Test_queriesInTransaction(t *testing.T) { mock.ExpectRollback() }, assertErr: func(t *testing.T, err error) { + t.Helper() if !strings.Contains(err.Error(), "query error") { t.Errorf("expected error to contain 'commit error', got %v", err) } @@ -89,6 +93,7 @@ func Test_queriesInTransaction(t *testing.T) { mock.ExpectRollback().WillReturnError(errors.New("rollback error")) }, assertErr: func(t *testing.T, err error) { + t.Helper() errMsg := err.Error() if !strings.Contains(errMsg, "query error") { t.Errorf("expected error to contain 'commit error', got %v", err)