Skip to content

Commit

Permalink
Replacements can be started from the end
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed May 4, 2021
1 parent c48f281 commit cfa294a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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.5.3",
Version: "v1.5.6",
EnableBashCompletion: true,
Flags: []cli.Flag{
&cli.StringFlag{
Expand All @@ -98,7 +98,7 @@ func GetApp() *cli.App {
Aliases: []string{"r"},
Usage: "Replacement `<string>`. If omitted, defaults to an empty string. Supports built-in and regex capture variables",
},
&cli.UintFlag{
&cli.IntFlag{
Name: "replace-limit",
Aliases: []string{"l"},
Usage: "Limit the number of replacements to be made (replaces all matches if set to 0)",
Expand Down
2 changes: 1 addition & 1 deletion src/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func setOptions(op *Operation, c *cli.Context) error {
op.maxDepth = int(c.Uint("max-depth"))
op.quiet = c.Bool("quiet")
op.revert = c.Bool("undo")
op.replaceLimit = int(c.Uint("replace-limit"))
op.replaceLimit = c.Int("replace-limit")

// Sorting
if c.String("sort") != "" {
Expand Down
22 changes: 20 additions & 2 deletions src/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ func (op *Operation) regexReplace(
fileName, replacement string,
) string {
var output string
if op.replaceLimit > 0 {

switch limit := op.replaceLimit; {
case limit > 0:
counter := 0
output = r.ReplaceAllStringFunc(
fileName,
Expand All @@ -402,7 +404,23 @@ func (op *Operation) regexReplace(
return r.ReplaceAllString(val, replacement)
},
)
} else {
case limit < 0:
matches := r.FindAllString(fileName, -1)

l := len(matches) + limit
counter := 0
output = r.ReplaceAllStringFunc(
fileName,
func(val string) string {
if counter >= l {
return r.ReplaceAllString(val, replacement)
}

counter++
return val
},
)
default:
output = r.ReplaceAllString(fileName, replacement)
}

Expand Down
87 changes: 87 additions & 0 deletions src/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,93 @@ func TestFindReplace(t *testing.T) {
testDir := setupFileSystem(t)

cases := []testCase{
{
name: "Replace the last 2 matches",
want: []Change{
{
Source: "No Pressure (2021) S1.E1.1080p.mkv",
BaseDir: testDir,
Target: "No Pressure (2021) S1.E5.5080p.mkv",
},
{
Source: "No Pressure (2021) S1.E2.1080p.mkv",
BaseDir: testDir,
Target: "No Pressure (2021) S5.E2.5080p.mkv",
},
{
Source: "No Pressure (2021) S1.E3.1080p.mkv",
BaseDir: testDir,
Target: "No Pressure (2021) S5.E3.5080p.mkv",
},
},
args: []string{
"-f",
"1",
"-r",
"5",
"-l",
"-2",
testDir,
},
},
{
name: "Replace the last match",
want: []Change{
{
Source: "No Pressure (2021) S1.E1.1080p.mkv",
BaseDir: testDir,
Target: "No Pressure (2021) S1.E1.5080p.mkv",
},
{
Source: "No Pressure (2021) S1.E2.1080p.mkv",
BaseDir: testDir,
Target: "No Pressure (2021) S1.E2.5080p.mkv",
},
{
Source: "No Pressure (2021) S1.E3.1080p.mkv",
BaseDir: testDir,
Target: "No Pressure (2021) S1.E3.5080p.mkv",
},
},
args: []string{
"-f",
"1",
"-r",
"5",
"-l",
"-1",
testDir,
},
},
{
name: "Replace the first 10 matches",
want: []Change{
{
Source: "No Pressure (2021) S1.E1.1080p.mkv",
BaseDir: testDir,
Target: "No Pressure (2025) S5.E5.5080p.mkv",
},
{
Source: "No Pressure (2021) S1.E2.1080p.mkv",
BaseDir: testDir,
Target: "No Pressure (2025) S5.E2.5080p.mkv",
},
{
Source: "No Pressure (2021) S1.E3.1080p.mkv",
BaseDir: testDir,
Target: "No Pressure (2025) S5.E3.5080p.mkv",
},
},
args: []string{
"-f",
"1",
"-r",
"5",
"-l",
"10",
testDir,
},
},
{
want: []Change{
{
Expand Down

0 comments on commit cfa294a

Please sign in to comment.