Skip to content

Commit df3e692

Browse files
author
Laurent Cozic
committed
Option to exclude directories from text buffer
1 parent efcfe38 commit df3e692

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ The executable can be downloaded from https://github.com/laurent22/massren/relea
7979
- Move files to trash in bulk instead of one by one.
8080
- Detect default text editor on Windows.
8181
- Disambiguate filenames when processing two or more folders that contain the same filenames.
82-
- Ability to delete files - see https://github.com/laurent22/massren/issues/11
83-
- Option to always exclude directories.
8482
- Option to remove header.
8583
- Other [various issues](https://github.com/laurent22/massren/issues).
8684

main.go

+26-12
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const (
3535
type CommandLineOptions struct {
3636
DryRun bool `short:"n" long:"dry-run" description:"Don't rename anything but show the operation that would have been performed."`
3737
Verbose bool `short:"v" long:"verbose" description:"Enable verbose output."`
38-
Config bool `short:"c" long:"config" description:"Set or list a configuration values. For more info: massren --config --help"`
38+
Config bool `short:"c" long:"config" description:"Set or list configuration values. For more info: massren --config --help"`
3939
Undo bool `short:"u" long:"undo" description:"Undo a rename operation. Currently delete operations cannot be undone (though files can be recovered from the trash in OSX and Windows). eg. massren --undo [path]"`
4040
Version bool `short:"V" long:"version" description:"Displays version information."`
4141
}
@@ -193,7 +193,7 @@ func editFile(filePath string) error {
193193
return nil
194194
}
195195

196-
func filePathsFromArgs(args []string) ([]string, error) {
196+
func filePathsFromArgs(args []string, includeDirectories bool) ([]string, error) {
197197
var output []string
198198
var err error
199199

@@ -218,6 +218,18 @@ func filePathsFromArgs(args []string) ([]string, error) {
218218
}
219219
}
220220

221+
if !includeDirectories {
222+
var temp []string
223+
for _, path := range output {
224+
f, err := os.Stat(path)
225+
if err == nil && f.IsDir() {
226+
continue
227+
}
228+
temp = append(temp, path)
229+
}
230+
output = temp
231+
}
232+
221233
sort.Strings(output)
222234

223235
return output, nil
@@ -262,12 +274,12 @@ func filePathsFromListFile(filePath string) ([]string, error) {
262274
return filePathsFromString(string(contentB)), nil
263275
}
264276

265-
func printHelp(subMenu string) {
277+
func printHelp(subMenu string) {
266278
var info string
267-
279+
268280
if subMenu == "" {
269281
flagParser_.WriteHelp(os.Stdout)
270-
282+
271283
info = `
272284
Examples:
273285
@@ -301,8 +313,10 @@ Config commands:
301313
302314
Possible key/values:
303315
304-
editor: The editor to use when editing the list of files. Default: auto-detected.
305-
use_trash: Whether files should be moved to the trash/recycle bin after deletion. Possible values: 0 or 1. Default: 1.
316+
editor: The editor to use when editing the list of files. Default: auto-detected.
317+
use_trash: Whether files should be moved to the trash/recycle bin after deletion. Possible values: 0 or 1. Default: 1.
318+
include_directories: Whether to include the directories the file buffer. Possible values: 0 or 1. Default: 1.
319+
include_header: Whether to show the header in the file buffer. Possible values: 0 or 1. Default: 1.
306320
307321
Examples:
308322
@@ -313,7 +327,7 @@ Examples:
313327
% APPNAME --config use_trash 0
314328
`
315329
}
316-
330+
317331
fmt.Println(strings.Replace(info, "APPNAME", APPNAME, -1))
318332
}
319333

@@ -444,7 +458,7 @@ func processFileActions(fileActions []*FileAction, dryRun bool) error {
444458
logError("Could not save history items: %s", err)
445459
}
446460
}()
447-
461+
448462
var deleteWaitGroup sync.WaitGroup
449463
var deleteChannel = make(chan int, 100)
450464
useTrash := config_.Bool("use_trash")
@@ -495,7 +509,7 @@ func processFileActions(fileActions []*FileAction, dryRun bool) error {
495509
if err != nil {
496510
logError("%s", err)
497511
}
498-
<- deleteChannel
512+
<-deleteChannel
499513
}(filePath, deleteChannel, useTrash)
500514
}
501515
break
@@ -509,7 +523,7 @@ func processFileActions(fileActions []*FileAction, dryRun bool) error {
509523

510524
doneActions = append(doneActions, action)
511525
}
512-
526+
513527
deleteWaitGroup.Wait()
514528

515529
// Conflict resolution:
@@ -630,7 +644,7 @@ func main() {
630644
return
631645
}
632646

633-
filePaths, err := filePathsFromArgs(args)
647+
filePaths, err := filePathsFromArgs(args, config_.Bool("include_directories"))
634648

635649
if err != nil {
636650
criticalError(err)

main_test.go

+33-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ func Test_filePathsFromArgs(t *testing.T) {
545545
filepath.Join(tempFolder(), "*"),
546546
}
547547

548-
filePaths, err := filePathsFromArgs(args)
548+
filePaths, err := filePathsFromArgs(args, true)
549549
if err != nil {
550550
t.Fatal(err)
551551
}
@@ -569,7 +569,7 @@ func Test_filePathsFromArgs(t *testing.T) {
569569
}
570570

571571
args = []string{}
572-
filePaths, err = filePathsFromArgs(args)
572+
filePaths, err = filePathsFromArgs(args, true)
573573
if err != nil {
574574
t.Fatal(err)
575575
}
@@ -582,6 +582,37 @@ func Test_filePathsFromArgs(t *testing.T) {
582582
os.Chdir(currentDir)
583583
}
584584

585+
func Test_filePathsFromArgs_noDirectories(t *testing.T) {
586+
setup(t)
587+
defer teardown(t)
588+
589+
f0 := filepath.Join(tempFolder(), "0")
590+
f1 := filepath.Join(tempFolder(), "1")
591+
d0 := filepath.Join(tempFolder(), "dir0")
592+
d1 := filepath.Join(tempFolder(), "dir1")
593+
594+
touch(f0)
595+
touch(f1)
596+
os.Mkdir(d0, 0700)
597+
os.Mkdir(d1, 0700)
598+
599+
args := []string{
600+
filepath.Join(tempFolder(), "*"),
601+
}
602+
603+
filePaths, _ := filePathsFromArgs(args, true)
604+
err := fileListsAreEqual(filePaths, []string{f0, f1, d0, d1})
605+
if err != nil {
606+
t.Error(err)
607+
}
608+
609+
filePaths, _ = filePathsFromArgs(args, false)
610+
err = fileListsAreEqual(filePaths, []string{f0, f1})
611+
if err != nil {
612+
t.Error(err)
613+
}
614+
}
615+
585616
func stringListsEqual(s1 []string, s2 []string) bool {
586617
for i, s := range s1 {
587618
if s != s2[i] {

undo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func handleUndoCommand(opts *CommandLineOptions, args []string) error {
9-
filePaths, err := filePathsFromArgs(args)
9+
filePaths, err := filePathsFromArgs(args, true)
1010
if err != nil {
1111
return err
1212
}

0 commit comments

Comments
 (0)