Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib-candidate] Clean up file bulk-rename a little #798

Merged
merged 1 commit into from
Mar 30, 2024

Conversation

texastoland
Copy link
Contributor

@texastoland texastoland commented Mar 20, 2024

Generated diff for command because move broke it.

  • No file prefix similar to other filesystem commands
  • Input paths instead of directory param for filters or globbing
  • Record closure param with original path ($in is still stem)
  • --verbose table output
  • --no-exectute for dry run with --verbose
  • Shorthand flags
  • Parallel renaming for large directories
  • More tests

@amtoine Request feedback ๐Ÿ™๐Ÿผ Happy to revise ot revert anything!

โฏ bulk-rename -h
Rename bulk input files in parallel using a closure.

The reason behind this command is quite simple:
- Sometimes one receives a bunch of files with integer ids: 1, 2, 3, ...
- These ids come rarely with padding... i.e. 1 instead of 001 when there are 3-digit ids
- This means that file with id 9 will be sorted way after file with id 1000

This command allows to do such a task!

Examples:
  Rename `.mise.toml` files to `.mise.local.toml` recursively
  > glob **/.mise.toml | bulk-rename { str append .local }

  Rename files in `/foo` with a name that has an id to have 3 digits with 0-padding
  > ls /foo | bulk-rename { |path|
     if $path.input.type == file {
       $path.stem | parse "some_format_{id}"
         | get 0
         | update id { fill --alignment r --character 0 --width 3 }
         | $"some_format_($in.id)"
     }
     # else skip dirs
   }

Usage:
  > main {flags} <update_stem>

Flags:
  -v, --verbose - Show which files were renamed, if any
  -n, --no-execute - Do not make any changes; add --verbose to see what would be made
whitespace bug: nushell/nushell#12264
  -h, --help - Display the help message for this command

Parameters:
  update_stem <closure()>: The code to rename the file stem: receives the old stem as input and a record param with both `stem` and `input` keys

Input/output types:
  โ•ญโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
  โ”‚ # โ”‚   input   โ”‚             output              โ”‚
  โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  โ”‚ 0 โ”‚ list<any> โ”‚ nothing                         โ”‚
  โ”‚ 1 โ”‚ list<any> โ”‚ table<old: string, new: string> โ”‚
  โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

@texastoland texastoland force-pushed the std-batch-rename branch 6 times, most recently from 5491221 to 743311b Compare March 20, 2024 22:21
@texastoland
Copy link
Contributor Author

texastoland commented Mar 22, 2024

Generated diff for command because move broke it.

Don't miss my linked diff in the description ยฑ

  • Input paths instead of directory param for filters or globbing

Helpful for filtering files or recursing in a directory for example:

# Examples:
#   Rename `.mise.toml` files to `.mise.local.toml` recursively
glob **/.mise.toml | batch-rename { str append .local }
  • Record closure param with original path ($in is still stem)

Possible to filter by $param.input instead of globing:

#   Rename files in `/foo` with a name that has an id to have 3 digits with 0-padding
ls /foo | bulk-rename { |path|
  if $path.input.type == file {
    # rename ...
  }
  # else skip dirs
}

Also makes it possible to append metadata like size to a file name.

--verbose table output

old and new columns for checking changes.

  • More tests

The previous test was more of an example. I refactored it to probe edge cases in file names including dotfiles, different extensions, and extension-less as well as new flags.

@texastoland texastoland force-pushed the std-batch-rename branch 21 times, most recently from d974b79 to 1e7810b Compare March 27, 2024 21:14
- No `file` prefix similar to other filesystem commands
- Optional input especially for globbing
- Record closure param with full path for filtering `--directory` (see examples)
- `--verbose` table output
- `--no-exectute` for dry-run with `--verbose`
- Shorthand flags
- Parallel renaming for large directories
- More tests
@fdncred fdncred merged commit 268201e into nushell:main Mar 30, 2024
1 check passed
@fdncred
Copy link
Collaborator

fdncred commented Mar 30, 2024

thanks for cleaning this up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants