From 44721f7e6b601293fdf63114fd96b3824d90068d Mon Sep 17 00:00:00 2001 From: Paolo Lazzari Date: Sun, 22 Oct 2023 12:45:20 +0200 Subject: [PATCH] Exclude certain file types from file picker --- cmd/core.go | 2 +- src/ui/ui.go | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/core.go b/cmd/core.go index 5abf4ff..a24c45b 100644 --- a/cmd/core.go +++ b/cmd/core.go @@ -13,7 +13,7 @@ import ( "golang.org/x/term" ) -const version = "0.2.3" +const version = "0.2.4" func completionCommand() *cobra.Command { return &cobra.Command{ diff --git a/src/ui/ui.go b/src/ui/ui.go index 193d8d4..7406ad8 100644 --- a/src/ui/ui.go +++ b/src/ui/ui.go @@ -273,6 +273,17 @@ func (ui *UI) changedText() func() { } } +// Helper function to exclude certain file types from file picker +func isExtensionInvalid(fileExtension string) bool { + invalidExtensions := [...]string{".gif", ".png"} + for _, invalidExtension := range invalidExtensions { + if invalidExtension == fileExtension { + return true + } + } + return false +} + // Helper function for populating nodes of TreeNode func add(target *tview.TreeNode, path string, ui *UI) { files, err := ioutil.ReadDir(path) @@ -296,7 +307,8 @@ func add(target *tview.TreeNode, path string, ui *UI) { scanner.Split(bufio.ScanLines) scanner.Scan() text := string(scanner.Text()) - if utf8.ValidString(text) { + fileExtension := filepath.Ext(file.Name()) + if utf8.ValidString(text) && !isExtensionInvalid(fileExtension) { target.AddChild(node) nodeRef := nodeReference{filepath.Join(path, file.Name()), text} node.SetReference(nodeRef)