reverse hidden flag; ignore hidden files by default

This commit is contained in:
Lilian Jónsdóttir 2024-06-30 15:59:14 -07:00
parent 120af1476c
commit b26ac28b7d
4 changed files with 34 additions and 30 deletions

View file

@ -105,7 +105,7 @@ func walk_dir(dir string, f *filter.Filter) (files Files) {
return nil return nil
} }
if is_in_recursive_dir(dir, path) && f.IgnoreHidden() { if is_in_recursive_dir(dir, path) && f.ShowHidden() {
return nil return nil
} }

View file

@ -18,7 +18,7 @@ type Filter struct {
unglob, unpattern string unglob, unpattern string
filenames []string filenames []string
dirsonly, filesonly bool dirsonly, filesonly bool
ignorehidden bool showhidden bool
matcher *regexp.Regexp matcher *regexp.Regexp
unmatcher *regexp.Regexp unmatcher *regexp.Regexp
} }
@ -31,7 +31,7 @@ func (f *Filter) Pattern() string { return f.pattern }
func (f *Filter) FileNames() []string { return f.filenames } func (f *Filter) FileNames() []string { return f.filenames }
func (f *Filter) FilesOnly() bool { return f.filesonly } func (f *Filter) FilesOnly() bool { return f.filesonly }
func (f *Filter) DirsOnly() bool { return f.dirsonly } func (f *Filter) DirsOnly() bool { return f.dirsonly }
func (f *Filter) IgnoreHidden() bool { return f.ignorehidden } func (f *Filter) ShowHidden() bool { return f.showhidden }
func (f *Filter) AddFileName(filename string) { func (f *Filter) AddFileName(filename string) {
filename = filepath.Clean(filename) filename = filepath.Clean(filename)
@ -85,7 +85,7 @@ func (f *Filter) Match(filename string, modified time.Time, isdir bool) bool {
if f.dirsonly && !isdir { if f.dirsonly && !isdir {
return false return false
} }
if f.ignorehidden && strings.HasPrefix(filename, ".") { if !f.showhidden && strings.HasPrefix(filename, ".") {
return false return false
} }
// okay it was good // okay it was good
@ -116,7 +116,7 @@ func (f *Filter) Blank() bool {
f.before.Equal(t) && f.before.Equal(t) &&
f.on.Equal(t) && f.on.Equal(t) &&
len(f.filenames) == 0 && len(f.filenames) == 0 &&
!f.ignorehidden && !f.showhidden &&
!f.filesonly && !f.filesonly &&
!f.dirsonly !f.dirsonly
} }
@ -136,7 +136,7 @@ func (f *Filter) String() string {
f.unglob, unm, f.unglob, unm,
f.filenames, f.filenames,
f.filesonly, f.dirsonly, f.filesonly, f.dirsonly,
f.ignorehidden, f.showhidden,
) )
} }
@ -154,7 +154,7 @@ func (f *Filter) has_unregex() bool {
return f.unmatcher.String() != "" return f.unmatcher.String() != ""
} }
func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly, dirsonly, ignorehidden bool, names ...string) (*Filter, error) { func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly, dirsonly, showhidden bool, names ...string) (*Filter, error) {
var ( var (
err error err error
now = time.Now() now = time.Now()
@ -165,7 +165,7 @@ func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly,
unglob: unglob, unglob: unglob,
filesonly: filesonly, filesonly: filesonly,
dirsonly: dirsonly, dirsonly: dirsonly,
ignorehidden: ignorehidden, showhidden: showhidden,
} }
f.AddFileNames(names...) f.AddFileNames(names...)

View file

@ -26,16 +26,16 @@ type testholder struct {
before, after, on string before, after, on string
filenames []string filenames []string
filesonly, dirsonly bool filesonly, dirsonly bool
ignorehidden bool showhidden bool
good, bad []singletest good, bad []singletest
} }
func (t testholder) String() string { func (t testholder) String() string {
return fmt.Sprintf( return fmt.Sprintf(
"pattern:'%s' glob:'%s' unpattern:'%s' unglob:'%s' filenames:'%v' "+ "pattern:'%s' glob:'%s' unpattern:'%s' unglob:'%s' filenames:'%v' "+
"before:'%s' after:'%s' on:'%s' filesonly:'%t' dirsonly:'%t' ignorehidden:'%t'", "before:'%s' after:'%s' on:'%s' filesonly:'%t' dirsonly:'%t' showhidden:'%t'",
t.pattern, t.glob, t.unpattern, t.unglob, t.filenames, t.before, t.after, t.on, t.pattern, t.glob, t.unpattern, t.unglob, t.filenames, t.before, t.after, t.on,
t.filesonly, t.dirsonly, t.ignorehidden, t.filesonly, t.dirsonly, t.showhidden,
) )
} }
@ -58,7 +58,7 @@ func testmatch(t *testing.T, testers []testholder) {
for _, tester := range testers { for _, tester := range testers {
f, err = New( f, err = New(
tester.on, tester.before, tester.after, tester.glob, tester.pattern, tester.on, tester.before, tester.after, tester.glob, tester.pattern,
tester.unglob, tester.unpattern, tester.filesonly, tester.dirsonly, tester.ignorehidden, tester.unglob, tester.unpattern, tester.filesonly, tester.dirsonly, tester.showhidden,
tester.filenames..., tester.filenames...,
) )
if err != nil { if err != nil {
@ -375,13 +375,17 @@ func TestFilterDirsOnly(t *testing.T) {
}) })
} }
func TestFilterIgnoreHidden(t *testing.T) { func TestFilterShowHidden(t *testing.T) {
testmatch(t, []testholder{ testmatch(t, []testholder{
{ {
ignorehidden: true, showhidden: false,
good: append(blanktimedir("test", "alsotest", "helloworld"), blanktimefile("test", "alsotest", "helloworld")...), good: append(blanktimedir("test", "alsotest", "helloworld"), blanktimefile("test", "alsotest", "helloworld")...),
bad: append(blanktimedir(".test", ".alsotest", ".helloworld"), blanktimefile(".test", ".alsotest", ".helloworld")...), bad: append(blanktimedir(".test", ".alsotest", ".helloworld"), blanktimefile(".test", ".alsotest", ".helloworld")...),
}, },
{
showhidden: true,
good: append(blanktimedir("test", "alsotest", ".helloworld"), blanktimefile("test", "alsotest", ".helloworld")...),
},
}) })
} }
@ -534,7 +538,7 @@ func TestFilterNotBlank(t *testing.T) {
dirsonly: true, dirsonly: true,
}, },
{ {
ignorehidden: true, showhidden: true,
}, },
} }
) )
@ -543,7 +547,7 @@ func TestFilterNotBlank(t *testing.T) {
t.Run("notblank"+tester.String(), func(t *testing.T) { t.Run("notblank"+tester.String(), func(t *testing.T) {
f, _ = New( f, _ = New(
tester.on, tester.before, tester.after, tester.glob, tester.pattern, tester.on, tester.before, tester.after, tester.glob, tester.pattern,
tester.unglob, tester.unpattern, tester.filesonly, tester.dirsonly, tester.ignorehidden, tester.unglob, tester.unpattern, tester.filesonly, tester.dirsonly, tester.showhidden,
tester.filenames..., tester.filenames...,
) )
if f.Blank() { if f.Blank() {

16
main.go
View file

@ -34,7 +34,7 @@ var (
f *filter.Filter f *filter.Filter
o, b, a, g, p string o, b, a, g, p string
ung, unp string ung, unp string
fo, do, ih bool fo, do, sh bool
askconfirm bool askconfirm bool
workdir, ogdir cli.Path workdir, ogdir cli.Path
recursive bool recursive bool
@ -91,7 +91,7 @@ var (
) )
if f == nil { if f == nil {
f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, ih) f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, sh)
} }
if err != nil { if err != nil {
return err return err
@ -115,7 +115,7 @@ var (
beforeCommands = func(ctx *cli.Context) (err error) { beforeCommands = func(ctx *cli.Context) (err error) {
// setup filter // setup filter
if f == nil { if f == nil {
f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, ih, ctx.Args().Slice()...) f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, sh, ctx.Args().Slice()...)
} }
log.Debugf("filter: %s", f.String()) log.Debugf("filter: %s", f.String())
return return
@ -123,7 +123,7 @@ var (
beforeTrash = func(_ *cli.Context) (err error) { beforeTrash = func(_ *cli.Context) (err error) {
if f == nil { if f == nil {
f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, ih) f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, sh)
} }
log.Debugf("filter: %s", f.String()) log.Debugf("filter: %s", f.String())
return return
@ -364,11 +364,11 @@ var (
Destination: &do, Destination: &do,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "ignore-hidden", Name: "hidden",
Usage: "operate on unhidden files only", Usage: "operate on hidden files",
Aliases: []string{"i"}, Aliases: []string{"H"},
DisableDefaultText: true, DisableDefaultText: true,
Destination: &ih, Destination: &sh,
}, },
} }