nowa-chit 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Manifest.txt +3 -1
  2. data/README.txt +15 -8
  3. data/lib/chit.rb +105 -38
  4. data/resources/chitrc +1 -0
  5. metadata +2 -2
@@ -4,4 +4,6 @@ Rakefile
4
4
  bin/chit
5
5
  lib/chit.rb
6
6
  lib/wrap.rb
7
- resources/chitrc
7
+ resources/chitrc
8
+ tools/chit-bash-completion.sh
9
+ tools/chit.tmCommand
data/README.txt CHANGED
@@ -30,8 +30,6 @@ To get a cheat sheet:
30
30
 
31
31
  $ chit [cheatsheet]
32
32
 
33
- If it does not exist, a new one will be created and waiting for editing. Leave it blank and quit the editor if you don't want to add a new one.
34
-
35
33
  To edit a cheat sheet, use the --edit/-e switch.
36
34
 
37
35
  $ cheat [cheatsheet] --edit
@@ -42,11 +40,11 @@ $ cheat [cheatsheet] --add
42
40
 
43
41
  During editing a cheat sheet, empty the content will get the cheat sheet removed.
44
42
 
45
- A prefix '@' indicates the cheat sheet is in private mode. A private cheat sheet is kept in another repository.
43
+ A prefix '@' indicates the cheat sheet is in special mode. In this mode cheat sheet is kept in another repositories.
46
44
 
47
- To get your private cheat sheet:
45
+ To specified repository cheat sheet in:
48
46
 
49
- $ chit @[cheatsheet]
47
+ $ chit @[repos_name] cheatsheet
50
48
 
51
49
  The prefix '@' works the same for both --edit/-e and --add/-a.
52
50
 
@@ -62,11 +60,19 @@ $ chit [all|sheets]
62
60
 
63
61
  To show all the private cheat sheets:
64
62
 
65
- $ chit @[all|sheets]
63
+ $ chit @[repos_name] all|sheets
64
+
65
+ To find cheat sheets begin with 'name', use the --find/-f switch
66
+
67
+ $ chit name --find
68
+
69
+ To search cheat sheets content with 'text', use the --search/-s switch
70
+
71
+ $ chit text --search
66
72
 
67
- To search cheat sheets begin with 'name', use the --search/-s switch
73
+ To move or rename a sheet, use '--mv/-m' switch
68
74
 
69
- $ chit name --search
75
+ $ chit zsh_if zsh/if -m
70
76
 
71
77
  == INSTALL:
72
78
 
@@ -79,6 +85,7 @@ chit --init
79
85
  Before run 'chit', you may want to config ~/.chitrc which is a YAML file.
80
86
 
81
87
  * root: local path to store the cheat sheet. By default, it is ~/.chit
88
+ * add_if_not_exist: when set as 'true', if no sheets found, a new one will be created and waiting for editing. Leave it blank and quit the editor if you don't want to add a new one.
82
89
  * respo: you can set not only one repository
83
90
  * main:
84
91
  * clone-from: where to get the public cheat sheets. You can use git://github.com/robin/chitsheet.git, which is a snap shoot of http://cheat.errtheblog.com/.
@@ -3,15 +3,15 @@ $:.unshift File.dirname(__FILE__)
3
3
 
4
4
  module Chit
5
5
  extend self
6
- VERSION = '0.0.5'
6
+ VERSION = '0.0.6'
7
7
 
8
8
  defaults = {
9
- 'root' => "#{ENV['HOME']}/.chit"
9
+ 'root' => File.join("#{ENV['HOME']}",".chit")
10
10
  }
11
11
 
12
- CHITRC = "#{ENV['HOME']}/.chitrc"
12
+ CHITRC = File.join("#{ENV['HOME']}",".chitrc")
13
13
 
14
- FileUtils.cp(File.join(File.dirname(__FILE__), "../resources/chitrc"), CHITRC) unless File.exist?(CHITRC)
14
+ FileUtils.cp(File.join(File.dirname(__FILE__), "..","resources","chitrc"), CHITRC) unless File.exist?(CHITRC)
15
15
 
16
16
  CONFIG = defaults.merge(YAML.load_file(CHITRC))
17
17
 
@@ -32,9 +32,16 @@ module Chit
32
32
  end
33
33
 
34
34
  unless File.exist?(sheet_file)
35
- add(sheet_file)
35
+ if args.delete('--no-add').nil? && CONFIG['add_if_not_exist']
36
+ add(sheet_file)
37
+ else
38
+ puts "Error!:\n #{@sheet} not found"
39
+ puts "Possible sheets:"
40
+ search_title
41
+ end
36
42
  else
37
- show(sheet_file)
43
+ format = 'html' if args.delete('--html')
44
+ show(sheet_file,format)
38
45
  end
39
46
  end
40
47
 
@@ -47,17 +54,26 @@ module Chit
47
54
  is_private = (@sheet =~ /^@(.*)/)
48
55
  if is_private
49
56
  @curr_repos = $1
50
- @sheet = args.length > 1 ? args.shift : 'chit'
57
+ @sheet = args.length > 0 ? args.shift : 'chit'
51
58
  end
52
59
 
53
- working_dir = is_private ? repos_path(@curr_repos) : main_path
54
- @git = Git.open(working_dir)
60
+ @working_dir = is_private ? repos_path(@curr_repos) : main_path
61
+ @git = Git.open(@working_dir)
55
62
 
56
- @fullpath = File.join(working_dir, "#{@sheet}.yml")
63
+ @fullpath = File.join(@working_dir, "#{@sheet}.yml")
57
64
 
58
65
  add(sheet_file) and return if (args.delete('--add')||args.delete('-a'))
59
66
  edit(sheet_file) and return if (args.delete('--edit')||args.delete('-e'))
60
- search and return if (args.delete('--search')||args.delete('-s'))
67
+ rm(sheet_file) and return if (args.delete('--delete')||args.delete('-d'))
68
+ search_title and return if (args.delete('--find')||args.delete('-f'))
69
+ search_content and return if (args.delete('--search')||args.delete('-s'))
70
+
71
+ if (args.delete('--mv') || args.delete('-m'))
72
+ target = args.shift
73
+ mv_to(target) and return if target
74
+ puts "Target not specified!"
75
+ return
76
+ end
61
77
  true
62
78
  end
63
79
 
@@ -65,10 +81,34 @@ module Chit
65
81
  puts all_sheets.sort.join("\n")
66
82
  end
67
83
 
68
- def search
84
+ def mv_to(target)
85
+ if target =~ /^@(.*)/
86
+ target = $1
87
+ end
88
+ target_path = File.join(@working_dir, "#{target}.yml")
89
+ prepare_dir(target_path)
90
+ @git.lib.mv(sheet_file, target_path)
91
+ sheet = YAML.load(IO.read(target_path)).to_a.first
92
+ body = sheet[-1]
93
+ title = parse_title(target)
94
+ open(target_path,'w') {|f| f << {title => body}.to_yaml}
95
+ @git.add
96
+ @git.commit_all(" #{@sheet} moved to #{target}")
97
+ end
98
+
99
+ def search_content
100
+ @git.grep(@sheet).each {|file, lines|
101
+ title = title_of_file(file.split(':')[1])
102
+ lines.each {|l|
103
+ puts "#{title}:#{l[0]}: #{l[1]}"
104
+ }
105
+ }
106
+ end
107
+
108
+ def search_title
69
109
  reg = Regexp.compile("^#{@sheet}")
70
110
  files = all_sheets.select {|sheet| sheet =~ reg }
71
- puts files.sort.join("\n")
111
+ puts " " + files.sort.join("\n ")
72
112
  true
73
113
  end
74
114
 
@@ -98,7 +138,10 @@ module Chit
98
138
  puts "Private chit initialized."
99
139
  else
100
140
  puts "Initialize private chit from scratch to #{CONFIG['root']}/private"
101
- Git.init(private_path)
141
+ git = Git.init(private_path)
142
+ FileUtils.touch(File.join(CONFIG['root'],'private','.gitignore'))
143
+ git.add
144
+ git.commit_all("init private repository")
102
145
  puts "Private chit initialized."
103
146
  end
104
147
  else
@@ -135,7 +178,7 @@ module Chit
135
178
  CONFIG['repos'][$1] ||= {}
136
179
  CONFIG['repos'][$1][$2] = $3
137
180
  unless File.exist?(repos_path($1))
138
- puts "Initialize chit repository $1 to #{CONFIG['root']}/$1"
181
+ puts "Initialize chit repository #{$1} to #{CONFIG['root']}/#{$1}"
139
182
  Git.init(repos_path($1))
140
183
  puts "Private chit initialized."
141
184
  end
@@ -161,47 +204,67 @@ module Chit
161
204
  File.join(CONFIG['root'], 'private')
162
205
  end
163
206
 
164
- def show(sheet_file)
165
- sheet = YAML.load(IO.read(sheet_file)).to_a.first
207
+ def show(file,format=nil)
208
+ sheet = YAML.load(IO.read(file)).to_a.first
166
209
  sheet[-1] = sheet.last.join("\n") if sheet[-1].is_a?(Array)
167
- puts sheet.first + ':'
168
- puts ' ' + sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap
210
+ case format
211
+ when 'html'
212
+ puts "<h1>#{sheet.first}</h1>"
213
+ puts "<pre>#{sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap}</pre>"
214
+ else
215
+ puts sheet.first + ':'
216
+ puts ' ' + sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap
217
+ end
169
218
  end
170
219
 
171
- def rm(sheet_file)
172
- @git.remove(sheet_file)
173
- @git.commit_all("-")
220
+ def rm(file)
221
+ @git.remove(file)
222
+ @git.commit_all("#{@sheet} removed")
174
223
  rescue Git::GitExecuteError
175
- FileUtils.rm_rf(sheet_file)
224
+ FileUtils.rm_rf(file)
176
225
  end
177
226
 
178
- def add(sheet_file)
179
- unless File.exist?(sheet_file)
180
- breaker = sheet_file.rindex('/')+1
181
- path = sheet_file[0,breaker]
182
- title = @sheet.gsub(/\//,'::')
183
- FileUtils.mkdir_p(path)
227
+ def add(file)
228
+ unless File.exist?(file)
229
+ prepare_dir(file)
230
+ title = parse_title(@sheet)
184
231
  yml = {"#{title}" => ''}.to_yaml
185
- open(sheet_file, 'w') {|f| f << yml}
232
+ open(file, 'w') {|f| f << yml}
186
233
  end
187
- edit(sheet_file)
234
+ edit(file)
188
235
  end
189
236
 
190
- def edit(sheet_file)
191
- sheet = YAML.load(IO.read(sheet_file)).to_a.first
237
+ def edit(file)
238
+ sheet = YAML.load(IO.read(file)).to_a.first
192
239
  sheet[-1] = sheet.last.gsub("\r", '')
193
240
  body, title = write_to_tempfile(*sheet), sheet.first
194
241
  if body.strip.empty?
195
- rm(sheet_file)
242
+ rm(file)
196
243
  else
197
- open(sheet_file,'w') {|f| f << {title => body}.to_yaml}
198
- @git.add
199
- @git.commit_all("-")
244
+ begin
245
+ open(file,'w') {|f| f << {title => body}.to_yaml}
246
+ @git.add
247
+ st = @git.status
248
+ unless st.added.empty? && st.changed.empty? && st.deleted.empty? && st.untracked.empty?
249
+ @git.commit_all(" #{@sheet} updated")
250
+ rescue Git::GitExecuteError
251
+ puts "ERROR: can not commit #{@curr_repos} chit."
252
+ puts $!
253
+ end
200
254
  end
201
255
  true
202
256
  end
203
257
 
204
258
  private
259
+ def parse_title(sheet_name)
260
+ sheet_name.split(File::Separator).join('::')
261
+ end
262
+
263
+ def prepare_dir(file)
264
+ breaker = file.rindex(File::Separator)+1
265
+ path = file[0,breaker]
266
+ FileUtils.mkdir_p(path)
267
+ end
205
268
 
206
269
  def editor
207
270
  ENV['VISUAL'] || ENV['EDITOR'] || "vim"
@@ -223,7 +286,11 @@ module Chit
223
286
 
224
287
  def all_sheets
225
288
  @git.ls_files.to_a.map {|f|
226
- f[0][0..((f[0].rindex('.')||0) - 1)]}
289
+ title_of_file(f[0])}
290
+ end
291
+
292
+ def title_of_file(f)
293
+ f[0..((f.rindex('.')||0) - 1)]
227
294
  end
228
295
 
229
296
  end
@@ -1,4 +1,5 @@
1
1
  # root:
2
+ # add_if_not_exist: true
2
3
  repos:
3
4
  main:
4
5
  clone-from: git://github.com/robin/chitsheet.git
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nowa-chit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Lu
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  requirements: []
75
75
 
76
76
  rubyforge_project: chit
77
- rubygems_version: 1.0.1
77
+ rubygems_version: 1.2.0
78
78
  signing_key:
79
79
  specification_version: 2
80
80
  summary: Chit is A command line tool for cheat sheet utility based on git.