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
}
if is_in_recursive_dir(dir, path) && f.IgnoreHidden() {
if is_in_recursive_dir(dir, path) && f.ShowHidden() {
return nil
}

View file

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

View file

@ -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() {

16
main.go
View file

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