wosmvp-yac 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/README.cn +57 -0
  2. data/README.rdoc +57 -0
  3. data/bin/yac +3 -0
  4. data/lib/yac.rb +237 -0
  5. data/resources/yacrc +46 -0
  6. data/yac.gemspec +33 -0
  7. metadata +74 -0
data/README.cn ADDED
@@ -0,0 +1,57 @@
1
+ = YAC => Yet Another Cheet (Powered By Ruby)
2
+
3
+ == FEATURES:
4
+ YAC was inspired by 'chit' (http://github.com/robin/chit) by robin and 'cheat' (http://cheat.errtheblog.com/) by Chris Wanstrath
5
+ what's the different:
6
+ @@@Colorful and Sexy!@@@
7
+
8
+ == How To Use
9
+
10
+ === 初始化一个仓库:
11
+ $ yac init
12
+ === 得到cheat表:
13
+ $ yac [cheatsheet]
14
+ === 修改一个cheat表
15
+ $ yac edit [cheatsheet]
16
+ === 加入一个cheat表
17
+ $ yac add [cheatsheet]
18
+ === 删除一个cheat表
19
+ $ yac rm [cheatsheet]
20
+ === 按照cheat表的文件名搜索
21
+ $ yac name [keyword]
22
+ === 按照cheat表的内容搜索
23
+ $ yac content [keyword]
24
+ === 更新git仓库
25
+ $ yac update main 更新主仓库
26
+ $ yac update private 更新个人仓库
27
+ $ yac update 全部更新
28
+ === 帮助
29
+ $ yac help
30
+ === 打开仓库shell => Comming Soon :)
31
+ $ yac shell main 打开主仓库的shell
32
+ $ yac shell 打开个人仓库的shell
33
+ === 文档格式
34
+ 使用.ch后缀,在行首加一个=为一级标题,加N个=为N级标题,不同级别标题可以加入不同颜色高亮
35
+ 在行首加入#将忽略该行,使用 '\#'可以得到 '#'的效果
36
+ 在某词两端加入三个@,例:@@\@ HI @@\@(去掉两个/)将会高亮 HI
37
+ === 设置高亮的颜色
38
+ 默认用主目录下的 .yacrc来设置,详细内容,参见 resources目录 yacrc文件
39
+ === 其它
40
+ 在 表名/关键词 前加入@将会只搜索主仓库
41
+ 更多性感使用方法,参见源码 :)
42
+
43
+ == REQUIREMENTS:
44
+
45
+ * Ruby
46
+ * rubygems
47
+ * git
48
+
49
+ == LICENSE:
50
+
51
+ This software is shared by GPL3 License
52
+
53
+ Copyright (c) 2008 Jinzhu Zhang
54
+
55
+ == BY: Jinzhu Zhang
56
+ http://www.zhangjinzhu.com
57
+ wosmvp (no-spam) gmail (no-spam) com
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = YAC => Yet Another Cheet (Powered By Ruby)
2
+
3
+ == FEATURES:
4
+ YAC was inspired by 'chit' (http://github.com/robin/chit) by robin and 'cheat' (http://cheat.errtheblog.com/) by Chris Wanstrath
5
+ what's the different:
6
+ @@@Colorful and Sexy!@@@
7
+
8
+ == How To Use
9
+
10
+ === To initialize repositories:
11
+ $ yac init
12
+ === To get a cheat sheet:
13
+ $ yac [cheatsheet]
14
+ === To edit a cheat sheet
15
+ $ yac edit [cheatsheet]
16
+ === To add a cheat sheet
17
+ $ yac add [cheatsheet]
18
+ === To rm a cheat sheet
19
+ $ yac rm [cheatsheet]
20
+ === Search according the sheets name
21
+ $ yac name [keyword]
22
+ === Search according the sheets content
23
+ $ yac content [keyword]
24
+ === Update repositories
25
+ $ yac update main => Update main repository
26
+ $ yac update private => Update private repository
27
+ $ yac update => Update All
28
+ === Help
29
+ $ yac help
30
+ === Shell => Comming Soon :)
31
+ $ yac shell main => Open the main repository's shell
32
+ $ yac shell => Open the private repository's shell
33
+ === Documents format
34
+ Use .ch as the suffix ,At the beginning of the row,plus N = for the N-class title
35
+ Use '#' at the beginning of a row to comment the line,You can use '\#' to get '#'
36
+ Use three @ wrap some words to highlight it,for example: @@\@ HI @@\@ (remove the \) will highlight HI
37
+ === Set the color of highlight
38
+ Default use .yacrc under home directory to set,, details, see the example file under the resources directory
39
+ === Others
40
+ Add @ before [cheatsheet]/[keyword] will only search the main depot
41
+ See also the source code to get more sexy ways
42
+
43
+ == REQUIREMENTS:
44
+
45
+ * Ruby
46
+ * rubygems
47
+ * git
48
+
49
+ == LICENSE:
50
+
51
+ This software is shared by GPL3 License
52
+
53
+ Copyright (c) 2008 Jinzhu Zhang
54
+
55
+ == BY: Jinzhu Zhang
56
+ http://www.zhangjinzhu.com
57
+ wosmvp (no-spam) gmail (no-spam) com
data/bin/yac ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.join(File.dirname(__FILE__), "..", "lib", "yac.rb")
3
+ Yac.new(ARGV)
data/lib/yac.rb ADDED
@@ -0,0 +1,237 @@
1
+ %w(rubygems git fileutils yaml).each {|f| require f}
2
+
3
+ module Yac
4
+ extend self
5
+
6
+ YACRC = File.join("#{ENV['HOME']}",".yacrc")
7
+ FileUtils.cp(File.join(File.dirname(__FILE__), "..","resources","yacrc"), YACRC) unless File.exist?(YACRC)
8
+ CONFIG = YAML.load_file(File.join(ENV['HOME'],".yacrc"))
9
+ CONFIG["root"] ||= File.join(ENV['HOME'],".yac")
10
+
11
+ @main_path = File.join(CONFIG["root"],"/main/")
12
+ @pri_path = File.join(CONFIG["root"],"/private/")
13
+ @main_git = Git.open(@main_path) if File.exist?(@main_path)
14
+ @pri_git = Git.open(@pri_path)if File.exist?(@pri_path)
15
+
16
+ def new(args)
17
+ unless File.exist?(@main_path) && File.exist?(@pri_path)
18
+ return unless init
19
+ end
20
+ @all_result = []
21
+ help && exit if args.empty?
22
+ case args.first
23
+ when "show" then show(args[1,args.size])
24
+ when "name" then search(args[1,args.size],"name")
25
+ when "content" then search(args[1,args.size],"content")
26
+ when "update" then update(args[1,args.size])
27
+ when "add" then add(args[1,args.size])
28
+ when "edit" then edit(args[1,args.size])
29
+ when "help" then help
30
+ when "shell" then shell(args[1,args.size])
31
+ when "rm" then rm(args[1,args.size])
32
+ when "init" then init
33
+ else show(args)
34
+ end
35
+ show_possible_result
36
+ end
37
+
38
+ def show(args)
39
+ args.each {|x| show_single(x)}
40
+ end
41
+
42
+ def search(args,type = "name")
43
+ args.each {|x| search_single(x,type)}
44
+ end
45
+
46
+ def update(args)
47
+ begin
48
+ if args
49
+ @main_path.pull if args.to_s =~ /main/
50
+ @pri_git.pull if args.to_s =~ /pri/
51
+ else
52
+ @main_path.pull
53
+ @pri_git.pull
54
+ end
55
+ rescue
56
+ puts "ERROR: can not update #{args}"
57
+ puts $!
58
+ end
59
+ end
60
+
61
+ def edit(args)
62
+ args.each {|x| edit_single(x)}
63
+ end
64
+ alias add edit
65
+
66
+ def rm(args)
67
+ args.each {|x| rm_single(x)}
68
+ end
69
+
70
+ def help
71
+ format_file(File.dirname(__FILE__)+"/../README.rdoc")
72
+ end
73
+
74
+ def shell(args = "pri")
75
+ puts "Comming Soon :)"
76
+ #case args.to_s
77
+ #when /main/
78
+ # system("cd #{@main_path}")
79
+ #when /pri/
80
+ # system("cd #{@pri_path}")
81
+ #end
82
+ end
83
+
84
+ protected
85
+ def format_file(file)
86
+ @level = 0
87
+ File.new(file).each do |x|
88
+ format_section(x)
89
+ end
90
+ end
91
+
92
+ def format_section(section)
93
+ case section
94
+ when /^(=+)\s+(.*)/
95
+ @level = $1.size
96
+ puts "\033[" + CONFIG["level#{@level}"].to_s + "m" + "\s"*2*(@level-1) + $2 +"\033[0m"
97
+ when /^(\s*)#/
98
+ else
99
+ puts section.sub(/^\#/,"#").sub(/^\s*/, "\s" * @level * 2 ).gsub(/@@@(.*)@@@/,"\033[" + CONFIG["empha"].to_s + "m" + '\1' + "\033[0m")
100
+ end
101
+ end
102
+
103
+ def full_path(args)
104
+ if args =~ /^@/
105
+ @file_path = @main_path + args.sub(/^@/,"") + ".ch"
106
+ @working_git = @main_git
107
+ else
108
+ @file_path = @pri_path + args + ".ch"
109
+ @working_git = @pri_git
110
+ end
111
+ end
112
+
113
+ def rm_single(args)
114
+ full_path(args)
115
+ begin
116
+ @working_git.remove(@file_path)
117
+ @working_git.commit_all("#{args.sub(/^@/,"")}.ch removed")
118
+ rescue Git::GitExecuteError
119
+ FileUtils.rm_rf(@file_path)
120
+ end
121
+ end
122
+
123
+ def edit_single(args)
124
+ full_path(args)
125
+ system("#{editor} #{@file_path}")
126
+ begin
127
+ @working_git.add
128
+ @working_git.commit_all(" #{args.sub(/^@/,"")}.ch Updated")
129
+ rescue
130
+ end
131
+ end
132
+
133
+ def editor
134
+ ENV['VISUAL'] || ENV['EDITOR'] || "vim"
135
+ end
136
+
137
+ def show_single(args)
138
+ result = search_single(args)
139
+ if result.size == 1
140
+ puts "\n\033[#{CONFIG["filename"]}m#{result.first}\033[0m\n"
141
+ format_file(result.first)
142
+ else
143
+ result.map do |x|
144
+ if x =~ /\/#{args}\.ch/
145
+ puts "\n\033[#{CONFIG["filename"]}m#{x}\033[0m\n"
146
+ format_file(x)
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ def search_single(args,type="name")
153
+ if type =~ /name/
154
+ search_name(args)
155
+ else
156
+ search_content(args)
157
+ end
158
+ end
159
+
160
+ def search_name(args)
161
+ if args =~ /^@/ && main = args.sub(/^@/,"")
162
+ @private_result = []
163
+ @main_result = @main_git.ls_files.keys.grep(/#{main}/)
164
+ else
165
+ @private_result = @pri_git.ls_files.keys.grep(/#{args}/)
166
+ @main_result = @main_git.ls_files.keys.grep(/#{args}/)
167
+ end
168
+ #ADD to all possible result
169
+ @all_result << @main_result.collect {|x| "@" + x}
170
+ @all_result << @private_result
171
+
172
+ #Remove duplicate files
173
+ @private_result.map do |x|
174
+ @main_result.delete(x)
175
+ end
176
+ #Return full path
177
+ return (@main_result.collect {|x| @main_path +x}).concat(@private_result.collect {|x| @pri_path +x})
178
+ end
179
+
180
+ def search_content(args)
181
+ @main_git.grep(args).each do |file, lines|
182
+ title = title_of_file(file.split(':')[1])
183
+ lines.each do |l|
184
+ puts "@#{title}:#{l[0]}: #{l[1]}"
185
+ end
186
+ end
187
+ @pri_git.grep(args).each do |file, lines|
188
+ title = title_of_file(file.split(':')[1])
189
+ lines.each do |l|
190
+ puts "#{title}:#{l[0]}: #{l[1]}"
191
+ end
192
+ end
193
+ end
194
+
195
+ def title_of_file(f)
196
+ f[0..((f.rindex('.')||0) - 1)]
197
+ end
198
+
199
+ def show_possible_result
200
+ 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?
201
+ end
202
+
203
+ def init
204
+ FileUtils.mkdir_p(CONFIG['root'])
205
+ if CONFIG['main']['clone-from']
206
+ if File.exist?(@main_path)
207
+ puts "Main repository has already been initialized."
208
+ else
209
+ puts "Initialize main repository from #{CONFIG['main']['clone-from']} to #{CONFIG['root']}/main"
210
+ Git.clone(CONFIG['main']['clone-from'], 'main', :path => CONFIG['root'])
211
+ puts "Main repository initialized."
212
+ end
213
+ else
214
+ puts "ERROR: configuration for main repository repository is missing!"
215
+ return
216
+ end
217
+
218
+ unless File.exist?(@pri_path)
219
+ if CONFIG['private'] && CONFIG['private']['clone-from']
220
+ puts "Initialize private repository from #{CONFIG['private']['clone-from']} to #{CONFIG['root']}/private"
221
+ Git.clone(CONFIG['private']['clone-from'], 'private', :path => CONFIG['root'])
222
+ puts "Private repository initialized."
223
+ else
224
+ puts "Initialize private repository from scratch to #{CONFIG['root']}/private"
225
+ git = Git.init(@pri_path)
226
+ git.add
227
+ git.commit_all("init private repository")
228
+ puts "Private repository initialized."
229
+ end
230
+ else
231
+ puts "Private repository has already been initialized."
232
+ end
233
+ @main_git = Git.open(@main_path) if File.exist?(@main_path)
234
+ @pri_git = Git.open(@pri_path)if File.exist?(@pri_path)
235
+ puts "Repository init done."
236
+ end
237
+ end
data/resources/yacrc ADDED
@@ -0,0 +1,46 @@
1
+ #root:
2
+ # add_if_not_exist: true
3
+ main:
4
+ clone-from: git://github.com/wosmvp/cheat.git
5
+ # push-to:
6
+ private:
7
+ # clone-from:
8
+ # push-to:
9
+
10
+
11
+ #Set Color
12
+ #1 - Change text to hicolor (bold) mode
13
+ #4 - " " " Underline
14
+ #5 - " " " Blink
15
+ #8 - " " " Hidden (same color as bg)
16
+ #30 - " " " Black
17
+ #31 - " " " Red
18
+ #32 - " " " Green
19
+ #33 - " " " Yellow
20
+ #34 - " " " Blue
21
+ #35 - " " " Magenta
22
+ #36 - " " " Cyan
23
+ #37 - " " " White
24
+ #40 - Change background to Black
25
+ #41 - " " " Red
26
+ #42 - " " " Green
27
+ #43 - " " " Yellow
28
+ #44 - " " " Blue
29
+ #45 - " " " Magenta
30
+ #46 - " " " Cyan
31
+ #47 - " " " White
32
+ #0 - Turn off all attributes.
33
+ #7 - Change to Black text on a White background
34
+
35
+ level1: 31
36
+ level2: 32
37
+ level3: 33
38
+ level4: 34
39
+ level5: 0;31;40
40
+ level6: 0;31;40
41
+ level7: 0;31;40
42
+
43
+ empha: 32;40
44
+ possible_result_title: 33;41
45
+ possible_result_content: 31
46
+ filename: 31
data/yac.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ GEM = "yac"
2
+ VER = "0.0.2"
3
+ AUTHOR = "Jinzhu Zhang"
4
+ EMAIL = "wosmvp@gmail.com"
5
+ HOMEPAGE = "http://www.zhangjinzhu.com"
6
+ SUMMARY = "Yet Another Cheat"
7
+
8
+ Gem::Specification.new do |s|
9
+
10
+ s.specification_version = 2 if s.respond_to? :specification_version=
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+
13
+ s.name = GEM
14
+ s.version = VER
15
+ s.author = AUTHOR
16
+ s.date = %q{2008-10-10}
17
+ s.email = EMAIL
18
+ s.homepage = HOMEPAGE
19
+ s.summary = SUMMARY
20
+ s.description = s.summary
21
+
22
+ s.require_path = 'lib'
23
+ s.autorequire = 'yac'
24
+ s.executables = ["yac"]
25
+
26
+ s.files = %w[README.rdoc README.cn yac.gemspec resources/yacrc bin/yac lib/yac.rb]
27
+
28
+ s.has_rdoc = true
29
+ s.rdoc_options = ["--quiet", "--title", "YAC => Yet Another Cheat", "--opname", "index.html", "--line-numbers", "--main", "README.rdoc", "--inline-source"]
30
+ s.extra_rdoc_files = "README.rdoc"
31
+
32
+ s.add_dependency("schacon-git", ">1.0.0")
33
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wosmvp-yac
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jinzhu Zhang
8
+ autorequire: yac
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: schacon-git
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">"
21
+ - !ruby/object:Gem::Version
22
+ version: 1.0.0
23
+ version:
24
+ description: Yet Another Cheat
25
+ email: wosmvp@gmail.com
26
+ executables:
27
+ - yac
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.rdoc
32
+ files:
33
+ - README.rdoc
34
+ - README.cn
35
+ - yac.gemspec
36
+ - resources/yacrc
37
+ - bin/yac
38
+ - lib/yac.rb
39
+ has_rdoc: true
40
+ homepage: http://www.zhangjinzhu.com
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --quiet
44
+ - --title
45
+ - YAC => Yet Another Cheat
46
+ - --opname
47
+ - index.html
48
+ - --line-numbers
49
+ - --main
50
+ - README.rdoc
51
+ - --inline-source
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Yet Another Cheat
73
+ test_files: []
74
+