From a3147ec3e95a105098d49a449977a1e9d8c4c32f Mon Sep 17 00:00:00 2001 From: Night Cat <107802416+MHNightCat@users.noreply.github.com> Date: Mon, 22 Apr 2024 23:29:28 +0800 Subject: [PATCH 1/2] update open file with editor --- src/components/config_type.go | 3 +++ src/components/global_controller.go | 14 +++++++++++++ src/components/key_function.go | 9 ++++++--- src/components/model.go | 13 ++++++++---- src/components/type.go | 31 ++++++++++++++++------------- src/superfile/hotkeys.toml | 3 +++ 6 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/components/config_type.go b/src/components/config_type.go index f713d89b..a7bd1777 100644 --- a/src/components/config_type.go +++ b/src/components/config_type.go @@ -131,6 +131,9 @@ type HotkeysType struct { CompressFile []string `toml:"compress_file"` ToggleDotFile []string `toml:"toggle_dot_file"` + OpenFileWithEditor []string `toml:"oepn_file_with_editor"` + OpenCurrentDirectoryWithEditor []string `toml:"open_current_directory_with_editor"` + Cancel []string `toml:"cancel"` Confirm []string `toml:"confirm"` diff --git a/src/components/global_controller.go b/src/components/global_controller.go index 8356505a..c0971ebe 100644 --- a/src/components/global_controller.go +++ b/src/components/global_controller.go @@ -3,6 +3,7 @@ package components import ( "encoding/json" "os" + "os/exec" "path" "path/filepath" "runtime" @@ -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" ) @@ -489,3 +491,15 @@ func compressFile(m model) model { m.fileModel.filePanels[m.filePanelFocusIndex] = panel return m } + +func openFileWithEditor(m model) tea.Cmd { + editor := os.Getenv("EDITOR") + m.editorMode = true + if editor == "" { + editor = "nano" + } + c := exec.Command(editor) + return tea.ExecProcess(c, func(err error) tea.Msg { + return editorFinishedMsg{err} + }) +} \ No newline at end of file diff --git a/src/components/key_function.go b/src/components/key_function.go index 2562a9a7..e38a4aed 100644 --- a/src/components/key_function.go +++ b/src/components/key_function.go @@ -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 @@ -77,14 +79,15 @@ 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) default: m = normalAndBrowserModeKey(msg, m) } - return m + return m, cmd } func normalAndBrowserModeKey(msg string, m model) model { diff --git a/src/components/model.go b/src/components/model.go index 52e3d35f..2f38265b 100644 --- a/src/components/model.go +++ b/src/components/model.go @@ -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 @@ -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 + } } } @@ -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 { @@ -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 { diff --git a/src/components/type.go b/src/components/type.go index 02ef2ab2..f46fec0c 100644 --- a/src/components/type.go +++ b/src/components/type.go @@ -75,6 +75,7 @@ type model struct { fileMetaData fileMetadata firstTextInput bool toggleDotFile bool + editorMode bool filePanelFocusIndex int mainPanelHeight int fullWidth int @@ -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 } @@ -212,3 +213,5 @@ type iconStyle struct { icon string color string } + +type editorFinishedMsg struct{ err error } diff --git a/src/superfile/hotkeys.toml b/src/superfile/hotkeys.toml index 9bbb6403..3f476d9f 100644 --- a/src/superfile/hotkeys.toml +++ b/src/superfile/hotkeys.toml @@ -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) From 6d6d9e02b9d319700bab51bd9eb8abbe65206e6e Mon Sep 17 00:00:00 2001 From: Night Cat <107802416+MHNightCat@users.noreply.github.com> Date: Mon, 22 Apr 2024 23:33:39 +0800 Subject: [PATCH 2/2] update open location or directory --- src/components/global_controller.go | 17 ++++++++++++++++- src/components/key_function.go | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/components/global_controller.go b/src/components/global_controller.go index c0971ebe..a2146817 100644 --- a/src/components/global_controller.go +++ b/src/components/global_controller.go @@ -493,12 +493,27 @@ func compressFile(m model) model { } 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) + c := exec.Command(editor, m.fileModel.filePanels[m.filePanelFocusIndex].location) return tea.ExecProcess(c, func(err error) tea.Msg { return editorFinishedMsg{err} }) diff --git a/src/components/key_function.go b/src/components/key_function.go index e38a4aed..e90a40c9 100644 --- a/src/components/key_function.go +++ b/src/components/key_function.go @@ -84,6 +84,8 @@ func mainKey(msg string, m model, cmd tea.Cmd) (model, tea.Cmd) { }() 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) }