nowa-chit 0.0.4

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.
@@ -0,0 +1,7 @@
1
+ Manifest.txt
2
+ README.txt
3
+ Rakefile
4
+ bin/chit
5
+ lib/chit.rb
6
+ lib/wrap.rb
7
+ resources/chitrc
@@ -0,0 +1,119 @@
1
+ = Chit
2
+
3
+ http://github.com/robin/chit
4
+
5
+ == DESCRIPTION:
6
+
7
+ Chit is a command line cheat sheet utility based on git.
8
+
9
+ == FEATURES:
10
+
11
+ Chit was inspired by 'cheat' (http://cheat.errtheblog.com/) by Chris Wanstrath. You can use chit to access and manage cheat sheets easily.
12
+
13
+ There are several differences between ‘cheat’ and ‘chit’. By using chit, besides the wonderful features of ‘cheat’, you get:
14
+
15
+ 1. Git powered cheat sheet repository. You can specify where you get the sheets and where to share them.
16
+ 2. Your own private cheat sheets. Everybody has some project related or smoe cheat sheets which are not mean to public. You can also put them into chit
17
+ 3. Directory support. You can group cheat sheets by directory now.
18
+ 4. One less letter to type.
19
+
20
+ == SYNOPSIS:
21
+
22
+ To initialize chit repositories
23
+
24
+ $ chit --init
25
+
26
+ This will be run automatically when you run chit for the first time.
27
+
28
+ To get a cheat sheet:
29
+
30
+ $ chit [cheatsheet]
31
+
32
+ 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.
33
+
34
+ To edit a cheat sheet, use the --edit switch.
35
+
36
+ $ cheat [cheatsheet] --edit
37
+
38
+ To add a cheat sheet, use the --add switch.
39
+
40
+ $ cheat [cheatsheet] --add
41
+
42
+ During editing a cheat sheet, empty the content will get the cheat sheet removed.
43
+
44
+ A prefix '@' indicates the cheat sheet is in private mode. A private cheat sheet is kept in another repository.
45
+
46
+ To get your private cheat sheet:
47
+
48
+ $ chit @[cheatsheet]
49
+
50
+ The prefix '@' works the same for both --edit and --add.
51
+
52
+ The cheat sheet can be in a path. For example:
53
+
54
+ $ chit mysql/select
55
+
56
+ will get the cheat sheet 'select' under mysql.
57
+
58
+ To show all the cheat sheets:
59
+
60
+ $ chit [all|sheets]
61
+
62
+ To show all the private cheat sheets:
63
+
64
+ $ chit @[all|sheets]
65
+
66
+ == INSTALL:
67
+
68
+ sudo gem install robin-chit -s http://gems.github.com
69
+
70
+ chit --init
71
+
72
+ == CONFIGURATION:
73
+
74
+ Before run 'chit', you may want to config ~/.chitrc which is a YAML file.
75
+
76
+ * root: local path to store the cheat sheet. By default, it is ~/.chit
77
+ * respo: you can set not only one repository
78
+ * main:
79
+ * 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/.
80
+ * private:
81
+ * clone-from: where to get the private cheat sheets. If not specified, a new git repository will be init for private cheat sheets.
82
+
83
+ == REQUIREMENTS:
84
+
85
+ * rubygems
86
+ * git
87
+
88
+ == LICENSE:
89
+
90
+ This software is shared by MIT License
91
+
92
+ Copyright (c) 2008 Robin Lu
93
+
94
+ Permission is hereby granted, free of charge, to any person obtaining
95
+ a copy of this software and associated documentation files (the
96
+ 'Software'), to deal in the Software without restriction, including
97
+ without limitation the rights to use, copy, modify, merge, publish,
98
+ distribute, sublicense, and/or sell copies of the Software, and to
99
+ permit persons to whom the Software is furnished to do so, subject to
100
+ the following conditions:
101
+
102
+ The above copyright notice and this permission notice shall be
103
+ included in all copies or substantial portions of the Software.
104
+
105
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
106
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
107
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
108
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
109
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
110
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
111
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
112
+
113
+ == BY:
114
+
115
+ Robin Lu
116
+
117
+ http://www.robinlu.com
118
+
119
+ iamawalrus[at]gmail[dot]com
@@ -0,0 +1,13 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/chit.rb'
6
+
7
+ Hoe.new('chit', Chit::VERSION) do |p|
8
+ p.developer("Robin Lu", "iamawalrus@gmail.com")
9
+ p.extra_deps = [['schacon-git','>= 1.0']]
10
+ # p.rubyforge_name = 'chitx' # if different than lowercase project name
11
+ end
12
+
13
+ # vim: syntax=Ruby
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'chit'
4
+ Chit.run(ARGV)
@@ -0,0 +1,214 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ %w(rubygems git tempfile fileutils yaml wrap).each {|f| require f}
3
+
4
+ module Chit
5
+ extend self
6
+ VERSION = '0.0.4'
7
+
8
+ defaults = {
9
+ 'root' => "#{ENV['HOME']}/.chit"
10
+ }
11
+
12
+ CHITRC = "#{ENV['HOME']}/.chitrc"
13
+
14
+ FileUtils.cp(File.join(File.dirname(__FILE__), "../resources/chitrc"), CHITRC) unless File.exist?(CHITRC)
15
+
16
+ CONFIG = defaults.merge(YAML.load_file(CHITRC))
17
+
18
+ def run(args)
19
+ unless File.exist?(main_path) && File.exist?(private_path)
20
+ return unless init_chit
21
+ end
22
+ args = args.dup
23
+
24
+ return unless parse_args(args)
25
+
26
+ if %w[sheets all].include? @sheet
27
+ return list_all()
28
+ end
29
+
30
+ unless File.exist?(sheet_file)
31
+ update
32
+ end
33
+
34
+ unless File.exist?(sheet_file)
35
+ add(sheet_file)
36
+ else
37
+ show(sheet_file)
38
+ end
39
+ end
40
+
41
+ def parse_args(args)
42
+ init_chit and return if args.delete('--init')
43
+ update and return if args.delete('--update')
44
+ config(args) and return if args.delete('--config')
45
+
46
+ @sheet = args.shift || 'chit'
47
+ is_private = (@sheet =~ /^@(.*)/)
48
+ if is_private
49
+ @curr_repos = $1
50
+ @sheet = args.length > 1 ? args.shift : 'chit'
51
+ end
52
+
53
+ working_dir = is_private ? repos_path(@curr_repos) : main_path
54
+ @git = Git.open(working_dir)
55
+
56
+ @fullpath = File.join(working_dir, "#{@sheet}.yml")
57
+
58
+ add(sheet_file) and return if args.delete('--add')
59
+ edit(sheet_file) and return if args.delete('--edit')
60
+ true
61
+ end
62
+
63
+ def list_all
64
+ files = @git.ls_files.to_a.map {|f|
65
+ f[0][0..((f[0].rindex('.')||0) - 1)]}
66
+ puts files.sort.join("\n")
67
+ end
68
+
69
+ def sheet_file
70
+ @fullpath
71
+ end
72
+
73
+ def init_chit
74
+ FileUtils.mkdir_p(CONFIG['root'])
75
+ if CONFIG['repos']['main']['clone-from']
76
+ if File.exist?(main_path)
77
+ puts "Main chit has already been initialized."
78
+ else
79
+ puts "Initialize main chit from #{CONFIG['repos']['main']['clone-from']} to #{CONFIG['root']}/main"
80
+ Git.clone(CONFIG['repos']['main']['clone-from'], 'main', :path => CONFIG['root'])
81
+ puts "Main chit initialized."
82
+ end
83
+ else
84
+ puts "ERROR: configuration for main chit repository is missing!"
85
+ return
86
+ end
87
+
88
+ unless File.exist?(private_path)
89
+ if CONFIG['repos']['private'] && CONFIG['repos']['private']['clone-from']
90
+ puts "Initialize private chit from #{CONFIG['repos']['private']['clone-from']} to #{CONFIG['root']}/private"
91
+ Git.clone(CONFIG['repos']['private']['clone-from'], 'private', :path => CONFIG['root'])
92
+ puts "Private chit initialized."
93
+ else
94
+ puts "Initialize private chit from scratch to #{CONFIG['root']}/private"
95
+ Git.init(private_path)
96
+ puts "Private chit initialized."
97
+ end
98
+ else
99
+ puts "Private chit has already been initialized."
100
+ end
101
+ puts "Chit init done."
102
+ true
103
+ end
104
+
105
+ def update
106
+ if CONFIG['repos']['main']['clone-from']
107
+ g = Git.open(main_path)
108
+ g.pull
109
+ end
110
+ rescue
111
+ puts "ERROR: can not update main chit."
112
+ puts $!
113
+ end
114
+
115
+ def config(args)
116
+ handle_repos(args) if args.delete('repos')
117
+
118
+ open(CHITRC,'w') {|f| f << CONFIG.to_yaml}
119
+ true
120
+ end
121
+
122
+ def handle_repos(args)
123
+ rm_repos(args) and return if args.delete('rm')
124
+
125
+ args.each{ |arg|
126
+ expr = (arg =~ /([\w\-]+)\.([\w\-]+)\=(.+)/)
127
+ puts "#{expr}"
128
+ if expr
129
+ CONFIG['repos'][$1] ||= {}
130
+ CONFIG['repos'][$1][$2] = $3
131
+ unless File.exist?(repos_path($1))
132
+ puts "Initialize chit repository $1 to #{CONFIG['root']}/$1"
133
+ Git.init(repos_path($1))
134
+ puts "Private chit initialized."
135
+ end
136
+ end
137
+ }
138
+ end
139
+
140
+ def rm_repos(args)
141
+ args.each{ |arg|
142
+ CONFIG['repos'].delete(arg)
143
+ }
144
+ end
145
+
146
+ def repos_path(repos)
147
+ File.join(CONFIG['root'], repos)
148
+ end
149
+
150
+ def main_path
151
+ File.join(CONFIG['root'], 'main')
152
+ end
153
+
154
+ def private_path
155
+ File.join(CONFIG['root'], 'private')
156
+ end
157
+
158
+ def show(sheet_file)
159
+ sheet = YAML.load(IO.read(sheet_file)).to_a.first
160
+ sheet[-1] = sheet.last.join("\n") if sheet[-1].is_a?(Array)
161
+ puts sheet.first + ':'
162
+ puts ' ' + sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap
163
+ end
164
+
165
+ def rm(sheet_file)
166
+ @git.remove(sheet_file)
167
+ @git.commit_all("-")
168
+ rescue Git::GitExecuteError
169
+ FileUtils.rm_rf(sheet_file)
170
+ end
171
+
172
+ def add(sheet_file)
173
+ unless File.exist?(sheet_file)
174
+ breaker = sheet_file.rindex('/')+1
175
+ path = sheet_file[0,breaker]
176
+ title = @sheet.gsub(/\//,'::')
177
+ FileUtils.mkdir_p(path)
178
+ yml = {"#{title}" => ''}.to_yaml
179
+ open(sheet_file, 'w') {|f| f << yml}
180
+ end
181
+ edit(sheet_file)
182
+ end
183
+
184
+ def edit(sheet_file)
185
+ sheet = YAML.load(IO.read(sheet_file)).to_a.first
186
+ sheet[-1] = sheet.last.gsub("\r", '')
187
+ body, title = write_to_tempfile(*sheet), sheet.first
188
+ if body.strip.empty?
189
+ rm(sheet_file)
190
+ else
191
+ open(sheet_file,'w') {|f| f << {title => body}.to_yaml}
192
+ @git.add
193
+ @git.commit_all("-")
194
+ end
195
+ end
196
+
197
+ def editor
198
+ ENV['VISUAL'] || ENV['EDITOR'] || "vim"
199
+ end
200
+
201
+ def write_to_tempfile(title, body = nil)
202
+ title = title.gsub(/\/|::/, '-')
203
+ # god dammit i hate tempfile, this is so messy but i think it's
204
+ # the only way.
205
+ tempfile = Tempfile.new(title + '.cheat')
206
+ tempfile.write(body) if body
207
+ tempfile.close
208
+ system "#{editor} #{tempfile.path}"
209
+ tempfile.open
210
+ body = tempfile.read
211
+ tempfile.close
212
+ body
213
+ end
214
+ end
@@ -0,0 +1,42 @@
1
+ # >> Evan Weaver
2
+ # => http://blog.evanweaver.com/articles/2006/09/03/smart-plaintext-wrapping
3
+ class String
4
+ def wrap(width = 80, hanging_indent = 0, magic_lists = false)
5
+ lines = self.split(/\n/)
6
+
7
+ lines.collect! do |line|
8
+
9
+ if magic_lists
10
+ line =~ /^([\s\-\d\.\:]*\s)/
11
+ else
12
+ line =~ /^([\s]*\s)/
13
+ end
14
+
15
+ indent = $1.length + hanging_indent rescue hanging_indent
16
+
17
+ buffer = ""
18
+ first = true
19
+
20
+ while line.length > 0
21
+ first ? (i, first = 0, false) : i = indent
22
+ pos = width - i
23
+
24
+ if line.length > pos and line[0..pos] =~ /^(.+)\s/
25
+ subline = $1
26
+ else
27
+ subline = line[0..pos]
28
+ end
29
+ buffer += " " * i + subline + "\n"
30
+ line.tail!(subline.length)
31
+ end
32
+ buffer[0..-2]
33
+ end
34
+
35
+ lines.join("\n")
36
+ end
37
+
38
+ def tail!(pos)
39
+ self[0..pos] = ""
40
+ strip!
41
+ end
42
+ end
@@ -0,0 +1,8 @@
1
+ # root:
2
+ repos:
3
+ main:
4
+ clone-from: git://github.com/robin/chitsheet.git
5
+ # push-to:
6
+ private:
7
+ # clone-from:
8
+ # push-to:
File without changes
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nowa-chit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Robin Lu
8
+ - nowa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-06-19 00:00:00 -07:00
14
+ default_executable: chit
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: schacon-git
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "1.0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ version_requirement:
28
+ version_requirements: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.5.3
33
+ version:
34
+ description: Chit is A command line tool for cheat sheet utility based on git.
35
+ email:
36
+ - iamawalrus@gmail.com
37
+ executables:
38
+ - chit
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - Manifest.txt
43
+ - README.txt
44
+ files:
45
+ - Manifest.txt
46
+ - README.txt
47
+ - Rakefile
48
+ - bin/chit
49
+ - lib/chit.rb
50
+ - lib/wrap.rb
51
+ - resources/chitrc
52
+ - test/test_chit.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/nowa/chit
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --main
58
+ - README.txt
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project: chit
76
+ rubygems_version: 1.0.1
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: Chit is A command line tool for cheat sheet utility based on git.
80
+ test_files:
81
+ - test/test_chit.rb