Skip to content

Commit

Permalink
Merge pull request #73 from MHNightCat/open-with-editor
Browse files Browse the repository at this point in the history
Open with editor
  • Loading branch information
yorukot authored Apr 22, 2024
2 parents 92e4592 + d982115 commit 31b05bb
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
29 changes: 29 additions & 0 deletions src/components/global_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package components
import (
"encoding/json"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
Expand All @@ -11,6 +12,7 @@ import (

"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/lithammer/shortuuid"
"github.com/rkoesters/xdg/trash"
)
Expand Down Expand Up @@ -489,3 +491,30 @@ func compressFile(m model) model {
m.fileModel.filePanels[m.filePanelFocusIndex] = panel
return m
}

func openFileWithEditor(m model) tea.Cmd {
panel := m.fileModel.filePanels[m.filePanelFocusIndex]

editor := os.Getenv("EDITOR")
m.editorMode = true
if editor == "" {
editor = "nano"
}
c := exec.Command(editor, panel.element[panel.cursor].location)

return tea.ExecProcess(c, func(err error) tea.Msg {
return editorFinishedMsg{err}
})
}

func openDirectoryWithEditor(m model) tea.Cmd {
editor := os.Getenv("EDITOR")
m.editorMode = true
if editor == "" {
editor = "nano"
}
c := exec.Command(editor, m.fileModel.filePanels[m.filePanelFocusIndex].location)
return tea.ExecProcess(c, func(err error) tea.Msg {
return editorFinishedMsg{err}
})
}
11 changes: 8 additions & 3 deletions src/components/key_function.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package components

func mainKey(msg string, m model) model {
import tea "github.com/charmbracelet/bubbletea"

func mainKey(msg string, m model, cmd tea.Cmd) (model, tea.Cmd) {
switch msg {
/* LIST CONTROLLER START */
// up list
Expand Down Expand Up @@ -77,14 +79,17 @@ func mainKey(msg string, m model) model {
m = extractFile(m)
}()
case hotkeys.CompressFile[0], hotkeys.CompressFile[1]:

go func() {
m = compressFile(m)
}()
case hotkeys.OpenFileWithEditor[0], hotkeys.OpenFileWithEditor[1]:
cmd = openFileWithEditor(m)
case hotkeys.OpenCurrentDirectoryWithEditor[0], hotkeys.OpenFileWithEditor[1]:
cmd = openDirectoryWithEditor(m)
default:
m = normalAndBrowserModeKey(msg, m)
}
return m
return m, cmd
}

func normalAndBrowserModeKey(msg string, m model) model {
Expand Down
13 changes: 9 additions & 4 deletions src/components/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
switch msg := msg.(type) {
case channelMessage:
if msg.returnWarnModal {
if msg.returnWarnModal {
m.warnModal = msg.warnModal
} else if msg.loadMetadata {
m.fileMetaData.metaData = msg.metadata
Expand Down Expand Up @@ -136,7 +136,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg.String() == hotkeys.Quit[0] || msg.String() == hotkeys.Quit[1] {
return m, tea.Quit
}
m = mainKey(msg.String(), m)
m, cmd = mainKey(msg.String(), m, cmd)
if m.editorMode {
m.editorMode = false
return m, cmd
}
}
}

Expand Down Expand Up @@ -169,7 +173,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if len(filePanel.element) > 500 && (len(filePanel.element) > 500 && (nowTime.Sub(filePanel.lastTimeGetElement) > 3*time.Second)) && !forceReloadElement {
continue
}

if filePanel.searchBar.Value() != "" {
fileElenent = returnFolderElementBySearchString(filePanel.location, m.toggleDotFile, filePanel.searchBar.Value())
} else {
Expand All @@ -180,7 +184,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.fileModel.filePanels[i].lastTimeGetElement = nowTime
forceReloadElement = false
}
return m, cmd

return m, tea.Batch(cmd)
}

func (m model) View() string {
Expand Down
31 changes: 17 additions & 14 deletions src/components/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type model struct {
fileMetaData fileMetadata
firstTextInput bool
toggleDotFile bool
editorMode bool
filePanelFocusIndex int
mainPanelHeight int
fullWidth int
Expand Down Expand Up @@ -119,25 +120,25 @@ type originalPanel struct {
/* FILE WINDOWS TYPE START*/
// Model for file windows
type fileModel struct {
filePanels []filePanel
width int
renaming bool
filePanels []filePanel
width int
renaming bool
maxFilePanel int
}

// Panel representing a file
type filePanel struct {
cursor int
render int
focusType filePanelFocusType
location string
panelMode panelMode
selected []string
element []element
directoryRecord map[string]directoryRecord
rename textinput.Model
renaming bool
searchBar textinput.Model
cursor int
render int
focusType filePanelFocusType
location string
panelMode panelMode
selected []string
element []element
directoryRecord map[string]directoryRecord
rename textinput.Model
renaming bool
searchBar textinput.Model
lastTimeGetElement time.Time
}

Expand Down Expand Up @@ -212,3 +213,5 @@ type iconStyle struct {
icon string
color string
}

type editorFinishedMsg struct{ err error }
3 changes: 3 additions & 0 deletions src/superfile/hotkeys.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ paste_item = ["ctrl+v", ""]
extract_file = ["ctrl+e", ""]
compress_file = ["ctrl+r", ""]

oepn_file_with_editor = ["e", ""]
open_current_directory_with_editor = ["E", ""]

toggle_dot_file = ["ctrl+h", ""]

# These hotkeys do not conflict with any other keys (including global hotkey)
Expand Down

0 comments on commit 31b05bb

Please sign in to comment.