Skip to content

Commit

Permalink
adding functionality to reload the external file (lazysql.sql) and ex…
Browse files Browse the repository at this point in the history
…ecute the query with F2
  • Loading branch information
oskretc committed Jan 24, 2025
1 parent 65b46fa commit 327b0cd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ var Keymaps = KeymapSystem{
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"},
Bind{Key: Key{Code: tcell.KeyF2}, Cmd: cmd.RefreshExternalFile, Description: "Refresh external file and execute query"},
},
SidebarGroup: {
Bind{Key: Key{Char: 's'}, Cmd: cmd.UnfocusSidebar, Description: "Focus table"},
Expand Down
3 changes: 3 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
Quit
Execute
OpenInExternalEditor
RefreshExternalFile
AppendNewRow
SortAsc
SortDesc
Expand Down Expand Up @@ -146,6 +147,8 @@ func (c Command) String() string {
return "Execute"
case OpenInExternalEditor:
return "OpenInExternalEditor"
case RefreshExternalFile:
return "RefreshExternalFile"
case AppendNewRow:
return "AppendNewRow"
case SortAsc:
Expand Down
22 changes: 22 additions & 0 deletions components/sql_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func NewSQLEditor() *SQLEditor {
text := openExternalEditor(sqlEditor)
sqlEditor.SetText(text, true)
}
case commands.RefreshExternalFile:

text := refreshExternalFile(sqlEditor)
sqlEditor.SetText(text, true)
sqlEditor.Publish(eventSQLEditorQuery, sqlEditor.GetText())
}

return event
Expand Down Expand Up @@ -92,6 +97,23 @@ func (s *SQLEditor) SetBlur() {
s.SetTextStyle(tcell.StyleDefault.Foreground(app.Styles.InverseTextColor))
}

/*
Function to refresh the external temporary file (lazysql.sql) from the CWD and load it to the SQLEditor instance
TODO: ability to pass the name of the file to load.
*/
func refreshExternalFile(s *SQLEditor) string {

// Current folder as path of temporary file
path := "./lazysql.sql"

updatedContent, err := os.ReadFile(path)

if err != nil {
return s.GetText()
}
return string(updatedContent)
}

/*
THIS FUNCTION OPENS EXTERNAL EDITOR.
Expand Down

0 comments on commit 327b0cd

Please sign in to comment.