From 327b0cd3f96a489889e6111310983125240b5ed4 Mon Sep 17 00:00:00 2001 From: Oscar Torresbaca Date: Fri, 24 Jan 2025 13:54:12 +0100 Subject: [PATCH] adding functionality to reload the external file (lazysql.sql) and execute the query with F2 --- app/keymap.go | 1 + commands/commands.go | 3 +++ components/sql_editor.go | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/app/keymap.go b/app/keymap.go index 71859f9..2c3a320 100644 --- a/app/keymap.go +++ b/app/keymap.go @@ -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"}, diff --git a/commands/commands.go b/commands/commands.go index 4a4cf45..96c3a0a 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -54,6 +54,7 @@ const ( Quit Execute OpenInExternalEditor + RefreshExternalFile AppendNewRow SortAsc SortDesc @@ -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: diff --git a/components/sql_editor.go b/components/sql_editor.go index f80867f..410ef73 100644 --- a/components/sql_editor.go +++ b/components/sql_editor.go @@ -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 @@ -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.