yac 1.0.4 → 1.1.0

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.
Files changed (5) hide show
  1. data/lib/format.rb +8 -6
  2. data/lib/git.rb +41 -0
  3. data/lib/yac.rb +33 -56
  4. data/yac.gemspec +2 -2
  5. metadata +2 -1
data/lib/format.rb CHANGED
@@ -1,3 +1,9 @@
1
+ class Symbol
2
+ def to_proc
3
+ Proc.new { |*args| args.shift.__send__(self, *args) }
4
+ end
5
+ end
6
+
1
7
  module Format
2
8
  Pdf_Error = "Please Modify ~/.yacrc To Provide A Valid Command To Operate PDF Document"
3
9
  Image_Error = "Please Modify ~/.yacrc To Provide A Valid Command To Operate Image Document"
@@ -47,19 +53,15 @@ module Format
47
53
  puts Doc_Error unless system("#{Yac::CONFIG["editor"] || ENV['EDITOR'] ||'vim'} \"#{file}\" 2>/dev/null")
48
54
  end
49
55
 
50
- def clean_filename(f)
51
- return f.sub(/^(.*)?(main|private)\/(.*)/,'\3').sub(/^@/,'')
52
- end
53
-
54
56
  def colorful(stuff,level="text",line_break = true)
55
57
  stuff = empha(stuff,level)
56
- print "\033[%sm%s\033[0m" % [Yac::CONFIG[level],stuff.rstrip]
58
+ print "\e[%sm%s\e[0m " % [Yac::CONFIG[level],stuff.rstrip]
57
59
  print "\n" if line_break
58
60
  end
59
61
 
60
62
  def empha(stuff,level="text",empha_regexp=/(@@@(.*)@@@)/)
61
63
  stuff.to_s.scan(empha_regexp) do |x|
62
- return stuff.gsub(x[0],"\033[0m\033[#{Yac::CONFIG["empha"].to_s}m%s\033[0m\033[%sm" % [x[1],Yac::CONFIG[level]])
64
+ return stuff.gsub(x[0],"\e[0m\e[#{Yac::CONFIG["empha"].to_s}m%s\e[0m\e[%sm" % [x[1],Yac::CONFIG[level]])
63
65
  end
64
66
  end
65
67
 
data/lib/git.rb ADDED
@@ -0,0 +1,41 @@
1
+ class Git
2
+ def initialize(path)
3
+ @working_path = path
4
+ end
5
+
6
+ def self.clone(from,path,name)
7
+ `cd #{path} && git clone #{from} #{name}`
8
+ end
9
+
10
+ def self.init(path)
11
+ `cd #{path} && git init`
12
+ end
13
+
14
+ def mv(orig,new,with_commit = true)
15
+ `cd '#@working_path' && git mv #{orig} #{new}`
16
+ self.commit("#{clean_name(orig)} Renamed to #{clean_name(new)}") if with_commit
17
+ end
18
+
19
+ def add(file,with_commit = true)
20
+ `cd '#@working_path' && git add file`
21
+ self.commit("#{clean_name(file)} Added") if with_commit
22
+ end
23
+
24
+ def edit(file,with_commit = true)
25
+ `cd '#@working_path' && git add file`
26
+ self.commit("#{clean_name(file)} Updated") if with_commit
27
+ end
28
+
29
+ def rm(file,with_commit=true)
30
+ `cd '#@working_path' && git rm -f file`
31
+ self.commit("#{clean_name(file)} Removed") if with_commit
32
+ end
33
+
34
+ def commit(msg,*args)
35
+ `cd '#@working_path' && git commit #{args.to_s} -m '#{msg}'`
36
+ end
37
+
38
+ def clean_name(f)
39
+ return f.sub(/^(.*)?\/(main|private)\/(.*)/,'\3').sub(/^@/,'')
40
+ end
41
+ end
data/lib/yac.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
- %w(rubygems git fileutils yaml format).each {|f| require f}
3
-
2
+ %w(git fileutils yaml format).each {|f| require f}
4
3
  module Yac
5
4
  include Format
6
5
  extend self
@@ -14,8 +13,8 @@ module Yac
14
13
  CONFIG["root"] ||= File.join(ENV['HOME'],".yac")
15
14
 
16
15
  @main_path, @pri_path = File.join(CONFIG["root"],"/main/"), File.join(CONFIG["root"],"/private/")
17
- @main_git = Git.open(@main_path) if File.exist?(@main_path)
18
- @pri_git = Git.open(@pri_path)if File.exist?(@pri_path)
16
+ @main_git = Git.new(@main_path)
17
+ @pri_git = Git.new(@pri_path)
19
18
 
20
19
  def new(args)
21
20
  init unless File.exist?(@main_path) && File.exist?(@pri_path)
@@ -47,13 +46,12 @@ module Yac
47
46
  else
48
47
  if CONFIG["#{name}"] && CONFIG["#{name}"]['clone-from']
49
48
  colorful("Initialize #{name} repository from #{CONFIG[name]['clone-from']} to #{CONFIG['root']}/#{name}","notice")
50
- Git.clone(CONFIG["#{name}"]['clone-from'], name, :path => CONFIG['root'])
49
+ Git.clone(CONFIG["#{name}"]['clone-from'],CONFIG['root'],name)
51
50
  else
52
51
  colorful("Initialize #{name} repository from scratch to #{CONFIG['root']}/#{name}","notice")
52
+ prepare_dir(path)
53
53
  Git.init(path)
54
54
  end
55
- @main_git = Git.open(@main_path) if File.exist?(@main_path)
56
- @pri_git = Git.open(@pri_path)if File.exist?(@pri_path)
57
55
  end
58
56
  end
59
57
  end
@@ -128,35 +126,34 @@ module Yac
128
126
  #You can use $ yac mv linux.ch linux/ to rename linux.ch to linux/linux.ch
129
127
  new_filename = args[1].sub(/\/$/,file.sub(/.*\/(.*)(\..*)/,'/\1')).sub(/^(@)?/,file =~ /^#{@main_path}/ ? "@":"")
130
128
  new_name = add_file(new_filename ,file.sub(/.*(\..*)/,'\1'))
131
- if confirm("You Are Renaming #{file} To #{new_name}")
129
+ if new_name && confirm("You Are Renaming #{file} To #{new_name}")
132
130
  prepare_dir(new_name)
133
- `mv "#{file}" "#{new_name}"`
134
- @working_git.add
135
- @working_git.commit_all("#{clean_filename(file)} Renamed to #{clean_filename(new_name)}")
131
+ @working_git.mv(file,new_name)
136
132
  end
137
133
  end
138
134
 
139
135
  protected
140
136
  def add_single(args)
141
137
  file = add_file(args)
142
- if confirm("You Are Adding #{file}")
138
+ if file && confirm("You Are Adding #{file}")
143
139
  edit_text(file)
144
- @working_git.add
145
- @working_git.commit_all("#{clean_filename(file)} Added")
140
+ @working_git.add(file)
146
141
  end
147
142
  end
148
143
 
149
144
  def add_file(args,suffix = ".ch")
150
- if args.include?('/') && args =~ /(@?)(?:(.*)\/)(.+)/
151
- path = $1.empty? ? @pri_path : @main_path
145
+ if args.include?('/') && args =~ /(@?)(?:(.*)\/)(.+)/ #choose directory
146
+ path = $1.empty? ? @pri_path : @main_path #choose git path
152
147
  all_path = %x{
153
- find #{path} -type d -iwholename '#{path}*#{$2}*' -not -iwholename '*.git*'| sed 's/^.*\\(private\\|main\\)\\//#{$1}/'
154
- }.to_a
155
- colorful("Which directory do you want to use:","notice") if all_path.size >1
156
- choosed_path = choose_one(all_path.concat([$1+$2]).uniq)
157
- args = choosed_path + "/" + $3 if choosed_path
148
+ find #{path} -type d -iwholename '#{path}*#{$2}*' -not -iwholename '*.git*'| sed 's/^.*\\/\\(private\\|main\\)\\//#{$1}/'
149
+ }.to_a.map(&:strip).concat([$1+$2]).uniq
150
+
151
+ colorful("Which directory do you want to use:","notice")
152
+ choosed_path = choose_one(all_path)
153
+ return full_path(choosed_path + "/" + $3 + suffix) if choosed_path
154
+ else
155
+ return full_path(args+suffix)
158
156
  end
159
- file = full_path(args+suffix)
160
157
  end
161
158
 
162
159
  def show_single(args)
@@ -169,53 +166,43 @@ module Yac
169
166
  def rm_single(args)
170
167
  file = search_name(args,"Remove")
171
168
  if confirm("You Are Removing #{file}.")
172
- begin
173
- @working_git.remove(file)
174
- @working_git.commit_all("#{clean_filename(file)} Removed")
175
- rescue Git::GitExecuteError
176
- FileUtils.rm_rf(file)
177
- end
169
+ @working_git.rm(file)
178
170
  end
179
171
  end
180
172
 
181
173
  def edit_single(args)
182
174
  file = search_name(args,"Edit")
183
175
  edit_file(file)
184
- @working_git.add
185
- @working_git.commit_all("#{clean_filename(file)} Updated")
176
+ @working_git.edit(file)
186
177
  end
187
178
 
188
179
  def search_name(args,msg = nil)
189
180
  path = (args =~ /^(@)/) ? [@main_path] : [@main_path , @pri_path]
190
181
  result = []
191
182
  path.each do |x|
192
- result.concat(`find "#{x}" -type f -iwholename '#{x}*#{args.sub(/^@/,'').strip}*' -not -iwholename '*.git*'| sed 's/^.*\\(private\\|main\\)\\//#{x=~/main/ ? '@':'' }/'`.to_a)
183
+ result.concat(`find "#{x}" -type f -iwholename '#{x}*#{args.sub(/^@/,'').strip}*' -not -iwholename '*.git*'| sed 's/^.*\\/\\(private\\|main\\)\\//#{x=~/main/ ? '@':'' }/'`.to_a)
193
184
  end
194
- #For loop show filename
195
- (format_file(full_path(result[0]));return nil) if msg =~ /show/i && result.size == 1
196
185
 
197
186
  return result.empty? ? (colorful("Nothing Found About < #{args} >","warn")) :
198
187
  (colorful("The Results About < #{args} > To #{msg || "Operate"} :","notice");full_path(choose_one(result)))
199
188
  end
200
189
 
201
190
  def search_content(args)
202
- args.sub!(/^"(.*)"/,'\1')
191
+ args.sub!(/^"(.*)"/,'\1') #Remove the " for input Regex
203
192
  result = `cd "#{@pri_path}" && grep -n -i -P '#{args}' -R *.ch 2>/dev/null`.to_a
204
193
  result.concat(`cd "#{@main_path}" && grep -n -i -P '#{args}' -R *.ch 2>/dev/null | sed 's/^/@/g'`.to_a)
205
194
  all_result = []
206
195
  result.each do |x|
207
196
  stuff = x.split(':',3)
208
197
  colorful(stuff[0],"filename",false)
209
- print " "
210
198
  colorful(stuff[1],"line_number",false)
211
- print " "
212
199
  format_section(empha(stuff[2],nil,/((#{args}))/i),true)
213
200
  all_result.concat(stuff[0].to_a)
214
201
  end
215
202
  all_result.uniq!
216
203
  loop do
217
- all_result.size > 1 ? (file = full_path(choose_one(all_result))) : (format_file(full_path(all_result[0]));break)
218
204
  colorful("All files Contain #{args.strip},Choose one to show","notice")
205
+ file = full_path(choose_one(all_result))
219
206
  file ? format_file(file) : break
220
207
  end
221
208
  end
@@ -234,39 +221,29 @@ module Yac
234
221
 
235
222
  def confirm(*msg)
236
223
  colorful("#{msg.to_s}\nAre You Sure (Y/N) (q to quit):","notice",false)
237
- case STDIN.gets
238
- when /n|q/i
239
- return false
240
- when /y/i
241
- return true
242
- else
243
- colorful("Please Input A Valid String,","warn")
244
- confirm(msg)
245
- end
224
+ return STDIN.gets.to_s =~ /n|q/i ? false : true
246
225
  end
247
226
 
248
- # Choose one file to operate
249
227
  def choose_one(stuff)
250
- if stuff.size == 1
251
- return stuff[0]
252
- elsif stuff.size > 1
228
+ if stuff.size > 0
253
229
  stuff.each_index do |x|
254
230
  colorful("%2s" % (x+1).to_s,"line_number",false)
255
- printf " %-20s \t" % [stuff[x].rstrip]
256
- print "\n" if (x+1)%4 == 0
231
+ printf "%-22s\t" % [stuff[x].rstrip]
232
+ print "\n" if (x+1)%3 == 0
257
233
  end
258
234
  printf "\n"
259
235
  num = choose_range(stuff.size)
260
- return stuff[num-1].to_s.strip
236
+ return stuff[num-1].to_s.strip #return the filename
261
237
  end
262
238
  rescue #Rescue for user input q to quit
263
239
  end
264
240
 
265
- #choose a valid range
241
+ #choose a valid number
266
242
  def choose_range(size)
267
243
  colorful("Please Input A Valid Number To Choose (1..#{size}) (q to quit): ","notice",false)
268
244
  num = STDIN.gets
269
- return if num =~ /q/i
270
- (1..size).member?(num.to_i) ? (return num.to_i) : choose_range(size)
245
+ return false if num =~ /q/i
246
+ choosed_num = num.strip.empty? ? 1 : num.to_i
247
+ (1..size).member?(choosed_num) ? (return choosed_num) : choose_range(size)
271
248
  end
272
249
  end
data/yac.gemspec CHANGED
@@ -1,5 +1,5 @@
1
1
  GEM = "yac"
2
- VER = "1.0.4"
2
+ VER = "1.1.0"
3
3
  DATE = %q{2008-10-28}
4
4
  AUTHOR = "Jinzhu Zhang"
5
5
  EMAIL = "wosmvp@gmail.com"
@@ -25,7 +25,7 @@ Gem::Specification.new do |s|
25
25
  s.autorequire = 'yac'
26
26
  s.executables = ["yac"]
27
27
 
28
- s.files = %w[README.rdoc yac.gemspec resources/yacrc bin/yac lib/yac.rb lib/format.rb]
28
+ s.files = %w[README.rdoc yac.gemspec resources/yacrc bin/yac lib/yac.rb lib/format.rb lib/git.rb]
29
29
 
30
30
  #s.has_rdoc = true
31
31
  s.rdoc_options = ["--quiet", "--title", "YAC => Yet Another Cheat", "--opname", "index.html", "--line-numbers", "--main", "README.rdoc", "--inline-source"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yac
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jinzhu Zhang
@@ -37,6 +37,7 @@ files:
37
37
  - bin/yac
38
38
  - lib/yac.rb
39
39
  - lib/format.rb
40
+ - lib/git.rb
40
41
  has_rdoc: false
41
42
  homepage: http://www.zhangjinzhu.com
42
43
  post_install_message: