Skip to content

Commit

Permalink
Merge pull request #103 from jorgerojas26/results-sidebar
Browse files Browse the repository at this point in the history
[WIP] Add a toggleable sidebar to view records
  • Loading branch information
jorgerojas26 authored Oct 12, 2024
2 parents aebceda + 765cafc commit c4ac8f1
Show file tree
Hide file tree
Showing 26 changed files with 835 additions and 225 deletions.
13 changes: 12 additions & 1 deletion app/App.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@ 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,
ContrastBackgroundColor: tcell.ColorBlue,
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
}
15 changes: 15 additions & 0 deletions app/Keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
TableGroup = "table"
EditorGroup = "editor"
ConnectionGroup = "connection"
SidebarGroup = "sidebar"
)

// Define a global KeymapSystem object with default keybinds
Expand Down Expand Up @@ -110,11 +111,25 @@ 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: '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"},
Bind{Key: Key{Char: 'G'}, Cmd: cmd.GotoEnd, Description: "Focus last field"},
Bind{Key: Key{Char: 'c'}, Cmd: cmd.Edit, Description: "Edit field"},
Bind{Key: Key{Code: tcell.KeyEnter}, Cmd: cmd.CommitEdit, Description: "Add edit to pending changes"},
Bind{Key: Key{Code: tcell.KeyEscape}, Cmd: cmd.DiscardEdit, Description: "Discard edit"},
},
},
}
15 changes: 15 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
UnfocusEditor
Copy
Edit
CommitEdit
DiscardEdit
Save
Delete
Search
Expand All @@ -60,6 +62,9 @@ const (
PreviousFoundNode
TreeCollapseAll
ExpandAll
FocusSidebar
UnfocusSidebar
ToggleSidebar

// Connection
NewConnection
Expand Down Expand Up @@ -179,6 +184,16 @@ func (c Command) String() string {
return "TreeCollapseAll"
case ExpandAll:
return "ExpandAll"
case FocusSidebar:
return "FocusSidebar"
case ToggleSidebar:
return "ToggleSidebar"
case UnfocusSidebar:
return "UnfocusSidebar"
case CommitEdit:
return "CommitEdit"
case DiscardEdit:
return "DiscardEdit"
}
return "Unknown"
}
6 changes: 4 additions & 2 deletions components/ConfirmationModal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package components

import (
"github.com/rivo/tview"

"github.com/jorgerojas26/lazysql/app"
)

type ConfirmationModal struct {
Expand All @@ -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,
Expand Down
29 changes: 15 additions & 14 deletions components/ConnectionForm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -77,7 +78,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(pageNameConnections)
} else if event.Key() == tcell.KeyF1 || event.Key() == tcell.KeyEnter {
connectionName := form.GetFormItem(0).(*tview.InputField).GetText()

Expand Down Expand Up @@ -111,7 +112,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages
}

switch form.Action {
case "create":
case actionNewConnection:

newDatabases = append(databases, parsedDatabaseData)
err := helpers.SaveConnectionConfig(newDatabases)
Expand All @@ -120,7 +121,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages
return event
}

case "edit":
case actionEditConnection:
newDatabases = make([]models.Connection, len(databases))
row, _ := ConnectionListTable.GetSelection()

Expand Down Expand Up @@ -151,7 +152,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages
}

ConnectionListTable.SetConnections(newDatabases)
connectionPages.SwitchToPage("Connections")
connectionPages.SwitchToPage(pageNameConnections)

} else if event.Key() == tcell.KeyF2 {
connectionString := form.GetFormItem(1).(*tview.InputField).GetText()
Expand All @@ -168,16 +169,16 @@ 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

switch parsed.Driver {
case "mysql":
case drivers.DriverMySQL:
db = &drivers.MySQL{}
case "postgres":
case drivers.DriverPostgres:
db = &drivers.Postgres{}
case "sqlite3":
case drivers.DriverSqlite:
db = &drivers.SQLite{}
}

Expand All @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions components/ConnectionPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(pageNameConnectionSelection, connectionSelection.Flex, true, true)
cp.AddPage(pageNameConnectionForm, connectionForm.Flex, true, false)

return cp
}
30 changes: 15 additions & 15 deletions components/ConnectionSelection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(pageNameConnectionForm)
connectionForm.GetFormItemByLabel("Name").(*tview.InputField).SetText(selectedConnection.Name)
connectionForm.GetFormItemByLabel("URL").(*tview.InputField).SetText(selectedConnection.URL)
connectionForm.StatusText.SetText("")

connectionForm.SetAction("edit")
connectionForm.SetAction(actionEditConnection)
return nil
case commands.DeleteConnection:
confirmationModal := NewConfirmationModal("")

confirmationModal.SetDoneFunc(func(_ int, buttonLabel string) {
MainPages.RemovePage("Confirmation")
MainPages.RemovePage(pageNameConfirmation)
confirmationModal = nil

if buttonLabel == "Yes" {
Expand All @@ -115,19 +115,19 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod
}
})

MainPages.AddPage("Confirmation", confirmationModal, true, true)
MainPages.AddPage(pageNameConfirmation, confirmationModal, true, true)

return nil
}
}

switch command {
case commands.NewConnection:
connectionForm.SetAction("create")
connectionForm.SetAction(actionNewConnection)
connectionForm.GetFormItemByLabel("Name").(*tview.InputField).SetText("")
connectionForm.GetFormItemByLabel("URL").(*tview.InputField).SetText("")
connectionForm.StatusText.SetText("")
connectionPages.SwitchToPage("ConnectionForm")
connectionPages.SwitchToPage(pageNameConnectionForm)
case commands.Quit:
if wrapper.HasFocus() {
app.App.Stop()
Expand All @@ -145,17 +145,17 @@ 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

switch connection.Provider {
case "mysql":
case drivers.DriverMySQL:
newDbDriver = &drivers.MySQL{}
case "postgres":
case drivers.DriverPostgres:
newDbDriver = &drivers.Postgres{}
case "sqlite3":
case drivers.DriverSqlite:
newDbDriver = &drivers.SQLite{}
}

Expand Down
3 changes: 2 additions & 1 deletion components/ConnectionsTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)

Expand Down
Loading

0 comments on commit c4ac8f1

Please sign in to comment.