diff --git a/ui/ignore.go b/ui/ignore.go new file mode 100644 index 0000000..348b020 --- /dev/null +++ b/ui/ignore.go @@ -0,0 +1,39 @@ +package ui + +import ( + "os" + "strings" +) + +// Returns whether or not the given path contains a file or directory starting +// with a dot. This is relative to the current working directory, so if you're +// in a dot directory and browsing files beneath this function won't return +// true every time. +func isDotFileOrDir(cwd, path string) bool { + p := strings.Replace(path, cwd, "", 1) + for _, v := range strings.Split(p, string(os.PathSeparator)) { + if len(v) > 0 && v[0] == '.' { + return true + } + } + return false +} + +// Returns whether or not a path is a child of a given path. For example: +// +// parent := "/usr/local/bin" +// child := "/usr/local/bin/glow" +// pathIsChild(parent, child) // true +// +func pathIsChild(parent, child string) bool { + if len(parent) == 0 || len(child) == 0 { + return false + } + if len(parent) > len(child) { + return false + } + if strings.Compare(parent, child[:len(parent)]) == 0 { + return true + } + return false +} diff --git a/ui/ignore_darwin.go b/ui/ignore_darwin.go new file mode 100644 index 0000000..43ceb08 --- /dev/null +++ b/ui/ignore_darwin.go @@ -0,0 +1,18 @@ +// +build darwin + +package ui + +import "os" + +func ignorePath(m model, p string) bool { + if isDotFileOrDir(m.cwd, p) { + return true + } + if pathIsChild(m.cfg.Gopath, p) { + return true + } + + // Look for ~/Library on macOS + macOSLibraryPath := m.cfg.HomeDir + string(os.PathSeparator) + "Library" + return pathIsChild(macOSLibraryPath, p) +} diff --git a/ui/ignore_general.go b/ui/ignore_general.go new file mode 100644 index 0000000..487fc85 --- /dev/null +++ b/ui/ignore_general.go @@ -0,0 +1,11 @@ +// +build !darwin + +package ui + +// Whether or not we should ignore the given path +func ignorePath(m model, p string) bool { + if isDotFileOrDir(m.cwd, p) { + return true + } + return pathIsChild(m.cfg.Gopath, p) +} diff --git a/ui/ui.go b/ui/ui.go index 5bf4bf9..a2eb258 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -33,6 +33,8 @@ var ( // Config contains configuration specified to the TUI. type Config struct { ShowAllFiles bool + Gopath string `env:"GOPATH"` + HomeDir string `env:"HOME"` // For debugging the UI Logfile string `env:"GLOW_UI_LOGFILE"` @@ -428,7 +430,7 @@ func findNextLocalFile(m model) tea.Cmd { return func() tea.Msg { res, ok := <-m.localFileFinder - if !m.cfg.ShowAllFiles && isDotFileOrDir(m.cwd, res.Path) { + if !m.cfg.ShowAllFiles && ignorePath(m, res.Path) { if debug { log.Println("ignoring file:", res.Path) } @@ -616,20 +618,6 @@ func localFileToMarkdown(cwd string, res gitcha.SearchResult) *markdown { return md } -// Returns whether or not the given path contains a file or directory starting -// with a dot. This is relative to the current working directory, so if you're -// in a dot directory and browsing files beneath this function won't return -// true every time. -func isDotFileOrDir(cwd, path string) bool { - p := strings.Replace(path, cwd, "", 1) - for _, v := range strings.Split(p, string(os.PathSeparator)) { - if len(v) > 0 && v[0] == '.' { - return true - } - } - return false -} - // Lightweight version of reflow's indent function. func indent(s string, n int) string { if n <= 0 || s == "" {