From 9ad88542bc3ca1e7309d3810ecd07516b48e88c9 Mon Sep 17 00:00:00 2001 From: Ivo Maceira Date: Mon, 5 May 2025 22:22:14 +0200 Subject: [PATCH] Use openDirInEditor when editing a single dir --- pkg/gui/controllers/files_controller.go | 12 ++++++++++-- pkg/gui/controllers/list_controller_trait.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 6df28a523..36e245ec5 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -90,8 +90,8 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types }, { Key: opts.GetKey(opts.Config.Universal.Edit), - Handler: self.withItems(self.edit), - GetDisabledReason: self.require(self.itemsSelected(self.canEditFiles)), + Handler: self.withItems(self.editOrOpenDir), + GetDisabledReason: self.any(self.singleItemSelected(), self.itemsSelected(self.canEditFiles)), Description: self.c.Tr.Edit, Tooltip: self.c.Tr.EditFileTooltip, DisplayOnScreen: true, @@ -913,6 +913,14 @@ func (self *FilesController) edit(nodes []*filetree.FileNode) error { })) } +func (self *FilesController) editOrOpenDir(nodes []*filetree.FileNode) error { + if len(nodes) == 1 && !nodes[0].IsFile() { + return self.c.Helpers().Files.OpenDirInEditor(nodes[0].GetPath()) + } else { + return self.edit(nodes) + } +} + func (self *FilesController) canEditFiles(nodes []*filetree.FileNode) *types.DisabledReason { if lo.NoneBy(nodes, func(node *filetree.FileNode) bool { return node.IsFile() }) { return &types.DisabledReason{ diff --git a/pkg/gui/controllers/list_controller_trait.go b/pkg/gui/controllers/list_controller_trait.go index fa5fc1492..c9a422962 100644 --- a/pkg/gui/controllers/list_controller_trait.go +++ b/pkg/gui/controllers/list_controller_trait.go @@ -44,6 +44,21 @@ func (self *ListControllerTrait[T]) require(callbacks ...func() *types.DisabledR } } +// Complement to require - returns nil if any of the provided callbacks return nil. +// If all callbacks return a non-nil DisabledReason, it returns the last one encountered. +func (self *ListControllerTrait[T]) any(callbacks ...func() *types.DisabledReason) func() *types.DisabledReason { + return func() *types.DisabledReason { + var disabledReason *types.DisabledReason + for _, callback := range callbacks { + if disabledReason := callback(); disabledReason == nil { + return disabledReason + } + } + + return disabledReason + } +} + // Convenience function for enforcing that a single item is selected. // Also takes callbacks for additional disabled reasons, and passes the selected // item into each one.