Module: Chit
Constant Summary collapse
- VERSION =
'0.0.4'
- CHITRC =
"#{ENV['HOME']}/.chitrc"
- CONFIG =
defaults.merge(YAML.load_file(CHITRC))
Instance Method Summary collapse
- #add(sheet_file) ⇒ Object
- #config(args) ⇒ Object
- #edit(sheet_file) ⇒ Object
- #editor ⇒ Object
- #handle_repos(args) ⇒ Object
- #init_chit ⇒ Object
- #list_all ⇒ Object
- #main_path ⇒ Object
- #parse_args(args) ⇒ Object
- #private_path ⇒ Object
- #repos_path(repos) ⇒ Object
- #rm(sheet_file) ⇒ Object
- #rm_repos(args) ⇒ Object
- #run(args) ⇒ Object
- #sheet_file ⇒ Object
- #show(sheet_file) ⇒ Object
- #update ⇒ Object
- #write_to_tempfile(title, body = nil) ⇒ Object
Instance Method Details
#add(sheet_file) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/chit.rb', line 172 def add(sheet_file) unless File.exist?(sheet_file) breaker = sheet_file.rindex('/')+1 path = sheet_file[0,breaker] title = @sheet.gsub(/\//,'::') FileUtils.mkdir_p(path) yml = {"#{title}" => ''}.to_yaml open(sheet_file, 'w') {|f| f << yml} end edit(sheet_file) end |
#config(args) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/chit.rb', line 115 def config(args) handle_repos(args) if args.delete('repos') open(CHITRC,'w') {|f| f << CONFIG.to_yaml} true end |
#edit(sheet_file) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/chit.rb', line 184 def edit(sheet_file) sheet = YAML.load(IO.read(sheet_file)).to_a.first sheet[-1] = sheet.last.gsub("\r", '') body, title = write_to_tempfile(*sheet), sheet.first if body.strip.empty? rm(sheet_file) else open(sheet_file,'w') {|f| f << {title => body}.to_yaml} @git.add @git.commit_all("-") end end |
#editor ⇒ Object
197 198 199 |
# File 'lib/chit.rb', line 197 def editor ENV['VISUAL'] || ENV['EDITOR'] || "vim" end |
#handle_repos(args) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/chit.rb', line 122 def handle_repos(args) rm_repos(args) and return if args.delete('rm') args.each{ |arg| expr = (arg =~ /([\w\-]+)\.([\w\-]+)\=(.+)/) puts "#{expr}" if expr CONFIG['repos'][$1] ||= {} CONFIG['repos'][$1][$2] = $3 unless File.exist?(repos_path($1)) puts "Initialize chit repository $1 to #{CONFIG['root']}/$1" Git.init(repos_path($1)) puts "Private chit initialized." end end } end |
#init_chit ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/chit.rb', line 73 def init_chit FileUtils.mkdir_p(CONFIG['root']) if CONFIG['repos']['main']['clone-from'] if File.exist?(main_path) puts "Main chit has already been initialized." else puts "Initialize main chit from #{CONFIG['repos']['main']['clone-from']} to #{CONFIG['root']}/main" Git.clone(CONFIG['repos']['main']['clone-from'], 'main', :path => CONFIG['root']) puts "Main chit initialized." end else puts "ERROR: configuration for main chit repository is missing!" return end unless File.exist?(private_path) if CONFIG['repos']['private'] && CONFIG['repos']['private']['clone-from'] puts "Initialize private chit from #{CONFIG['repos']['private']['clone-from']} to #{CONFIG['root']}/private" Git.clone(CONFIG['repos']['private']['clone-from'], 'private', :path => CONFIG['root']) puts "Private chit initialized." else puts "Initialize private chit from scratch to #{CONFIG['root']}/private" Git.init(private_path) puts "Private chit initialized." end else puts "Private chit has already been initialized." end puts "Chit init done." true end |
#list_all ⇒ Object
63 64 65 66 67 |
# File 'lib/chit.rb', line 63 def list_all files = @git.ls_files.to_a.map {|f| f[0][0..((f[0].rindex('.')||0) - 1)]} puts files.sort.join("\n") end |
#main_path ⇒ Object
150 151 152 |
# File 'lib/chit.rb', line 150 def main_path File.join(CONFIG['root'], 'main') end |
#parse_args(args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/chit.rb', line 41 def parse_args(args) init_chit and return if args.delete('--init') update and return if args.delete('--update') config(args) and return if args.delete('--config') @sheet = args.shift || 'chit' is_private = (@sheet =~ /^@(.*)/) if is_private @curr_repos = $1 @sheet = args.length > 1 ? args.shift : 'chit' end working_dir = is_private ? repos_path(@curr_repos) : main_path @git = Git.open(working_dir) @fullpath = File.join(working_dir, "#{@sheet}.yml") add(sheet_file) and return if args.delete('--add') edit(sheet_file) and return if args.delete('--edit') true end |
#private_path ⇒ Object
154 155 156 |
# File 'lib/chit.rb', line 154 def private_path File.join(CONFIG['root'], 'private') end |
#repos_path(repos) ⇒ Object
146 147 148 |
# File 'lib/chit.rb', line 146 def repos_path(repos) File.join(CONFIG['root'], repos) end |
#rm(sheet_file) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/chit.rb', line 165 def rm(sheet_file) @git.remove(sheet_file) @git.commit_all("-") rescue Git::GitExecuteError FileUtils.rm_rf(sheet_file) end |
#rm_repos(args) ⇒ Object
140 141 142 143 144 |
# File 'lib/chit.rb', line 140 def rm_repos(args) args.each{ |arg| CONFIG['repos'].delete(arg) } end |
#run(args) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/chit.rb', line 18 def run(args) unless File.exist?(main_path) && File.exist?(private_path) return unless init_chit end args = args.dup return unless parse_args(args) if %w[sheets all].include? @sheet return list_all() end unless File.exist?(sheet_file) update end unless File.exist?(sheet_file) add(sheet_file) else show(sheet_file) end end |
#sheet_file ⇒ Object
69 70 71 |
# File 'lib/chit.rb', line 69 def sheet_file @fullpath end |
#show(sheet_file) ⇒ Object
158 159 160 161 162 163 |
# File 'lib/chit.rb', line 158 def show(sheet_file) sheet = YAML.load(IO.read(sheet_file)).to_a.first sheet[-1] = sheet.last.join("\n") if sheet[-1].is_a?(Array) puts sheet.first + ':' puts ' ' + sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap end |
#update ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/chit.rb', line 105 def update if CONFIG['repos']['main']['clone-from'] g = Git.open(main_path) g.pull end rescue puts "ERROR: can not update main chit." puts $! end |
#write_to_tempfile(title, body = nil) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/chit.rb', line 201 def write_to_tempfile(title, body = nil) title = title.gsub(/\/|::/, '-') # god dammit i hate tempfile, this is so messy but i think it's # the only way. tempfile = Tempfile.new(title + '.cheat') tempfile.write(body) if body tempfile.close system "#{editor} #{tempfile.path}" tempfile.open body = tempfile.read tempfile.close body end |