nowa-chit 0.0.5 → 0.0.6
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.
- data/Manifest.txt +3 -1
- data/README.txt +15 -8
- data/lib/chit.rb +105 -38
- data/resources/chitrc +1 -0
- metadata +2 -2
data/Manifest.txt
CHANGED
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
|
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
|
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
|
73
|
+
To move or rename a sheet, use '--mv/-m' switch
|
68
74
|
|
69
|
-
$ chit
|
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/.
|
data/lib/chit.rb
CHANGED
@@ -3,15 +3,15 @@ $:.unshift File.dirname(__FILE__)
|
|
3
3
|
|
4
4
|
module Chit
|
5
5
|
extend self
|
6
|
-
VERSION = '0.0.
|
6
|
+
VERSION = '0.0.6'
|
7
7
|
|
8
8
|
defaults = {
|
9
|
-
'root' => "#{ENV['HOME']}
|
9
|
+
'root' => File.join("#{ENV['HOME']}",".chit")
|
10
10
|
}
|
11
11
|
|
12
|
-
CHITRC = "#{ENV['HOME']}
|
12
|
+
CHITRC = File.join("#{ENV['HOME']}",".chitrc")
|
13
13
|
|
14
|
-
FileUtils.cp(File.join(File.dirname(__FILE__), "
|
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
|
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
|
-
|
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 >
|
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
|
-
|
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
|
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']}
|
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(
|
165
|
-
sheet = YAML.load(IO.read(
|
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
|
-
|
168
|
-
|
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(
|
172
|
-
@git.remove(
|
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(
|
224
|
+
FileUtils.rm_rf(file)
|
176
225
|
end
|
177
226
|
|
178
|
-
def add(
|
179
|
-
unless File.exist?(
|
180
|
-
|
181
|
-
|
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(
|
232
|
+
open(file, 'w') {|f| f << yml}
|
186
233
|
end
|
187
|
-
edit(
|
234
|
+
edit(file)
|
188
235
|
end
|
189
236
|
|
190
|
-
def edit(
|
191
|
-
sheet = YAML.load(IO.read(
|
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(
|
242
|
+
rm(file)
|
196
243
|
else
|
197
|
-
|
198
|
-
|
199
|
-
|
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
|
-
|
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
|
data/resources/chitrc
CHANGED
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.
|
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
|
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.
|