Ignore $GOPATH everywhere and ~/Library on macOS

This commit is contained in:
Christian Rocha 2020-09-07 14:26:02 -04:00 committed by Christian Muehlhaeuser
parent 5b20e3edae
commit 9903ea8dcf
4 changed files with 71 additions and 15 deletions

39
ui/ignore.go Normal file
View file

@ -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
}

18
ui/ignore_darwin.go Normal file
View file

@ -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)
}

11
ui/ignore_general.go Normal file
View file

@ -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)
}

View file

@ -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 == "" {