Class: Lhj::Command::RenameImage
Class Method Summary
collapse
Instance Method Summary
collapse
#auto_spin, #begin_title, #run, #stop
Constructor Details
Returns a new instance of RenameImage.
25
26
27
28
29
30
31
|
# File 'lib/lhj/command/rename_image.rb', line 25
def initialize(argv)
@current_path = argv.shift_argument || Dir.pwd
@image_pre = argv.option('pre')
@image_name = argv.option('name')
@image_other_name = argv.option('other')
super
end
|
Class Method Details
.options ⇒ Object
12
13
14
15
16
17
18
|
# File 'lib/lhj/command/rename_image.rb', line 12
def self.options
[
%w[--pre 图片前缀],
%w[--name 图片名称],
%w[--other 图片变化后的名称]
]
end
|
Instance Method Details
#handle ⇒ Object
33
34
35
|
# File 'lib/lhj/command/rename_image.rb', line 33
def handle
rename_image
end
|
#list_all_image ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/lhj/command/rename_image.rb', line 46
def list_all_image
sort_list = Dir.glob("#{@current_path}/**/*.{png}").sort { |b, a| File.size(a) <=> File.size(b) }
sort_list.each do |f|
s = File.size(f)
s_s = "#{s}B"
if s > 1024 * 1024
bf = s / (1024.0 * 1024.0)
s_s = "#{format("%.1f",bf)}MB"
elsif s > 1024
bf = s / 1024.0
s_s = "#{format("%.1f",bf)}KB"
else
s_s = "#{s}KB"
end
puts "#{f} #{s_s}"
end
end
|
#modify_name(file_name, extname) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/lhj/command/rename_image.rb', line 64
def modify_name(file_name, extname)
name = file_name.downcase + extname
if @image_pre
name = @image_pre + file_name.downcase + extname
elsif @image_name && @image_other_name
if file_name =~ /#{@image_name}/
other_name = file_name.gsub(/#{@image_name}/, @image_other_name)
name = other_name + extname
end
end
name
end
|
#rename_image ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/lhj/command/rename_image.rb', line 37
def rename_image
Dir.glob("#{@current_path}/**/*.{png}").sort.each do |f|
filename = File.basename(f, File.extname(f))
m_filename = modify_name(filename, File.extname(f))
target = File.join(@current_path, m_filename)
File.rename(f, target)
end
end
|
#validate! ⇒ Object
20
21
22
23
|
# File 'lib/lhj/command/rename_image.rb', line 20
def validate!
super
help! '输入图片信息' unless @image_pre || @image_name || @image_other_name
end
|