robin-chit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt ADDED
@@ -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
data/README.txt ADDED
@@ -0,0 +1,92 @@
1
+ = Chit
2
+
3
+ http://github.com/robin/chit
4
+
5
+ == DESCRIPTION:
6
+
7
+ Chit is A command line tool for 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 your 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 sheet. 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 get your cheat sheet:
23
+ $ chit [cheatsheet]
24
+ If it does not exist, a new one will be created and waiting for your editing. Leave it blank and quit the editor if you don't want to add a new one.
25
+
26
+ To edit a cheat sheet, use the --edit switch.
27
+ $ cheat [cheatsheet] --edit
28
+
29
+ To add a cheat sheet, use the --add switch.
30
+ $ cheat [cheatsheet] --add
31
+
32
+ During editing a cheat sheet, empty the content will get the cheat sheet removed.
33
+
34
+ A prefix '@' indicates the cheat sheet is in private mode. A private cheat sheet is kept in another repository.
35
+
36
+ To get your private cheat sheet:
37
+ $ chit @[cheatsheet]
38
+
39
+ The prefix '@' works the same for both --edit and --add.
40
+
41
+ The cheat sheet can be in a path. For example:
42
+ $ chit mysql/select
43
+ will get your the cheat sheet 'select' under mysql.
44
+
45
+ == INSTALL:
46
+
47
+ sudo gem install chit
48
+
49
+ == CONFIGURATION:
50
+
51
+ Before run 'chit', you may want to config ~/.chitrc which is a YAML file.
52
+
53
+ * root: local path to store the cheat sheet. By default, it is ~/.chit
54
+ * main:
55
+ ** 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/.
56
+ * private:
57
+ ** clone-from: where to get the private cheat sheets. If not specified, a new git repository will be init for private cheat sheets.
58
+
59
+ == REQUIREMENTS:
60
+
61
+ * rubygems
62
+ * git
63
+
64
+ == LICENSE:
65
+
66
+ This software is shared by MIT License
67
+
68
+ Copyright (c) 2008 FIX
69
+
70
+ Permission is hereby granted, free of charge, to any person obtaining
71
+ a copy of this software and associated documentation files (the
72
+ 'Software'), to deal in the Software without restriction, including
73
+ without limitation the rights to use, copy, modify, merge, publish,
74
+ distribute, sublicense, and/or sell copies of the Software, and to
75
+ permit persons to whom the Software is furnished to do so, subject to
76
+ the following conditions:
77
+
78
+ The above copyright notice and this permission notice shall be
79
+ included in all copies or substantial portions of the Software.
80
+
81
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
82
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
83
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
84
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
85
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
86
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
87
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
88
+
89
+ == BY:
90
+ Robin Lu
91
+ http://www.robinlu.com
92
+ iamawalrus[at]gmail[dot]com
data/Rakefile ADDED
@@ -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
data/bin/chit ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'chit'
4
+ Chit.run(ARGV)
data/lib/chit.rb ADDED
@@ -0,0 +1,159 @@
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.1'
7
+
8
+ defaults = {
9
+ 'root' => "#{ENV['HOME']}/.chit"
10
+ }
11
+
12
+ CONFIG = defaults.merge(YAML.load_file("#{ENV['HOME']}/.chitrc"))
13
+
14
+ def run(args)
15
+ args = args.dup
16
+
17
+ return unless parse_args(args)
18
+
19
+ unless File.exist?(sheet_file)
20
+ update
21
+ end
22
+
23
+ unless File.exist?(sheet_file)
24
+ add(sheet_file)
25
+ else
26
+ show(sheet_file)
27
+ end
28
+ end
29
+
30
+ def parse_args(args)
31
+ init_chit and return if args.delete('--init')
32
+ update and return if args.delete('--update')
33
+
34
+ @sheet = args.shift || 'chit'
35
+ is_private = (@sheet =~ /^@(.*)/)
36
+ @sheet = is_private ? $1 : @sheet
37
+ working_dir = is_private ? private_path : main_path
38
+ @fullpath = File.join(working_dir, "#{@sheet}.yml")
39
+ @git = Git.open(working_dir)
40
+
41
+ add(sheet_file) and return if args.delete('--add')
42
+ edit(sheet_file) and return if args.delete('--edit')
43
+ true
44
+ end
45
+
46
+ def sheet_file
47
+ @fullpath
48
+ end
49
+
50
+ def init_chit
51
+ FileUtils.mkdir_p(CONFIG['root'])
52
+ if CONFIG['main']['clone-from']
53
+ if File.exist?(main_path)
54
+ puts "Main chit has already been initialized."
55
+ else
56
+ puts "Initialize main chit from #{CONFIG['main']['clone-from']} to #{CONFIG['root']}/main"
57
+ Git.clone(CONFIG['main']['clone-from'], 'main', :path => CONFIG['root'])
58
+ puts "Main chit initialized."
59
+ end
60
+ else
61
+ puts "ERROR: configuration for main chit repository is missing!"
62
+ return
63
+ end
64
+
65
+ unless File.exist?(private_path)
66
+ if CONFIG['private'] && CONFIG['private']['clone-from']
67
+ puts "Initialize private chit from #{CONFIG['private']['clone-from']} to #{CONFIG['root']}/private"
68
+ Git.clone(CONFIG['private']['clone-from'], 'private', :path => CONFIG['root'])
69
+ puts "Private chit initialized."
70
+ else
71
+ puts "Initialize private chit from scratch to #{CONFIG['root']}/private"
72
+ Git.init(private_path)
73
+ puts "Private chit initialized."
74
+ end
75
+ else
76
+ puts "Private chit has already been initialized."
77
+ end
78
+ puts "Chit init done."
79
+ true
80
+ end
81
+
82
+ def update
83
+ unless File.exist?(main_path)
84
+ return unless init_chit
85
+ end
86
+ if CONFIG['main']['clone-from']
87
+ g = Git.open(main_path)
88
+ g.pull
89
+ end
90
+ rescue
91
+ puts "ERROR: can not update main chit."
92
+ puts $!
93
+ end
94
+
95
+ def main_path
96
+ File.join(CONFIG['root'], 'main')
97
+ end
98
+
99
+ def private_path
100
+ File.join(CONFIG['root'], 'private')
101
+ end
102
+
103
+ def show(sheet_file)
104
+ sheet = YAML.load(IO.read(sheet_file)).to_a.first
105
+ sheet[-1] = sheet.last.join("\n") if sheet[-1].is_a?(Array)
106
+ puts sheet.first + ':'
107
+ puts ' ' + sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap
108
+ end
109
+
110
+ def rm(sheet_file)
111
+ @git.remove(sheet_file)
112
+ @git.commit_all("-")
113
+ rescue Git::GitExecuteError
114
+ FileUtils.rm_rf(sheet_file)
115
+ end
116
+
117
+ def add(sheet_file)
118
+ unless File.exist?(sheet_file)
119
+ breaker = sheet_file.rindex('/')+1
120
+ path = sheet_file[0,breaker]
121
+ title = @sheet.gsub(/\//,'::')
122
+ FileUtils.mkdir_p(path)
123
+ yml = {"#{title}" => ''}.to_yaml
124
+ open(sheet_file, 'w') {|f| f << yml}
125
+ end
126
+ edit(sheet_file)
127
+ end
128
+
129
+ def edit(sheet_file)
130
+ sheet = YAML.load(IO.read(sheet_file)).to_a.first
131
+ sheet[-1] = sheet.last.gsub("\r", '')
132
+ body, title = write_to_tempfile(*sheet), sheet.first
133
+ if body.strip.empty?
134
+ rm(sheet_file)
135
+ else
136
+ open(sheet_file,'w') {|f| f << {title => body}.to_yaml}
137
+ @git.add
138
+ @git.commit_all("-")
139
+ end
140
+ end
141
+
142
+ def editor
143
+ ENV['VISUAL'] || ENV['EDITOR'] || "vim"
144
+ end
145
+
146
+ def write_to_tempfile(title, body = nil)
147
+ title = title.gsub(/\/|::/, '-')
148
+ # god dammit i hate tempfile, this is so messy but i think it's
149
+ # the only way.
150
+ tempfile = Tempfile.new(title + '.cheat')
151
+ tempfile.write(body) if body
152
+ tempfile.close
153
+ system "#{editor} #{tempfile.path}"
154
+ tempfile.open
155
+ body = tempfile.read
156
+ tempfile.close
157
+ body
158
+ end
159
+ end
data/lib/wrap.rb ADDED
@@ -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
data/resources/chitrc ADDED
@@ -0,0 +1,7 @@
1
+ # root:
2
+ main:
3
+ clone-from: git://github.com/robin/chitsheet.git
4
+ # push-to:
5
+ private:
6
+ # clone-from:
7
+ # push-to:
data/test/test_chit.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: robin-chit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Robin Lu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-19 00:00:00 -07:00
13
+ default_executable: chit
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"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hoe
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.5.3
32
+ version:
33
+ description: Chit is A command line tool for cheat sheet utility based on git.
34
+ email:
35
+ - iamawalrus@gmail.com
36
+ executables:
37
+ - chit
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - Manifest.txt
42
+ - README.txt
43
+ files:
44
+ - Manifest.txt
45
+ - README.txt
46
+ - Rakefile
47
+ - bin/chit
48
+ - lib/chit.rb
49
+ - lib/wrap.rb
50
+ - resources/chitrc
51
+ - test/test_chit.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/robin/chit
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --main
57
+ - README.txt
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project: chit
75
+ rubygems_version: 1.0.1
76
+ signing_key:
77
+ specification_version: 2
78
+ summary: Chit is A command line tool for cheat sheet utility based on git.
79
+ test_files:
80
+ - test/test_chit.rb