Skip to content

Commit

Permalink
Implement string literal mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed Mar 9, 2021
1 parent fe66aa2 commit 81b2f32
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ AUTHOR:
Ayooluwa Isaiah <ayo@freshman.tech>
VERSION:
v1.1.0
v1.1.1
FLAGS:
--find string, -f string Search string or regular expression.
Expand All @@ -140,6 +140,7 @@ FLAGS:
--only-dir, -D Rename only directories (implies include-dir) (default: false)
--hidden, -H Include hidden files and directories (default: false)
--fix-conflicts, -F Fix any detected conflicts with auto indexing (default: false)
--string-mode, -s Opt into string literal mode by treating find expressions as non-regex strings (default: false)
--help, -h show help (default: false)
--version, -v print the version (default: false)
Expand All @@ -152,8 +153,8 @@ WEBSITE:
**Notes**:
- F2 does not make any changes to your filesystem by default (performs a dry run).
- To enforce the changes, include the `--exec` or `-x` flag.
- The `-f` or `--find` flag supports regular expressions. If omitted, it matches the entire filename of each file.
- The `-r` or `--replace` flag supports variables
- The `-f` or `--find` flag supports regular expressions and string literals. If omitted, it matches the entire filename of each file.
- The `-r` or `--replace` flag supports [variables](#use-a-variable).

### Basic find and replace

Expand All @@ -170,6 +171,34 @@ $ f2 -f 'Screenshot' -r 'Image'
+--------------------+---------------+--------+
```

By default, find expressions are treated as regex. Use `-s` or `--string-mode` to disable regex. This helps when replacing filenames with characters that have a special meaning in regex such as `- . { } ( [ ] )`.

**Regex enabled**:

```bash
$ f2 -f '\(2021\)' -r '[2022]'
+--------------------------------------+--------------------------------------+--------+
| INPUT | OUTPUT | STATUS |
+--------------------------------------+--------------------------------------+--------+
| No Pressure (2021) S01.E01.2160p.mp4 | No Pressure [2022] S01.E01.2160p.mp4 | ok |
| No Pressure (2021) S01.E02.2160p.mp4 | No Pressure [2022] S01.E02.2160p.mp4 | ok |
| No Pressure (2021) S01.E03.2160p.mp4 | No Pressure [2022] S01.E03.2160p.mp4 | ok |
+--------------------------------------+--------------------------------------+--------+
```

**Regex disabled**:

```bash
$ f2 -f '(2021)' -r '[2022]' -s
+--------------------------------------+--------------------------------------+--------+
| INPUT | OUTPUT | STATUS |
+--------------------------------------+--------------------------------------+--------+
| No Pressure (2021) S01.E01.2160p.mp4 | No Pressure [2022] S01.E01.2160p.mp4 | ok |
| No Pressure (2021) S01.E02.2160p.mp4 | No Pressure [2022] S01.E02.2160p.mp4 | ok |
| No Pressure (2021) S01.E03.2160p.mp4 | No Pressure [2022] S01.E03.2160p.mp4 | ok |
+--------------------------------------+--------------------------------------+--------+
```

### Recursive find and replace

Replace all instances of `js` to `ts` in the current directory and all sub directories (no depth limit).
Expand Down
7 changes: 6 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func GetApp() *cli.App {
},
Usage: "F2 is a command-line tool for batch renaming multiple files and directories quickly and safely",
UsageText: "FLAGS [OPTIONS] [PATHS...]",
Version: "v1.1.1",
Version: "v1.2.0",
EnableBashCompletion: true,
Flags: []cli.Flag{
&cli.StringFlag{
Expand Down Expand Up @@ -141,6 +141,11 @@ func GetApp() *cli.App {
Aliases: []string{"F"},
Usage: "Fix any detected conflicts with auto indexing",
},
&cli.BoolFlag{
Name: "string-mode",
Aliases: []string{"s"},
Usage: "Opt into string literal mode by treating find expressions as non-regex strings",
},
},
Action: func(c *cli.Context) error {
op, err := NewOperation(c)
Expand Down
9 changes: 8 additions & 1 deletion operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type Operation struct {
undoFile string
outputFile string
workingDir string
stringMode bool
}

type mapFile struct {
Expand Down Expand Up @@ -604,7 +605,12 @@ func (op *Operation) Replace() error {
fileName = filenameWithoutExtension(fileName)
}

str := op.searchRegex.ReplaceAllString(fileName, op.replaceString)
var str string
if op.stringMode {
str = strings.ReplaceAll(fileName, op.searchRegex.String(), op.replaceString)
} else {
str = op.searchRegex.ReplaceAllString(fileName, op.replaceString)
}

// handle variables
str, err := op.handleVariables(str, v)
Expand Down Expand Up @@ -689,6 +695,7 @@ func NewOperation(c *cli.Context) (*Operation, error) {
op.directories = c.Args().Slice()
op.undoFile = c.String("undo")
op.onlyDir = c.Bool("only-dir")
op.stringMode = c.Bool("string-mode")

if op.onlyDir {
op.includeDir = true
Expand Down
8 changes: 8 additions & 0 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ func TestFindReplace(t *testing.T) {
},
args: []string{"-f", "pic", "-r", "image", "-D", "-R", testDir},
},
{
want: []Change{
{Source: "No Pressure (2021) S1.E1.1080p.mkv", BaseDir: testDir, Target: "No Pressure (2022) S1.E1.1080p.mkv"},
{Source: "No Pressure (2021) S1.E2.1080p.mkv", BaseDir: testDir, Target: "No Pressure (2022) S1.E2.1080p.mkv"},
{Source: "No Pressure (2021) S1.E3.1080p.mkv", BaseDir: testDir, Target: "No Pressure (2022) S1.E3.1080p.mkv"},
},
args: []string{"-f", "(2021)", "-r", "(2022)", "-s", testDir},
},
}

for i, v := range table {
Expand Down

0 comments on commit 81b2f32

Please sign in to comment.