Skip to content

Commit

Permalink
Merge branch 'externEditor'
Browse files Browse the repository at this point in the history
Resolved merge conflict
  • Loading branch information
mithcs committed Jun 14, 2024
2 parents f7b6c31 + 220e237 commit b02f94c
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 11 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,13 @@ Support for multiple RDBMS is a work in progress.

### SQL Editor

| Key | Action |
| -------- | --------------------- |
| CTRL + R | Run the SQL statement |
| Key | Action |
| ------------ | --------------------------------- |
| CTRL + R | Run the SQL statement |
| CTRL + Space | Open external editor(Linux only) |

Specific editor for lazysql can be set by `$SQL_EDITOR`.
Specific terminal for opening editor can be set by `$SQL_TERMINAL`

## Example connection URLs

Expand Down
118 changes: 110 additions & 8 deletions components/SQLEditor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package components

import (
"github.com/jorgerojas26/lazysql/app"
"github.com/jorgerojas26/lazysql/commands"
"github.com/jorgerojas26/lazysql/models"
"os"
"os/exec"
"runtime"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"

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

type SQLEditorState struct {
Expand All @@ -21,18 +25,15 @@ type SQLEditor struct {

func NewSQLEditor() *SQLEditor {
textarea := tview.NewTextArea()

textarea.SetBorder(true)
textarea.SetTitleAlign(tview.AlignLeft)
textarea.SetPlaceholder("Enter your SQL query here...")

sqlEditor := &SQLEditor{
TextArea: textarea,
state: &SQLEditorState{
isFocused: false,
},
}

sqlEditor.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
command := app.Keymaps.Group("editor").Resolve(event)

Expand All @@ -41,11 +42,16 @@ func NewSQLEditor() *SQLEditor {
return nil
} else if command == commands.Quit {
sqlEditor.Publish("Escape", "")
}
} else if event.Key() == tcell.KeyCtrlSpace && runtime.GOOS == "linux" {
// ----- THIS IS A LINUX-ONLY FEATURE, for now

text := openExternalEditor(sqlEditor)

// Set the text from file
sqlEditor.TextArea.SetText(text, true)
}
return event
})

return sqlEditor
}

Expand Down Expand Up @@ -81,3 +87,99 @@ func (s *SQLEditor) SetBlur() {
s.SetBorderColor(tcell.ColorWhite)
s.SetTextStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite))
}


/*
THIS FUNCTION OPENS EXTERNAL EDITOR.
Q: WHY OPEN ANOTHER TERMINAL?
A: OPENING EDITORS LIKE VIM/NEOVIM REALLY MESSED UP INITIAL TERMINAL'S OUTPUT.
*/

func openExternalEditor(s *SQLEditor) string {
// Current folder as path of temporary file
path := "./lazysql.sql"

editor := getEditor()
terminal := getTerminal()

// Create a temporary file with the current SQL query content
content := []byte(s.TextArea.GetText())

/*
0644 Permission
* User: read & write
* Group: read
* Other: read
*/

err := os.WriteFile(path, content, 0644)
if err != nil {
return s.TextArea.GetText()
}

// Remove the temporary file with the end of function
defer os.Remove(path)

// Setup command
cmd := exec.Command(terminal, "-e", editor, path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout

// cmd.Stderr = os.Stderr

err = cmd.Run()
if err != nil {
return s.TextArea.GetText()
}

// Read the updated content from the temporary file
updatedContent, err := os.ReadFile(path)
if err != nil {
return s.TextArea.GetText()
}

// Convert to string before returning
return string(updatedContent)
}

// Function to select editor
func getEditor() string {
var editor string = os.Getenv("SQL_EDITOR")
if editor == "" {
editor = os.Getenv("EDITOR")
}

if editor == "" {
editor = os.Getenv("VISUAL")
}

if editor == "" {
editor = "vi" // use "vi" if $EDITOR not set
}

return editor
}

// Function to select terminal
func getTerminal() string {
var terminal string = os.Getenv("SQL_TERMINAL")

if terminal == "" {
terminal = os.Getenv("TERMINAL")
}

// Check if x-terminal-emulator exists.
x_term_emu, err := exec.LookPath("x-terminal-emulator")

// If exists then set terminal as x-terminal-emulator
if err == nil {
terminal = x_term_emu
}

if terminal == "" {
terminal = "xterm" // use "xterm" if none of the above exists
}

return terminal
}

0 comments on commit b02f94c

Please sign in to comment.