yac 0.1.2 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/yac.rb +55 -100
  2. data/resources/yacrc +9 -7
  3. data/yac.gemspec +3 -2
  4. metadata +4 -4
data/lib/yac.rb CHANGED
@@ -1,6 +1,8 @@
1
- %w(rubygems git fileutils yaml).each {|f| require f}
1
+ $:.unshift File.dirname(__FILE__)
2
+ %w(rubygems git fileutils yaml format).each {|f| require f}
2
3
 
3
4
  module Yac
5
+ include Format
4
6
  extend self
5
7
 
6
8
  YACRC = File.join("#{ENV['HOME']}",".yacrc")
@@ -34,6 +36,26 @@ module Yac
34
36
  rescue
35
37
  end
36
38
 
39
+ def init
40
+ FileUtils.mkdir_p(CONFIG['root'])
41
+ {"main" => @main_path,"private" => @pri_path}.each do |name,path|
42
+ unless File.exist?(path)
43
+ if CONFIG["#{name}"] && CONFIG["#{name}"]['clone-from']
44
+ puts "Initialize #{name} repository from #{CONFIG[name]['clone-from']} to #{CONFIG['root']}/#{name}"
45
+ Git.clone(CONFIG["#{name}"]['clone-from'], name, :path => CONFIG['root'])
46
+ else
47
+ puts "Initialize #{name} repository from scratch to #{CONFIG['root']}/#{name}"
48
+ git = Git.init(path)
49
+ git.add
50
+ git.commit_all("init #{name} repository")
51
+ end
52
+ puts "#{name} repository initialized."
53
+ @main_git = Git.open(@main_path) if File.exist?(@main_path)
54
+ @pri_git = Git.open(@pri_path)if File.exist?(@pri_path)
55
+ end
56
+ end
57
+ end
58
+
37
59
  def show(args)
38
60
  args.each {|x| show_single(x)}
39
61
  end
@@ -43,18 +65,14 @@ module Yac
43
65
  end
44
66
 
45
67
  def update(args)
46
- begin
47
- unless args.empty?
48
- @pri_git.pull if args.to_s =~ /pri/
68
+ unless args.empty?
69
+ @pri_git.pull if args.to_s =~ /pri/
49
70
  @main_git.pull if args.to_s =~ /main/
50
- else
51
- @main_git.pull
52
- @pri_git.pull
53
- end
54
- rescue
55
- puts "ERROR: can not update the repository,"
56
- puts $!
71
+ else
72
+ @main_git.pull && @pri_git.pull
57
73
  end
74
+ rescue
75
+ puts "ERROR: can not update the repository,\n #{$!}"
58
76
  end
59
77
 
60
78
  def edit(args)
@@ -72,50 +90,15 @@ module Yac
72
90
  def shell(args)
73
91
  case args.to_s
74
92
  when /main/
75
- puts "\033[31m Welcome To The Main Yac Repository \033[0m"
93
+ colorful(" Welcome To The Main Yac Repository","head1")
76
94
  system "cd #{@main_path}; sh"
77
95
  else
78
- puts "\033[31m Welcome To The Private Yac Repository \033[0m"
96
+ colorful(" Welcome To The Private Yac Repository","head1")
79
97
  system "cd #{@pri_path}; sh"
80
98
  end
81
99
  end
82
100
 
83
101
  protected
84
- def format_file(file)
85
- @level = 0
86
- case `file #{file}`
87
- when / PDF /
88
- puts "Please Modify ~/.yacrc To Provide A Valid Command To Open PDF Document" unless system("#{CONFIG["pdf_command"]||'evince'} #{file}")
89
- when /( image )|(\.svg)/
90
- puts "Please Modify ~/.yacrc To Provide A Valid Command To Open Image Document" unless system("#{CONFIG["pic_command"]||'eog'} #{file}")
91
- else
92
- File.new(file).each do |x|
93
- format_section(x)
94
- end
95
- end
96
- end
97
-
98
- def format_section(section)
99
- case section
100
- when /^(=+)\s+(.*)/
101
- @level = $1.size
102
- puts "\033[" + CONFIG["level#{@level}"].to_s + "m" + "\s"*2*(@level-1) + $2 +"\033[0m"
103
- when /^(\s*)#/
104
- else
105
- puts section.sub(/^\#/,"#").sub(/^\s*/, "\s" * @level * 2 ).gsub(/@@@(.*)@@@/,"\033[" + CONFIG["empha"].to_s + "m" + '\1' + "\033[0m")
106
- end
107
- end
108
-
109
- def full_path(args)
110
- if args =~ /^@/
111
- @file_path = @main_path + args.sub(/^@/,"") + ".ch"
112
- @working_git = @main_git
113
- else
114
- @file_path = @pri_path + args + ".ch"
115
- @working_git = @pri_git
116
- end
117
- end
118
-
119
102
  def rm_single(args)
120
103
  full_path(args)
121
104
  begin
@@ -124,7 +107,7 @@ module Yac
124
107
  rescue Git::GitExecuteError
125
108
  FileUtils.rm_rf(@file_path)
126
109
  end
127
- end
110
+ end
128
111
 
129
112
  def edit_single(args)
130
113
  full_path(args)
@@ -134,19 +117,15 @@ module Yac
134
117
  @working_git.commit_all(" #{args.sub(/^@/,"")}.ch Updated")
135
118
  end
136
119
 
137
- def editor
138
- CONFIG["editor"] || ENV['EDITOR'] || "vim"
139
- end
140
-
141
120
  def show_single(args)
142
121
  result = search_single(args)
143
122
  if result.size == 1
144
- puts "\n\033[#{CONFIG["filename"]}m#{result.first}\033[0m\n"
123
+ colorful(result.first,"filename")
145
124
  format_file(result.first)
146
125
  else
147
126
  result.map do |x|
148
- if x =~ /\/#{args}\.\w+/
149
- puts "\n\033[#{CONFIG["filename"]}m#{x}\033[0m\n"
127
+ if x =~ /\/#{args.sub(/^@/,"")}\.\w+/
128
+ colorful(x,"filename")
150
129
  format_file(x)
151
130
  end
152
131
  end
@@ -169,58 +148,24 @@ module Yac
169
148
  @private_result = @pri_git.ls_files.keys.grep(/#{args}/)
170
149
  @main_result = @main_git.ls_files.keys.grep(/#{args}/)
171
150
  end
172
- #ADD to all possible result
173
- @all_result << @main_result.collect {|x| "@" + x}
174
- @all_result << @private_result
151
+ @all_result << @main_result.collect {|x| "@" + x} << @private_result
175
152
 
176
- #Remove duplicate files
177
153
  @private_result.map do |x|
178
154
  @main_result.delete(x)
179
155
  end
180
- #Return full path
181
156
  return (@main_result.collect {|x| @main_path +x}).concat(@private_result.collect {|x| @pri_path +x})
182
157
  end
183
158
 
184
159
  def search_content(args)
185
- @main_git.grep(args).each do |file, lines|
186
- title = title_of_file(file.split(':')[1])
187
- lines.each do |l|
188
- puts "@#{title}:#{l[0]}: #{l[1]}"
189
- end
190
- end
191
- @pri_git.grep(args).each do |file, lines|
192
- title = title_of_file(file.split(':')[1])
193
- lines.each do |l|
194
- puts "#{title}:#{l[0]}: #{l[1]}"
195
- end
196
- end
197
- end
198
-
199
- def title_of_file(f)
200
- f[0..((f.rindex('.')||0) - 1)]
201
- end
202
-
203
- def show_possible_result
204
- puts "\n\033[" + CONFIG["possible_result_title"].to_s + "mALL POSSIBLE RESULT:\033[0m\n \033[" +CONFIG["possible_result_content"].to_s + "m" + @all_result.join("\s"*2) + "\033[0m" unless @all_result.to_s.empty?
205
- end
206
-
207
- def init
208
- FileUtils.mkdir_p(CONFIG['root'])
209
- {"main" => @main_path,"private" => @pri_path}.each do |name,path|
210
- unless File.exist?(path)
211
- if CONFIG["#{name}"] && CONFIG["#{name}"]['clone-from']
212
- puts "Initialize #{name} repository from #{CONFIG[name]['clone-from']} to #{CONFIG['root']}/#{name}"
213
- Git.clone(CONFIG["#{name}"]['clone-from'], name, :path => CONFIG['root'])
214
- else
215
- puts "Initialize #{name} repository from scratch to #{CONFIG['root']}/#{name}"
216
- git = Git.init(path)
217
- git.add
218
- git.commit_all("init #{name} repository")
219
- end
220
- puts "#{name} repository initialized."
221
- @main_git = Git.open(@main_path) if File.exist?(@main_path)
222
- @pri_git = Git.open(@pri_path)if File.exist?(@pri_path)
223
- end
160
+ result = `cd #{@pri_path} && grep -n #{args} -R *.ch 2>/dev/null`
161
+ result << `cd #{@main_path} && grep -n #{args} -R *.ch 2>/dev/null | sed 's/^/@/g'`
162
+ result.each do |x|
163
+ stuff = x.split(':',3)
164
+ colorful(title_of_file(stuff[0]),"filename",false)
165
+ print " "
166
+ colorful(stuff[1],"line_number",false)
167
+ print " "
168
+ format_section(empha(stuff[2],nil,/((#{args}))/),true)
224
169
  end
225
170
  end
226
171
 
@@ -228,4 +173,14 @@ module Yac
228
173
  dirseparator = @file_path.rindex(File::Separator)+1
229
174
  FileUtils.mkdir_p(@file_path[0,dirseparator])
230
175
  end
176
+
177
+ def full_path(args)
178
+ if args =~ /^@/
179
+ @file_path = @main_path + args.sub(/^@/,"") + ".ch"
180
+ @working_git = @main_git
181
+ else
182
+ @file_path = @pri_path + args + ".ch"
183
+ @working_git = @pri_git
184
+ end
185
+ end
231
186
  end
data/resources/yacrc CHANGED
@@ -35,15 +35,17 @@ private:
35
35
  #0 - Turn off all attributes.
36
36
  #7 - Change to Black text on a White background
37
37
 
38
- level1: 31
39
- level2: 32
40
- level3: 33
41
- level4: 34
42
- level5: 0;31;40
43
- level6: 0;31;40
44
- level7: 0;31;40
38
+ head1: 31
39
+ head2: 32
40
+ head3: 33
41
+ head4: 34
42
+ head5: 0;31;40
43
+ head6: 0;31;40
44
+ head7: 0;31;40
45
45
 
46
46
  empha: 32;40
47
47
  possible_result_title: 33;41
48
48
  possible_result_content: 31
49
49
  filename: 31
50
+ line_number: 32
51
+ text:
data/yac.gemspec CHANGED
@@ -1,5 +1,5 @@
1
1
  GEM = "yac"
2
- VER = "0.1.2"
2
+ VER = "0.1.5"
3
3
  DATE = %q{2008-10-18}
4
4
  AUTHOR = "Jinzhu Zhang"
5
5
  EMAIL = "wosmvp@gmail.com"
@@ -11,6 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.specification_version = 2 if s.respond_to? :specification_version=
12
12
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
13
13
 
14
+ s.rubyforge_project = 'yac'
14
15
  s.name = GEM
15
16
  s.version = VER
16
17
  s.author = AUTHOR
@@ -26,7 +27,7 @@ Gem::Specification.new do |s|
26
27
 
27
28
  s.files = %w[README.rdoc README.cn yac.gemspec resources/yacrc bin/yac lib/yac.rb]
28
29
 
29
- s.has_rdoc = true
30
+ #s.has_rdoc = true
30
31
  s.rdoc_options = ["--quiet", "--title", "YAC => Yet Another Cheat", "--opname", "index.html", "--line-numbers", "--main", "README.rdoc", "--inline-source"]
31
32
  s.extra_rdoc_files = "README.rdoc"
32
33
 
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: 0.1.2
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jinzhu Zhang
@@ -37,7 +37,7 @@ files:
37
37
  - resources/yacrc
38
38
  - bin/yac
39
39
  - lib/yac.rb
40
- has_rdoc: true
40
+ has_rdoc: false
41
41
  homepage: http://www.zhangjinzhu.com
42
42
  post_install_message:
43
43
  rdoc_options:
@@ -66,8 +66,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  version:
67
67
  requirements: []
68
68
 
69
- rubyforge_project:
70
- rubygems_version: 1.2.0
69
+ rubyforge_project: yac
70
+ rubygems_version: 1.3.0
71
71
  signing_key:
72
72
  specification_version: 2
73
73
  summary: Yet Another Cheat