-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cdb61f4
Showing
12 changed files
with
451 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in mvmv.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2012 Junegunn Choi | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Mvmv | ||
|
||
"Move! Move!" | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'mvmv' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install mvmv | ||
|
||
## Usage | ||
|
||
``` | ||
usage: mvmv <command> [<args>] <files> | ||
mvmv prefix <prefix> <files> | ||
mvmv suffix <suffix> <files> | ||
mvmv name <name> <files> | ||
mvmv name-suffix <suffix> <files> | ||
mvmv ext <.extension> <files> | ||
mvmv regex <from> <to> <files> | ||
mvmv upper <files> | ||
mvmv lower <files> | ||
-f, --force Force rename | ||
--no-color Disable ANSI color codes | ||
--help Show this message | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env rake | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env ruby | ||
require 'rubygems' | ||
require 'mvmv' | ||
require 'optparse' | ||
|
||
options = {} | ||
opts = OptionParser.new do |opts| | ||
opts.banner = | ||
"usage: mvmv <command> [<args>] <files>" | ||
|
||
opts.separator '' | ||
opts.separator ' mvmv prefix <prefix> <files>' | ||
opts.separator ' mvmv suffix <suffix> <files>' | ||
opts.separator ' mvmv name <name> <files>' | ||
opts.separator ' mvmv name-suffix <suffix> <files>' | ||
opts.separator ' mvmv ext <.extension> <files>' | ||
opts.separator ' mvmv regex <from> <to> <files>' | ||
opts.separator ' mvmv upper <files>' | ||
opts.separator ' mvmv lower <files>' | ||
opts.separator '' | ||
|
||
opts.on('-f', '--force', 'Force rename') do |v| | ||
options[:force] = v | ||
end | ||
|
||
opts.on('--no-color', 'Disable ANSI color codes') do |v| | ||
options[:color] = false | ||
end | ||
|
||
opts.on_tail('--help', "Show this message") do | ||
puts opts | ||
exit | ||
end | ||
end | ||
|
||
opts.parse! ARGV | ||
|
||
if ARGV.length < 2 | ||
puts opts | ||
exit 1 | ||
end | ||
|
||
command = ARGV[0].to_s.gsub('-', '_').to_sym | ||
args = ARGV[1..-1] | ||
mvmv = Mvmv.new options.fetch(:color, true) | ||
begin | ||
if options[:force] | ||
mvmv.rename! command, *args | ||
else | ||
mvmv.rename command, *args | ||
end | ||
rescue ArgumentError => e | ||
puts opts | ||
exit 1 | ||
rescue Exception => e | ||
exit 1 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
require 'mvmv/version' | ||
require 'mvmv/command' | ||
require 'ansi' | ||
|
||
class Mvmv | ||
def initialize color = true | ||
@color = color | ||
end | ||
|
||
def convert_filenames symb, *args | ||
unless Mvmv::Command.respond_to?(symb) | ||
error "Invalid command: #{symb}" | ||
end | ||
|
||
arg_arity = Mvmv::Command.method(symb).arity - 1 | ||
if arg_arity >= args.length | ||
error "Invalid number of parameters for #{symb}", ArgumentError | ||
end | ||
|
||
files = args[arg_arity..-1] | ||
files.zip Mvmv::Command.send(symb, *(args[0, arg_arity] + [files])) | ||
end | ||
|
||
def rename symb, *args | ||
rename_impl false, symb, *args | ||
end | ||
|
||
def rename! symb, *args | ||
rename_impl true, symb, *args | ||
end | ||
|
||
private | ||
def rename_impl force, symb, *args | ||
pairs = convert_filenames symb, *args | ||
|
||
flen = pairs.map(&:first).map(&:length).max | ||
tlen = pairs.map(&:last).map(&:length).max | ||
|
||
pairs.each do |pair| | ||
from, to = pair | ||
next if from == to | ||
skip = false | ||
while true | ||
log [ | ||
ansi('[', :bold), | ||
from.ljust(flen), | ||
ansi('=>', :bold), | ||
ansi(to.ljust(tlen), :blue, :bold), | ||
ansi(']', :bold), | ||
force ? nil : ansi('(', :yellow) + | ||
ansi('y', :yellow, :bold) + | ||
ansi('/', :yellow) + | ||
'n' + | ||
ansi(')? ', :yellow) | ||
].compact.join(' ') | ||
|
||
unless force | ||
case $stdin.gets | ||
when nil | ||
puts | ||
next | ||
when /^n/i | ||
skip = true | ||
break | ||
when /^[^y\n]/i | ||
next | ||
end | ||
end | ||
|
||
unless File.exists?(from) | ||
error " File not found: #{from}" | ||
break | ||
end | ||
|
||
if !force && File.exists?(to) | ||
skip = true | ||
while true | ||
log ansi(" #{to} already exists. Overwrite " + | ||
ansi('(', :yellow) + | ||
'y' + | ||
ansi('/', :yellow) + | ||
ansi('n', :yellow, :bold) + | ||
ansi(')? ', :yellow) | ||
) | ||
|
||
unless | ||
case $stdin.gets | ||
when nil | ||
puts | ||
next | ||
when /^[n\n]/i | ||
break | ||
when /^y/i | ||
skip = false | ||
break | ||
else | ||
next | ||
end | ||
end | ||
end | ||
end | ||
|
||
# DOIT | ||
# Overwrite? | ||
unless skip | ||
begin | ||
File.rename from, to | ||
logln " Renamed", :green | ||
rescue Exception => e | ||
error " Failed to rename #{from}" | ||
end | ||
end | ||
break | ||
end | ||
end | ||
end | ||
|
||
def ansi msg, *colors | ||
(@color && !colors.empty?) ? ANSI::Code.ansi(*colors) { msg } : msg | ||
end | ||
|
||
def log msg, *colors | ||
$stdout.print ansi(msg, *colors) | ||
end | ||
|
||
def logln msg, *colors | ||
$stdout.puts ansi(msg, *colors) | ||
end | ||
|
||
def error message, x = Exception | ||
logln message, :red, :bold | ||
raise x.new(message) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class Mvmv | ||
module Command | ||
class Sequencer | ||
def initialize n = 1 | ||
@n = n - 1 | ||
end | ||
|
||
def convert str | ||
@n += 1 | ||
str.gsub(/#+/) { |x| @n.to_s.rjust(x.length, '0') } | ||
end | ||
end | ||
|
||
class << self | ||
def name n, files | ||
seq = Sequencer.new | ||
files.map { |file| | ||
seq.convert(n) + File.extname(file) | ||
} | ||
end | ||
|
||
def prefix p, files | ||
seq = Sequencer.new | ||
files.map { |file| seq.convert(p) + file } | ||
end | ||
|
||
def suffix s, files | ||
seq = Sequencer.new | ||
files.map { |file| file + seq.convert(s) } | ||
end | ||
|
||
def name_suffix s, files | ||
seq = Sequencer.new | ||
files.map { |file| | ||
ext = File.extname(file) | ||
file.chomp(ext) + seq.convert(s) + ext | ||
} | ||
end | ||
|
||
def ext x, files | ||
files.map { |file| | ||
ext = File.extname(file) | ||
file.chomp(ext) + x | ||
} | ||
end | ||
|
||
def upper files | ||
files.map(&:upcase) | ||
end | ||
|
||
def lower files | ||
files.map(&:downcase) | ||
end | ||
|
||
def regex f, t, files | ||
seq = Sequencer.new | ||
files.map { |file| | ||
file.gsub(Regexp.compile f) { |match| | ||
n = 0 | ||
captures = [] | ||
while v = eval("$#{n += 1}") | ||
captures << v | ||
end | ||
seq.convert t.gsub(/\$([1-9][0-9]*)/) { |c| captures[$1.to_i - 1] } | ||
} | ||
} | ||
end | ||
end | ||
end#Command | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Mvmv | ||
VERSION = "0.0.1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- encoding: utf-8 -*- | ||
require File.expand_path('../lib/mvmv/version', __FILE__) | ||
|
||
Gem::Specification.new do |gem| | ||
gem.authors = ["Junegunn Choi"] | ||
gem.email = ["junegunn.c@gmail.com"] | ||
gem.description = %q{Rename a series of files easily} | ||
gem.summary = %q{Rename a series of files easily} | ||
gem.homepage = "https://github.com/junegunn/mvmv" | ||
|
||
gem.files = `git ls-files`.split($\) | ||
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } | ||
gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) | ||
gem.name = "mvmv" | ||
gem.require_paths = ["lib"] | ||
gem.version = Mvmv::VERSION | ||
gem.add_runtime_dependency 'ansi', '~> 1.4.2' | ||
gem.add_development_dependency 'test-unit' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env ruby | ||
# encoding: utf-8 | ||
|
||
# DISCLAIMER: | ||
# Not a real test! | ||
# Just a helper script for running scripts with local source | ||
|
||
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib') | ||
load File.join(File.dirname(__FILE__), '../bin/', File.basename(ARGV.shift)) |
Oops, something went wrong.