Skip to content

Commit 857943d

Browse files
committed
feat: add menu modal to select a value
1 parent 7cd8dd2 commit 857943d

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

Diff for: app/Keymap.go

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ var Keymaps = KeymapSystem{
9696
Bind{Key: Key{Char: 'o'}, Cmd: cmd.AppendNewRow, Description: "Append new row"},
9797
Bind{Key: Key{Char: 'J'}, Cmd: cmd.SortDesc, Description: "Sort descending"},
9898
Bind{Key: Key{Char: 'K'}, Cmd: cmd.SortAsc, Description: "Sort ascending"},
99+
Bind{Key: Key{Char: 'C'}, Cmd: cmd.SetValue, Description: "Toggle value menu to put values like NULL, EMPTY or DEFAULT"},
99100
// Tabs
100101
Bind{Key: Key{Char: '['}, Cmd: cmd.TabPrev, Description: "Switch to previous tab"},
101102
Bind{Key: Key{Char: ']'}, Cmd: cmd.TabNext, Description: "Switch to next tab"},

Diff for: commands/commands.go

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const (
6060
PreviousFoundNode
6161
TreeCollapseAll
6262
ExpandAll
63+
SetValue
6364

6465
// Connection
6566
NewConnection
@@ -179,6 +180,9 @@ func (c Command) String() string {
179180
return "TreeCollapseAll"
180181
case ExpandAll:
181182
return "ExpandAll"
183+
case SetValue:
184+
return "SetValue"
182185
}
186+
183187
return "Unknown"
184188
}

Diff for: components/ResultsTable.go

+19
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,19 @@ func (table *ResultsTable) tableInputCapture(event *tcell.EventKey) *tcell.Event
367367
}
368368

369369
}
370+
} else if command == commands.SetValue {
371+
table.SetIsEditing(true)
372+
table.SetInputCapture(nil)
373+
374+
cell := table.GetCell(selectedRowIndex, selectedColumnIndex)
375+
x, y, width := cell.GetLastPosition()
376+
377+
list := NewSetValueList()
378+
list.SetRect(x, y, width, 7)
379+
380+
list.OnFinish(table.FinishSettingValue)
381+
382+
list.Show(x, y, width)
370383
}
371384

372385
if len(table.GetRecords()) > 0 {
@@ -1224,3 +1237,9 @@ func (table *ResultsTable) search() {
12241237

12251238
table.SetInputCapture(nil)
12261239
}
1240+
1241+
func (table *ResultsTable) FinishSettingValue() {
1242+
table.SetIsEditing(false)
1243+
table.SetInputCapture(table.tableInputCapture)
1244+
App.SetFocus(table)
1245+
}

Diff for: components/SetValueList.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package components
2+
3+
import (
4+
"github.com/gdamore/tcell/v2"
5+
"github.com/jorgerojas26/lazysql/app"
6+
"github.com/jorgerojas26/lazysql/commands"
7+
"github.com/rivo/tview"
8+
)
9+
10+
type SetValueList struct {
11+
*tview.List
12+
}
13+
14+
type value struct {
15+
value string
16+
key rune
17+
}
18+
19+
var VALUES = []value{
20+
{value: "NULL", key: 'n'},
21+
{value: "EMPTY", key: 'e'},
22+
{value: "DEFAULT", key: 'd'},
23+
}
24+
25+
func NewSetValueList() *SetValueList {
26+
list := tview.NewList()
27+
list.SetBorder(true)
28+
29+
for _, value := range VALUES {
30+
list.AddItem(value.value, "", value.key, nil)
31+
}
32+
33+
return &SetValueList{List: list}
34+
}
35+
36+
func (list *SetValueList) OnFinish(callback func()) {
37+
list.SetDoneFunc(func() {
38+
list.Hide()
39+
callback()
40+
})
41+
42+
list.SetSelectedFunc(func(int, string, string, rune) {
43+
list.Hide()
44+
callback()
45+
})
46+
47+
list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
48+
command := app.Keymaps.Group(app.TableGroup).Resolve(event)
49+
50+
if command == commands.SetValue {
51+
list.Hide()
52+
callback()
53+
return nil
54+
}
55+
56+
return event
57+
})
58+
}
59+
60+
func (list *SetValueList) Show(x, y, width int) {
61+
list.SetRect(x, y, width, len(VALUES)*2+1)
62+
MainPages.AddPage("setValue", list, false, true)
63+
App.SetFocus(list)
64+
App.ForceDraw()
65+
}
66+
67+
func (list *SetValueList) Hide() {
68+
MainPages.RemovePage("setValue")
69+
App.SetFocus(list)
70+
App.ForceDraw()
71+
}

0 commit comments

Comments
 (0)