vim_rename 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +2 -0
- data/bin/vim_rename +86 -0
- data/lib/vim_rename/file_data.rb +32 -0
- data/lib/vim_rename/options.rb +45 -0
- data/lib/vim_rename/version.rb +3 -0
- data/lib/vim_rename.rb +11 -0
- data/vim_rename.gemspec +25 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bb715f071ebecf3fd226a967f436f74e63a54f4b
|
4
|
+
data.tar.gz: 800a3874a88014dd8d8a91913e7954806adbdeee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d0cbb31be26aeafb38d82abf230073cd46ce35fc1061f038295f4cf3d4463995fa307070496f2765ef5a26d42a57a5446e786f03dac3d34e13e9bf7f2e74549
|
7
|
+
data.tar.gz: 07030d9f7f2a58e73003bfa64bef5428aa2515bd0fbaa9700e1074080fdc1bd93df71f4ae37cb59ac62577920621a0a19cbf7472f3f4231264d75910c749a957
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Enrico Carlesso
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# VimRename
|
2
|
+
|
3
|
+
`vim_rename` allows you to use the power of your preferite editor to mass rename files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install like a normal gem with
|
8
|
+
|
9
|
+
$ gem install vim_rename
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```
|
14
|
+
Usage: vim_rename [options]
|
15
|
+
|
16
|
+
-s, --split Split vim view in 2 columns, target file names and old names for reference
|
17
|
+
-c, --confirm Asks for confirmation, showing the changes, before doing any action
|
18
|
+
-C, --confirm-each Asks for confirmation before EACH action
|
19
|
+
-d, --diff Use vimdiff
|
20
|
+
-e, --skip-extensions Skip extension from being displayed
|
21
|
+
-v, --verbose Increase verbosity
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it ( https://github.com/carlesso/vim\_rename/fork )
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/vim_rename
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'vim_rename'
|
4
|
+
require 'optparse'
|
5
|
+
require 'tempfile'
|
6
|
+
require 'colored'
|
7
|
+
|
8
|
+
options = {}
|
9
|
+
options = VimRename::Optparse.parse(ARGV)
|
10
|
+
|
11
|
+
target_file = Tempfile.new('vim_rename_target')
|
12
|
+
original_file = if options.split
|
13
|
+
Tempfile.new('vim_rename_original')
|
14
|
+
else
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
|
18
|
+
files = []
|
19
|
+
options.files.each do |file|
|
20
|
+
if File.exists? file
|
21
|
+
fd = VimRename::FileData.new(file)
|
22
|
+
target_file.write "#{fd.basename}\n"
|
23
|
+
original_file.write "#{fd.basename}\n" if original_file
|
24
|
+
files << fd
|
25
|
+
else
|
26
|
+
# Don't raise, just notice
|
27
|
+
STDERR.puts "cannot access #{file}: No such file or directory".red.bold
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
if files.empty?
|
32
|
+
STDERR.puts "No files left to rename"
|
33
|
+
exit 0
|
34
|
+
end
|
35
|
+
target_file.close
|
36
|
+
original_file.close if original_file
|
37
|
+
|
38
|
+
if options.vim_diff
|
39
|
+
puts "executing vimdiff #{target_file.path} #{original_file.path}" if options.verbose
|
40
|
+
system "vimdiff #{target_file.path} #{original_file.path}"
|
41
|
+
elsif options.split
|
42
|
+
puts "executing vim -O2 #{target_file.path} #{original_file.path}" if options.verbose
|
43
|
+
system "vim -O2 #{target_file.path} #{original_file.path}"
|
44
|
+
else
|
45
|
+
puts "executing vim #{target_file.path}" if options.verbose
|
46
|
+
system "vim #{target_file.path}"
|
47
|
+
end
|
48
|
+
|
49
|
+
new_names = File.readlines(target_file.path).map(&:chomp)
|
50
|
+
if new_names.size != files.size
|
51
|
+
STDERR.puts "Wrong number of lines found in file... Aborting"
|
52
|
+
exit
|
53
|
+
end
|
54
|
+
|
55
|
+
puts "Going to move these files:" if options.confirm
|
56
|
+
|
57
|
+
any_changes = false
|
58
|
+
|
59
|
+
new_names.each_with_index do |new_name, i|
|
60
|
+
files[i].update new_name
|
61
|
+
any_changes ||= files[i].changed?
|
62
|
+
if options.confirm && files[i].changed?
|
63
|
+
puts "#{files[i].name} -> #{files[i].full_name}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
unless any_changes
|
68
|
+
puts "No changes to do... Bye"
|
69
|
+
exit
|
70
|
+
end
|
71
|
+
|
72
|
+
if options.confirm
|
73
|
+
exit unless VimRename.yes_no('Do you want to apply the changes?')
|
74
|
+
end
|
75
|
+
|
76
|
+
files.each do |f|
|
77
|
+
next unless f.changed?
|
78
|
+
if options.confirm_each
|
79
|
+
puts "#{f.name} -> #{f.full_name}"
|
80
|
+
f.rename! if VimRename.yes_no('Confirm?')
|
81
|
+
else
|
82
|
+
puts "#{f.name} -> #{f.full_name}" if options.verbose
|
83
|
+
f.rename!
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
module VimRename
|
3
|
+
class FileData
|
4
|
+
attr_accessor :name, :extension, :basename
|
5
|
+
def initialize(name, skip_extension = true)
|
6
|
+
self.name = name
|
7
|
+
self.basename = if skip_extension
|
8
|
+
m = name.match(/(?<extension>\.([^.]*)|\.tar\.gz|\.tar\.bz2|\.svg\.gz|\.tar\.xz)$/)
|
9
|
+
self.extension = m[:extension] if m
|
10
|
+
name.gsub(/#{extension}$/, '')
|
11
|
+
else
|
12
|
+
name
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def update(new_name)
|
17
|
+
self.basename = new_name
|
18
|
+
end
|
19
|
+
|
20
|
+
def full_name
|
21
|
+
"#{basename}#{extension}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def changed?
|
25
|
+
full_name != name
|
26
|
+
end
|
27
|
+
|
28
|
+
def rename!
|
29
|
+
FileUtils.mv name, basename
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
module VimRename
|
5
|
+
class Optparse
|
6
|
+
def self.parse(args)
|
7
|
+
options = OpenStruct.new
|
8
|
+
options.split = true
|
9
|
+
options.confirm = true
|
10
|
+
options.confirm_each = false
|
11
|
+
options.skip_extension = false
|
12
|
+
options.vim_diff = false
|
13
|
+
options.verbose = false
|
14
|
+
options.files = []
|
15
|
+
|
16
|
+
opt_parser = OptionParser.new do |opts|
|
17
|
+
opts.banner = 'Usage: vim_rename [options]'
|
18
|
+
opts.separator ''
|
19
|
+
|
20
|
+
opts.on('-s', '--split', "Split vim view in 2 columns, target file names and old names for reference") do |split|
|
21
|
+
options.split = true
|
22
|
+
end
|
23
|
+
opts.on('-c', '--confirm', "Asks for confirmation, showing the changes, before doing any action") do |split|
|
24
|
+
options.confirm = true
|
25
|
+
end
|
26
|
+
opts.on('-C', '--confirm-each', "Asks for confirmation before EACH action (will exclude --confirm) ") do |split|
|
27
|
+
options.confirm_each = true
|
28
|
+
end
|
29
|
+
opts.on('-d', '--diff', "Use vimdiff (will exclude --split)") do |split|
|
30
|
+
options.vim_diff = true
|
31
|
+
end
|
32
|
+
opts.on('-e', '--skip-extensions', "Skip extension from being displayed") do |split|
|
33
|
+
options.skip_extension = true
|
34
|
+
end
|
35
|
+
opts.on('-v', '--verbose', "Increase verbosity") do |split|
|
36
|
+
options.verbose = true
|
37
|
+
end
|
38
|
+
end
|
39
|
+
opt_parser.parse!(args)
|
40
|
+
options.files = args
|
41
|
+
options.confirm = false if options.confirm_each
|
42
|
+
options
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/vim_rename.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "vim_rename/version"
|
2
|
+
require "vim_rename/options"
|
3
|
+
require "vim_rename/file_data"
|
4
|
+
|
5
|
+
module VimRename
|
6
|
+
def self.yes_no(message)
|
7
|
+
puts "#{message} [Yn]: "
|
8
|
+
answer = STDIN.gets.chomp.downcase
|
9
|
+
%w{y yes}.include?(answer) || answer.empty?
|
10
|
+
end
|
11
|
+
end
|
data/vim_rename.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vim_rename/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "vim_rename"
|
8
|
+
spec.version = VimRename::VERSION
|
9
|
+
spec.authors = ["Enrico Carlesso"]
|
10
|
+
spec.email = ["enricocarlesso@gmail.com"]
|
11
|
+
spec.summary = %q{Mass rename files with vim.}
|
12
|
+
spec.description = %q{Mass rename files with vim. Ever needed to rename a lot of similar files, and asking youself: "Wow, only if I could use vim block selection..."? Well, now you can!}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "colored"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vim_rename
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Enrico Carlesso
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colored
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: 'Mass rename files with vim. Ever needed to rename a lot of similar files,
|
56
|
+
and asking youself: "Wow, only if I could use vim block selection..."? Well, now
|
57
|
+
you can!'
|
58
|
+
email:
|
59
|
+
- enricocarlesso@gmail.com
|
60
|
+
executables:
|
61
|
+
- vim_rename
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/vim_rename
|
71
|
+
- lib/vim_rename.rb
|
72
|
+
- lib/vim_rename/file_data.rb
|
73
|
+
- lib/vim_rename/options.rb
|
74
|
+
- lib/vim_rename/version.rb
|
75
|
+
- vim_rename.gemspec
|
76
|
+
homepage: ''
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.4.5
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Mass rename files with vim.
|
100
|
+
test_files: []
|
101
|
+
has_rdoc:
|