Skip to content

Commit

Permalink
add OpenInExternalEditor command to the keymap system
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerojas26 committed Jun 17, 2024
1 parent 887c545 commit 57369c6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/Keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ var Keymaps KeymapSystem = KeymapSystem{
"editor": {
Bind{Key: Key{Code: tcell.KeyCtrlR}, Cmd: Execute},
Bind{Key: Key{Code: tcell.KeyEscape}, Cmd: Quit},
Bind{Key: Key{Code: tcell.KeyCtrlSpace}, Cmd: OpenInExternalEditor},
},
},
}
3 changes: 3 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
Search
Quit
Execute
OpenInExternalEditor
)

func (c Command) String() string {
Expand Down Expand Up @@ -109,6 +110,8 @@ func (c Command) String() string {
return "Quit"
case Execute:
return "Execute"
case OpenInExternalEditor:
return "OpenInExternalEditor"
}
return "Unknown"
}
19 changes: 9 additions & 10 deletions components/SQLEditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"

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

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

text := openExternalEditor(sqlEditor)
Expand Down Expand Up @@ -88,7 +88,6 @@ func (s *SQLEditor) SetBlur() {
s.SetTextStyle(tcell.StyleDefault.Foreground(tcell.ColorWhite))
}


/*
THIS FUNCTION OPENS EXTERNAL EDITOR.
Expand All @@ -107,10 +106,10 @@ func openExternalEditor(s *SQLEditor) string {
content := []byte(s.TextArea.GetText())

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

err := os.WriteFile(path, content, 0644)
Expand Down Expand Up @@ -157,7 +156,7 @@ func getEditor() string {
}

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

return editor
Expand All @@ -170,7 +169,7 @@ func getTerminal() string {
if terminal == "" {
terminal = os.Getenv("TERMINAL")
}

if terminal == "" {
terminal = "xterm"
}
Expand All @@ -180,7 +179,7 @@ func getTerminal() string {

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

return terminal
Expand Down

0 comments on commit 57369c6

Please sign in to comment.