Skip to content

Commit

Permalink
Merge branch 'main' into refactor-dml
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerojas26 committed Aug 30, 2024
2 parents a48cf25 + c325d45 commit be92c60
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 208 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ mssql://user:[email protected]/instance/dbname
ms://user:[email protected]:port/instance/dbname?keepAlive=10
oracle://user:[email protected]/sid
sap://user:pass@localhost/dbname
sqlite:/path/to/file.db
file:myfile.sqlite3?loc=auto
/path/to/sqlite/file/test.db
odbc+postgres://user:pass@localhost:port/dbname?option1=
```

Expand All @@ -221,7 +221,7 @@ odbc+postgres://user:pass@localhost:port/dbname?option1=
- [ ] Columns and indexes creation through TUI
- [ ] Table tree input filter
- [ ] Custom keybindings
- [ ] Show keybindings on a modal
- [x] Show keybindings on a modal
- [ ] Rewrite row `create`, `update` and `delete` logic

See the [open issues](https://github.com/jorgerojas26/lazysql/issues) for a full list of proposed features (and known issues).
Expand Down
57 changes: 37 additions & 20 deletions app/Keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,13 @@ type (
)

// KeymapSystem is the actual key mapping system.
//
// A map can have several groups. But it always has a "Global" one.
type KeymapSystem struct {
Global Map
Groups map[string]Map
Global Map
}

func (c KeymapSystem) Group(name string) Map {
// Global is special.
if name == "global" {
return c.Global
}

// Lookup the group
if group, ok := c.Groups[name]; ok {
return group
Expand All @@ -43,19 +37,35 @@ func (c KeymapSystem) Resolve(event *tcell.EventKey) cmd.Command {
return c.Global.Resolve(event)
}

const (
HomeGroup = "home"
TreeGroup = "tree"
TableGroup = "table"
EditorGroup = "editor"
ConnectionGroup = "connection"
)

// Define a global KeymapSystem object with default keybinds
var Keymaps = KeymapSystem{
Global: Map{
Bind{Key: Key{Char: 'L'}, Cmd: cmd.MoveRight, Description: "Focus table"},
Bind{Key: Key{Char: 'H'}, Cmd: cmd.MoveLeft, Description: "Focus tree"},
Bind{Key: Key{Code: tcell.KeyCtrlE}, Cmd: cmd.SwitchToEditorView, Description: "Open SQL editor"},
Bind{Key: Key{Code: tcell.KeyCtrlS}, Cmd: cmd.Save, Description: "Execute pending changes"},
Bind{Key: Key{Char: 'q'}, Cmd: cmd.Quit, Description: "Quit"},
Bind{Key: Key{Code: tcell.KeyBackspace2}, Cmd: cmd.SwitchToConnectionsView, Description: "Switch to connections list"},
Bind{Key: Key{Char: '?'}, Cmd: cmd.HelpPopup, Description: "Help"},
},
Groups: map[string]Map{
"tree": {
HomeGroup: {
Bind{Key: Key{Char: 'L'}, Cmd: cmd.MoveRight, Description: "Focus table"},
Bind{Key: Key{Char: 'H'}, Cmd: cmd.MoveLeft, Description: "Focus tree"},
Bind{Key: Key{Code: tcell.KeyCtrlE}, Cmd: cmd.SwitchToEditorView, Description: "Open SQL editor"},
Bind{Key: Key{Code: tcell.KeyCtrlS}, Cmd: cmd.Save, Description: "Execute pending changes"},
Bind{Key: Key{Char: 'q'}, Cmd: cmd.Quit, Description: "Quit"},
Bind{Key: Key{Code: tcell.KeyBackspace2}, Cmd: cmd.SwitchToConnectionsView, Description: "Switch to connections list"},
Bind{Key: Key{Char: '?'}, Cmd: cmd.HelpPopup, Description: "Help"},
},
ConnectionGroup: {
Bind{Key: Key{Char: 'n'}, Cmd: cmd.NewConnection, Description: "Create a new database connection"},
Bind{Key: Key{Char: 'c'}, Cmd: cmd.Connect, Description: "Connect to database"},
Bind{Key: Key{Code: tcell.KeyEnter}, Cmd: cmd.Connect, Description: "Connect to database"},
Bind{Key: Key{Char: 'e'}, Cmd: cmd.EditConnection, Description: "Edit a database connection"},
Bind{Key: Key{Char: 'd'}, Cmd: cmd.DeleteConnection, Description: "Delete a database connection"},
Bind{Key: Key{Char: 'q'}, Cmd: cmd.Quit, Description: "Quit"},
},
TreeGroup: {
Bind{Key: Key{Char: 'g'}, Cmd: cmd.GotoTop, Description: "Go to top"},
Bind{Key: Key{Char: 'G'}, Cmd: cmd.GotoBottom, Description: "Go to bottom"},
Bind{Key: Key{Code: tcell.KeyEnter}, Cmd: cmd.Execute, Description: "Open"},
Expand All @@ -64,7 +74,7 @@ var Keymaps = KeymapSystem{
Bind{Key: Key{Char: 'k'}, Cmd: cmd.MoveUp, Description: "Go up"},
Bind{Key: Key{Code: tcell.KeyUp}, Cmd: cmd.MoveUp, Description: "Go up"},
},
"table": {
TableGroup: {
Bind{Key: Key{Char: '/'}, Cmd: cmd.Search, Description: "Search"},
Bind{Key: Key{Char: 'c'}, Cmd: cmd.Edit, Description: "Change cell"},
Bind{Key: Key{Char: 'd'}, Cmd: cmd.Delete, Description: "Delete row"},
Expand All @@ -74,6 +84,8 @@ var Keymaps = KeymapSystem{
Bind{Key: Key{Char: '0'}, Cmd: cmd.GotoStart, Description: "Go to first cell"},
Bind{Key: Key{Char: 'y'}, Cmd: cmd.Copy, Description: "Copy cell to clipboard"},
Bind{Key: Key{Char: 'o'}, Cmd: cmd.AppendNewRow, Description: "Append new row"},
Bind{Key: Key{Char: 'J'}, Cmd: cmd.SortDesc, Description: "Sort descending"},
Bind{Key: Key{Char: 'K'}, Cmd: cmd.SortAsc, Description: "Sort ascending"},
// Tabs
Bind{Key: Key{Char: '['}, Cmd: cmd.TabPrev, Description: "Switch to previous tab"},
Bind{Key: Key{Char: ']'}, Cmd: cmd.TabNext, Description: "Switch to next tab"},
Expand All @@ -83,10 +95,15 @@ var Keymaps = KeymapSystem{
// Pages
Bind{Key: Key{Char: '>'}, Cmd: cmd.PageNext, Description: "Switch to next page"},
Bind{Key: Key{Char: '<'}, Cmd: cmd.PagePrev, Description: "Switch to previous page"},
Bind{Key: Key{Char: '1'}, Cmd: cmd.RecordsMenu, Description: "Switch to records menu"},
Bind{Key: Key{Char: '2'}, Cmd: cmd.ColumnsMenu, Description: "Switch to columns menu"},
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"},
},
"editor": {
EditorGroup: {
Bind{Key: Key{Code: tcell.KeyCtrlR}, Cmd: cmd.Execute, Description: "Execute query"},
Bind{Key: Key{Code: tcell.KeyEscape}, Cmd: cmd.Quit, Description: "Unfocus editor"},
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"},
},
},
Expand Down
43 changes: 43 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ const (
PageNext
PagePrev

// Menu:
RecordsMenu
ColumnsMenu
ConstraintsMenu
ForeignKeysMenu
IndexesMenu

// Tabs
TabNext
TabPrev
Expand All @@ -35,6 +42,7 @@ const (
TabClose

// Operations
UnfocusEditor
Copy
Edit
Save
Expand All @@ -44,6 +52,15 @@ const (
Execute
OpenInExternalEditor
AppendNewRow
SortAsc
SortDesc

// Connection
NewConnection
Connect
TestConnection
EditConnection
DeleteConnection
)

func (c Command) String() string {
Expand Down Expand Up @@ -118,6 +135,32 @@ func (c Command) String() string {
return "OpenInExternalEditor"
case AppendNewRow:
return "AppendNewRow"
case SortAsc:
return "SortAsc"
case SortDesc:
return "SortDesc"
case NewConnection:
return "NewConnection"
case Connect:
return "Connect"
case TestConnection:
return "TestConnection"
case EditConnection:
return "EditConnection"
case DeleteConnection:
return "DeleteConnection"
case UnfocusEditor:
return "UnfocusEditor"
case RecordsMenu:
return "RecordsMenu"
case ColumnsMenu:
return "ColumnsMenu"
case ConstraintsMenu:
return "ConstraintsMenu"
case ForeignKeysMenu:
return "ForeignKeysMenu"
case IndexesMenu:
return "IndexesMenu"
}
return "Unknown"
}
16 changes: 10 additions & 6 deletions components/ConnectionSelection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/rivo/tview"

"github.com/jorgerojas26/lazysql/app"
"github.com/jorgerojas26/lazysql/commands"
"github.com/jorgerojas26/lazysql/drivers"
"github.com/jorgerojas26/lazysql/helpers"
"github.com/jorgerojas26/lazysql/models"
Expand Down Expand Up @@ -77,22 +78,24 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod
wrapper.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
connections := ConnectionListTable.GetConnections()

command := app.Keymaps.Group(app.ConnectionGroup).Resolve(event)

if len(connections) != 0 {
row, _ := ConnectionListTable.GetSelection()
selectedConnection := connections[row]

if event.Rune() == 'c' || event.Key() == tcell.KeyEnter {
switch command {
case commands.Connect:
go cs.Connect(selectedConnection)
} else if event.Rune() == 'e' {
case commands.EditConnection:
connectionPages.SwitchToPage("ConnectionForm")
connectionForm.GetFormItemByLabel("Name").(*tview.InputField).SetText(selectedConnection.Name)
connectionForm.GetFormItemByLabel("URL").(*tview.InputField).SetText(selectedConnection.URL)
connectionForm.StatusText.SetText("")

connectionForm.SetAction("edit")
return nil

} else if event.Rune() == 'd' {
case commands.DeleteConnection:
confirmationModal := NewConfirmationModal("")

confirmationModal.SetDoneFunc(func(_ int, buttonLabel string) {
Expand All @@ -118,13 +121,14 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod
}
}

if event.Rune() == 'n' {
switch command {
case commands.NewConnection:
connectionForm.SetAction("create")
connectionForm.GetFormItemByLabel("Name").(*tview.InputField).SetText("")
connectionForm.GetFormItemByLabel("URL").(*tview.InputField).SetText("")
connectionForm.StatusText.SetText("")
connectionPages.SwitchToPage("ConnectionForm")
} else if event.Rune() == 'q' {
case commands.Quit:
if wrapper.HasFocus() {
app.App.Stop()
}
Expand Down
15 changes: 3 additions & 12 deletions components/HelpModal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

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

type HelpModal struct {
Expand Down Expand Up @@ -37,13 +36,7 @@ func NewHelpModal() *HelpModal {
table.SetSelectable(true, false)
table.SetSelectedStyle(tcell.StyleDefault.Background(tview.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor))

keymapGroups := make(map[string]keymap.Map, len(app.Keymaps.Groups)+1)

keymapGroups["global"] = app.Keymaps.Global

for name, group := range app.Keymaps.Groups {
keymapGroups[name] = group
}
keymapGroups := app.Keymaps.Groups

mostLengthyKey := ""

Expand Down Expand Up @@ -80,10 +73,8 @@ func NewHelpModal() *HelpModal {
table.Select(3, 0)

table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
command := app.Keymaps.Group("global").Resolve(event)
if command == commands.Quit {
App.Stop()
} else if command == commands.HelpPopup {
command := app.Keymaps.Group(app.HomeGroup).Resolve(event)
if command == commands.Quit || command == commands.HelpPopup {
MainPages.RemovePage(HelpPageName)
}
return event
Expand Down
8 changes: 4 additions & 4 deletions components/HelpStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type HelpStatus struct {
}

func NewHelpStatus() HelpStatus {

status := HelpStatus{tview.NewTextView().SetTextColor(tview.Styles.TertiaryTextColor)}

status.SetStatusOnTree()
Expand All @@ -21,7 +20,6 @@ func NewHelpStatus() HelpStatus {
}

func (status *HelpStatus) UpdateText(binds []keymap.Bind) {

newtext := ""

for i, key := range binds {
Expand All @@ -46,9 +44,11 @@ func (status *HelpStatus) UpdateText(binds []keymap.Bind) {
func (status *HelpStatus) SetStatusOnTree() {
status.UpdateText(app.Keymaps.Global)
}

func (status *HelpStatus) SetStatusOnEditorView() {
status.UpdateText(app.Keymaps.Group("editor"))
status.UpdateText(app.Keymaps.Group(app.EditorGroup))
}

func (status *HelpStatus) SetStatusOnTableView() {
status.UpdateText(app.Keymaps.Group("table"))
status.UpdateText(app.Keymaps.Group(app.TableGroup))
}
Loading

0 comments on commit be92c60

Please sign in to comment.