|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/dundee/gdu/v4/analyze" |
| 9 | +) |
| 10 | + |
| 11 | +// CreateIgnorePattern creates one pattern from all path patterns |
| 12 | +func CreateIgnorePattern(paths []string) (*regexp.Regexp, error) { |
| 13 | + var err error |
| 14 | + |
| 15 | + for i, path := range paths { |
| 16 | + if _, err = regexp.Compile(path); err != nil { |
| 17 | + return nil, err |
| 18 | + } |
| 19 | + paths[i] = "(" + path + ")" |
| 20 | + } |
| 21 | + |
| 22 | + ignore := `^` + strings.Join(paths, "|") + `$` |
| 23 | + return regexp.Compile(ignore) |
| 24 | +} |
| 25 | + |
| 26 | +// SetIgnoreDirPaths sets paths to ignore |
| 27 | +func (ui *UI) SetIgnoreDirPaths(paths []string) { |
| 28 | + log.Printf("Ignoring dirs %s", strings.Join(paths, ", ")) |
| 29 | + ui.IgnoreDirPaths = make(map[string]struct{}, len(paths)) |
| 30 | + for _, path := range paths { |
| 31 | + ui.IgnoreDirPaths[path] = struct{}{} |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// SetIgnoreDirPatterns sets regular patters of dirs to ignore |
| 36 | +func (ui *UI) SetIgnoreDirPatterns(paths []string) error { |
| 37 | + var err error |
| 38 | + log.Printf("Ignoring dir patterns %s", strings.Join(paths, ", ")) |
| 39 | + ui.IgnoreDirPathPatterns, err = CreateIgnorePattern(paths) |
| 40 | + return err |
| 41 | +} |
| 42 | + |
| 43 | +// SetIgnoreHidden sets flags if hidden dirs should be ignored |
| 44 | +func (ui *UI) SetIgnoreHidden(value bool) { |
| 45 | + log.Printf("Ignoring hidden dirs") |
| 46 | + ui.IgnoreHidden = value |
| 47 | +} |
| 48 | + |
| 49 | +// ShouldDirBeIgnored returns true if given path should be ignored |
| 50 | +func (ui *UI) ShouldDirBeIgnored(name, path string) bool { |
| 51 | + _, shouldIgnore := ui.IgnoreDirPaths[path] |
| 52 | + if shouldIgnore { |
| 53 | + log.Printf("Directory %s ignored", path) |
| 54 | + } |
| 55 | + return shouldIgnore |
| 56 | +} |
| 57 | + |
| 58 | +// ShouldDirBeIgnoredUsingPattern returns true if given path should be ignored |
| 59 | +func (ui *UI) ShouldDirBeIgnoredUsingPattern(name, path string) bool { |
| 60 | + shouldIgnore := ui.IgnoreDirPathPatterns.MatchString(path) |
| 61 | + if shouldIgnore { |
| 62 | + log.Printf("Directory %s ignored", path) |
| 63 | + } |
| 64 | + return shouldIgnore |
| 65 | +} |
| 66 | + |
| 67 | +// IsHiddenDir returns if the dir name beggins with dot |
| 68 | +func (ui *UI) IsHiddenDir(name, path string) bool { |
| 69 | + shouldIgnore := name[0] == '.' |
| 70 | + if shouldIgnore { |
| 71 | + log.Printf("Directory %s ignored", path) |
| 72 | + } |
| 73 | + return shouldIgnore |
| 74 | +} |
| 75 | + |
| 76 | +// CreateIgnoreFunc returns function for detecting if dir should be ignored |
| 77 | +func (ui *UI) CreateIgnoreFunc() analyze.ShouldDirBeIgnored { |
| 78 | + switch { |
| 79 | + case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns == nil && !ui.IgnoreHidden: |
| 80 | + return ui.ShouldDirBeIgnored |
| 81 | + case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns != nil && !ui.IgnoreHidden: |
| 82 | + return func(name, path string) bool { |
| 83 | + return ui.ShouldDirBeIgnored(name, path) || ui.ShouldDirBeIgnoredUsingPattern(name, path) |
| 84 | + } |
| 85 | + case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns != nil && ui.IgnoreHidden: |
| 86 | + return func(name, path string) bool { |
| 87 | + return ui.ShouldDirBeIgnored(name, path) || ui.ShouldDirBeIgnoredUsingPattern(name, path) || ui.IsHiddenDir(name, path) |
| 88 | + } |
| 89 | + case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns != nil && ui.IgnoreHidden: |
| 90 | + return func(name, path string) bool { |
| 91 | + return ui.ShouldDirBeIgnoredUsingPattern(name, path) || ui.IsHiddenDir(name, path) |
| 92 | + } |
| 93 | + case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns != nil && !ui.IgnoreHidden: |
| 94 | + return ui.ShouldDirBeIgnoredUsingPattern |
| 95 | + case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns == nil && ui.IgnoreHidden: |
| 96 | + return ui.IsHiddenDir |
| 97 | + case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns == nil && ui.IgnoreHidden: |
| 98 | + return func(name, path string) bool { |
| 99 | + return ui.ShouldDirBeIgnored(name, path) || ui.IsHiddenDir(name, path) |
| 100 | + } |
| 101 | + default: |
| 102 | + return func(name, path string) bool { return false } |
| 103 | + } |
| 104 | +} |
0 commit comments