robin-chit 0.0.4 → 0.0.5

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 (3) hide show
  1. data/README.txt +4 -0
  2. data/lib/chit.rb +66 -26
  3. metadata +1 -1
data/README.txt CHANGED
@@ -69,6 +69,10 @@ To search cheat sheets content with 'text', use the --search/-s switch
69
69
 
70
70
  $ chit text --search
71
71
 
72
+ To move or rename a sheet, use '--mv/-m' switch
73
+
74
+ $ chit zsh_if zsh/if -m
75
+
72
76
  == INSTALL:
73
77
 
74
78
  sudo gem install robin-chit -s http://gems.github.com
@@ -3,7 +3,7 @@ $:.unshift File.dirname(__FILE__)
3
3
 
4
4
  module Chit
5
5
  extend self
6
- VERSION = '0.0.4'
6
+ VERSION = '0.0.5'
7
7
 
8
8
  defaults = {
9
9
  'root' => File.join("#{ENV['HOME']}",".chit")
@@ -40,7 +40,8 @@ module Chit
40
40
  search_title
41
41
  end
42
42
  else
43
- show(sheet_file)
43
+ format = 'html' if args.delete('--html')
44
+ show(sheet_file,format)
44
45
  end
45
46
  end
46
47
 
@@ -52,15 +53,22 @@ module Chit
52
53
  is_private = (@sheet =~ /^@(.*)/)
53
54
  @sheet = is_private ? $1 : @sheet
54
55
 
55
- working_dir = is_private ? private_path : main_path
56
- @git = Git.open(working_dir)
56
+ @working_dir = is_private ? private_path : main_path
57
+ @git = Git.open(@working_dir)
57
58
 
58
- @fullpath = File.join(working_dir, "#{@sheet}.yml")
59
+ @fullpath = File.join(@working_dir, "#{@sheet}.yml")
59
60
 
60
61
  add(sheet_file) and return if (args.delete('--add')||args.delete('-a'))
61
62
  edit(sheet_file) and return if (args.delete('--edit')||args.delete('-e'))
62
63
  search_title and return if (args.delete('--find')||args.delete('-f'))
63
64
  search_content and return if (args.delete('--search')||args.delete('-s'))
65
+
66
+ if (args.delete('--mv') || args.delete('-m'))
67
+ target = args.shift
68
+ mv_to(target) and return if target
69
+ puts "Target not specified!"
70
+ return
71
+ end
64
72
  true
65
73
  end
66
74
 
@@ -68,6 +76,21 @@ module Chit
68
76
  puts all_sheets.sort.join("\n")
69
77
  end
70
78
 
79
+ def mv_to(target)
80
+ if target =~ /^@(.*)/
81
+ target = $1
82
+ end
83
+ target_path = File.join(@working_dir, "#{target}.yml")
84
+ prepare_dir(target_path)
85
+ @git.lib.mv(sheet_file, target_path)
86
+ sheet = YAML.load(IO.read(target_path)).to_a.first
87
+ body = sheet[-1]
88
+ title = parse_title(target)
89
+ open(target_path,'w') {|f| f << {title => body}.to_yaml}
90
+ @git.add
91
+ @git.commit_all(" #{@sheet} moved to #{target}")
92
+ end
93
+
71
94
  def search_content
72
95
  @git.grep(@sheet).each {|file, lines|
73
96
  title = title_of_file(file.split(':')[1])
@@ -138,47 +161,64 @@ module Chit
138
161
  File.join(CONFIG['root'], 'private')
139
162
  end
140
163
 
141
- def show(sheet_file)
142
- sheet = YAML.load(IO.read(sheet_file)).to_a.first
164
+ def show(file,format=nil)
165
+ sheet = YAML.load(IO.read(file)).to_a.first
143
166
  sheet[-1] = sheet.last.join("\n") if sheet[-1].is_a?(Array)
144
- puts sheet.first + ':'
145
- puts ' ' + sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap
167
+ case format
168
+ when 'html'
169
+ puts "<h1>#{sheet.first}</h1>"
170
+ puts "<pre>#{sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap}</pre>"
171
+ else
172
+ puts sheet.first + ':'
173
+ puts ' ' + sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap
174
+ end
146
175
  end
147
176
 
148
- def rm(sheet_file)
149
- @git.remove(sheet_file)
150
- @git.commit_all("-")
177
+ def rm(file)
178
+ @git.remove(file)
179
+ @git.commit_all("#{@sheet} removed")
151
180
  rescue Git::GitExecuteError
152
- FileUtils.rm_rf(sheet_file)
181
+ FileUtils.rm_rf(file)
153
182
  end
154
183
 
155
- def add(sheet_file)
156
- unless File.exist?(sheet_file)
157
- breaker = sheet_file.rindex(File::Separator)+1
158
- path = sheet_file[0,breaker]
159
- title = @sheet.split(File::Separator).join('::')
160
- FileUtils.mkdir_p(path)
184
+ def add(file)
185
+ unless File.exist?(file)
186
+ prepare_dir(file)
187
+ title = parse_title(@sheet)
161
188
  yml = {"#{title}" => ''}.to_yaml
162
- open(sheet_file, 'w') {|f| f << yml}
189
+ open(file, 'w') {|f| f << yml}
163
190
  end
164
- edit(sheet_file)
191
+ edit(file)
165
192
  end
166
193
 
167
- def edit(sheet_file)
168
- sheet = YAML.load(IO.read(sheet_file)).to_a.first
194
+ def edit(file)
195
+ sheet = YAML.load(IO.read(file)).to_a.first
169
196
  sheet[-1] = sheet.last.gsub("\r", '')
170
197
  body, title = write_to_tempfile(*sheet), sheet.first
171
198
  if body.strip.empty?
172
- rm(sheet_file)
199
+ rm(file)
173
200
  else
174
- open(sheet_file,'w') {|f| f << {title => body}.to_yaml}
201
+ open(file,'w') {|f| f << {title => body}.to_yaml}
175
202
  @git.add
176
- @git.commit_all("-")
203
+ st = @git.status
204
+ unless st.added.empty? && st.changed.empty? && st.deleted.empty? && st.untracked.empty?
205
+ @git.commit_all(" #{@sheet} updated")
206
+ end
177
207
  end
178
208
  true
179
209
  end
180
210
 
181
211
  private
212
+ def parse_title(sheet_name)
213
+ sheet_name.split(File::Separator).join('::')
214
+ end
215
+
216
+ def prepare_dir(file)
217
+ breaker = file.rindex(File::Separator)+1
218
+ path = file[0,breaker]
219
+ FileUtils.mkdir_p(path)
220
+ end
221
+
182
222
  def editor
183
223
  ENV['VISUAL'] || ENV['EDITOR'] || "vim"
184
224
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: robin-chit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Lu