From b26ac28b7d2a3228dbc8969a4c54d0e993a1b4e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Sun, 30 Jun 2024 15:59:14 -0700 Subject: [PATCH] reverse hidden flag; ignore hidden files by default --- internal/files/files.go | 2 +- internal/filter/filter.go | 22 +++++++++++----------- internal/filter/filter_test.go | 24 ++++++++++++++---------- main.go | 16 ++++++++-------- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/internal/files/files.go b/internal/files/files.go index d323117..bef66e7 100644 --- a/internal/files/files.go +++ b/internal/files/files.go @@ -105,7 +105,7 @@ func walk_dir(dir string, f *filter.Filter) (files Files) { return nil } - if is_in_recursive_dir(dir, path) && f.IgnoreHidden() { + if is_in_recursive_dir(dir, path) && f.ShowHidden() { return nil } diff --git a/internal/filter/filter.go b/internal/filter/filter.go index 1924fcc..939cc0a 100644 --- a/internal/filter/filter.go +++ b/internal/filter/filter.go @@ -18,7 +18,7 @@ type Filter struct { unglob, unpattern string filenames []string dirsonly, filesonly bool - ignorehidden bool + showhidden bool matcher *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) FilesOnly() bool { return f.filesonly } 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) { filename = filepath.Clean(filename) @@ -85,7 +85,7 @@ func (f *Filter) Match(filename string, modified time.Time, isdir bool) bool { if f.dirsonly && !isdir { return false } - if f.ignorehidden && strings.HasPrefix(filename, ".") { + if !f.showhidden && strings.HasPrefix(filename, ".") { return false } // okay it was good @@ -116,7 +116,7 @@ func (f *Filter) Blank() bool { f.before.Equal(t) && f.on.Equal(t) && len(f.filenames) == 0 && - !f.ignorehidden && + !f.showhidden && !f.filesonly && !f.dirsonly } @@ -136,7 +136,7 @@ func (f *Filter) String() string { f.unglob, unm, f.filenames, f.filesonly, f.dirsonly, - f.ignorehidden, + f.showhidden, ) } @@ -154,18 +154,18 @@ func (f *Filter) has_unregex() bool { 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 ( err error now = time.Now() ) f := &Filter{ - glob: glob, - unglob: unglob, - filesonly: filesonly, - dirsonly: dirsonly, - ignorehidden: ignorehidden, + glob: glob, + unglob: unglob, + filesonly: filesonly, + dirsonly: dirsonly, + showhidden: showhidden, } f.AddFileNames(names...) diff --git a/internal/filter/filter_test.go b/internal/filter/filter_test.go index f6f5a3f..47c44cb 100644 --- a/internal/filter/filter_test.go +++ b/internal/filter/filter_test.go @@ -26,16 +26,16 @@ type testholder struct { before, after, on string filenames []string filesonly, dirsonly bool - ignorehidden bool + showhidden bool good, bad []singletest } func (t testholder) String() string { return fmt.Sprintf( "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.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 { f, err = New( 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..., ) if err != nil { @@ -375,12 +375,16 @@ func TestFilterDirsOnly(t *testing.T) { }) } -func TestFilterIgnoreHidden(t *testing.T) { +func TestFilterShowHidden(t *testing.T) { testmatch(t, []testholder{ { - ignorehidden: true, - good: append(blanktimedir("test", "alsotest", "helloworld"), blanktimefile("test", "alsotest", "helloworld")...), - bad: append(blanktimedir(".test", ".alsotest", ".helloworld"), blanktimefile(".test", ".alsotest", ".helloworld")...), + showhidden: false, + good: 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, }, { - ignorehidden: true, + showhidden: true, }, } ) @@ -543,7 +547,7 @@ func TestFilterNotBlank(t *testing.T) { t.Run("notblank"+tester.String(), func(t *testing.T) { f, _ = New( 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..., ) if f.Blank() { diff --git a/main.go b/main.go index fff91b6..d81a158 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,7 @@ var ( f *filter.Filter o, b, a, g, p string ung, unp string - fo, do, ih bool + fo, do, sh bool askconfirm bool workdir, ogdir cli.Path recursive bool @@ -91,7 +91,7 @@ var ( ) 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 { return err @@ -115,7 +115,7 @@ var ( beforeCommands = func(ctx *cli.Context) (err error) { // setup filter 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()) return @@ -123,7 +123,7 @@ var ( beforeTrash = func(_ *cli.Context) (err error) { 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()) return @@ -364,11 +364,11 @@ var ( Destination: &do, }, &cli.BoolFlag{ - Name: "ignore-hidden", - Usage: "operate on unhidden files only", - Aliases: []string{"i"}, + Name: "hidden", + Usage: "operate on hidden files", + Aliases: []string{"H"}, DisableDefaultText: true, - Destination: &ih, + Destination: &sh, }, }