Module: Chit

Extended by:
Chit
Included in:
Chit
Defined in:
lib/chit.rb

Constant Summary collapse

VERSION =
'0.0.6'
CHITRC =
File.join("#{ENV['HOME']}",".chitrc")
CONFIG =
defaults.merge(YAML.load_file(CHITRC))

Instance Method Summary collapse

Instance Method Details

#add(file) ⇒ Object



205
206
207
208
209
210
211
212
213
# File 'lib/chit.rb', line 205

def add(file)
  unless File.exist?(file)
    prepare_dir(file)
    title = parse_title(@sheet)
    yml = {"#{title}" => ''}.to_yaml
    open(file, 'w') {|f| f << yml}
  end
  edit(file)
end

#edit(file) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/chit.rb', line 215

def edit(file)
  sheet = YAML.load(IO.read(file)).to_a.first
  sheet[-1] = sheet.last.gsub("\r", '')
  body, title = write_to_tempfile(*sheet), sheet.first
  if body.strip.empty?
    rm(file)
  else
    open(file,'w') {|f| f << {title => body}.to_yaml}
    @git.add
    st = @git.status
    unless st.added.empty? && st.changed.empty? && st.deleted.empty? && st.untracked.empty?
      @git.commit_all(" #{@sheet} updated")        
    end
  end
  true
end

#init_chitObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/chit.rb', line 115

def init_chit
  FileUtils.mkdir_p(CONFIG['root'])
  if CONFIG['main']['clone-from']
    if File.exist?(main_path)
      puts "Main chit has already been initialized."
    else
      puts "Initialize main chit from #{CONFIG['main']['clone-from']} to #{CONFIG['root']}/main"
      Git.clone(CONFIG['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['private'] && CONFIG['private']['clone-from']
      puts "Initialize private chit from #{CONFIG['private']['clone-from']} to #{CONFIG['root']}/private"
      Git.clone(CONFIG['private']['clone-from'], 'private', :path => CONFIG['root'])
      puts "Private chit initialized."
    else
      puts "Initialize private chit from scratch to #{CONFIG['root']}/private"
      git = Git.init(private_path)
      FileUtils.touch(File.join(CONFIG['root'],'private','.gitignore'))
      git.add
      git.commit_all("init private repository")
      puts "Private chit initialized."
    end
  else
    puts "Private chit has already been initialized."
  end
  puts "Chit init done."
  true
end

#list_allObject



76
77
78
# File 'lib/chit.rb', line 76

def list_all
  puts all_sheets.sort.join("\n")
end

#main_pathObject



160
161
162
# File 'lib/chit.rb', line 160

def main_path
  File.join(CONFIG['root'], 'main')
end

#mv_to(target) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chit.rb', line 80

def mv_to(target)
  if target =~ /^@(.*)/
    target = $1
  end
  target_path = File.join(@working_dir, "#{target}.yml")
  prepare_dir(target_path)
  @git.lib.mv(sheet_file, target_path)
  sheet = YAML.load(IO.read(target_path)).to_a.first
  body = sheet[-1]
  title = parse_title(target)
  open(target_path,'w') {|f| f << {title => body}.to_yaml}
  @git.add
  @git.commit_all(" #{@sheet} moved to #{target}")
end

#pager_programObject



168
169
170
# File 'lib/chit.rb', line 168

def pager_program
  ENV['PAGER'] || "more"
end

#parse_args(args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/chit.rb', line 48

def parse_args(args)
  init_chit and return if args.delete('--init')
  update and return if args.delete('--update')
  
  @sheet = args.shift || 'chit'
  @sheet = 'chit' if @sheet == '--help'
  is_private = (@sheet =~ /^@(.*)/)
  @sheet = is_private ? $1 : @sheet

  @working_dir = is_private ? private_path : main_path
  @git = Git.open(@working_dir)

  @fullpath = File.join(@working_dir, "#{@sheet}.yml")
  
  add(sheet_file) and return if (args.delete('--add')||args.delete('-a'))
  edit(sheet_file) and return if (args.delete('--edit')||args.delete('-e'))
  search_title and return if (args.delete('--find')||args.delete('-f'))
  search_content and return if (args.delete('--search')||args.delete('-s'))
  
  if (args.delete('--mv') || args.delete('-m'))
    target = args.shift
    mv_to(target) and return if target
    puts "Target not specified!"
    return
  end
  true
end

#pipe_to_pagerObject



172
173
174
# File 'lib/chit.rb', line 172

def pipe_to_pager
    IO.popen(pager_program, "w")
end

#private_pathObject



164
165
166
# File 'lib/chit.rb', line 164

def private_path
  File.join(CONFIG['root'], 'private')
end

#rm(file) ⇒ Object



198
199
200
201
202
203
# File 'lib/chit.rb', line 198

def rm(file)
  @git.remove(file)
  @git.commit_all("#{@sheet} removed")
rescue Git::GitExecuteError
  FileUtils.rm_rf(file)
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
40
41
42
43
44
45
46
# 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 with_stdout_redirected_to(pipe_to_pager) { list_all() }
  end
  
  unless File.exist?(sheet_file)
    update
  end
  
  unless File.exist?(sheet_file)
    if args.delete('--no-add').nil? && CONFIG['add_if_not_exist']
      add(sheet_file)
    else
      puts "Error!:\n  #{@sheet} not found"
      puts "Possible sheets:"
      search_title
    end
  else
    format = 'html' if args.delete('--html')
    with_stdout_redirected_to(pipe_to_pager) { show(sheet_file, format) }
  end
end

#search_contentObject



95
96
97
98
99
100
101
102
# File 'lib/chit.rb', line 95

def search_content
  @git.grep(@sheet).each {|file, lines|
    title = title_of_file(file.split(':')[1])
    lines.each {|l|
      puts "#{title}:#{l[0]}:  #{l[1]}"
    }
  }
end

#search_titleObject



104
105
106
107
108
109
# File 'lib/chit.rb', line 104

def search_title
  reg = Regexp.compile("^#{@sheet}")
  files = all_sheets.select {|sheet| sheet =~ reg }
  puts "  " + files.sort.join("\n  ")
  true
end

#sheet_fileObject



111
112
113
# File 'lib/chit.rb', line 111

def sheet_file
  @fullpath
end

#show(file, format = nil) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/chit.rb', line 185

def show(file,format=nil)
  sheet = YAML.load(IO.read(file)).to_a.first
  sheet[-1] = sheet.last.join("\n") if sheet[-1].is_a?(Array)
  case format
  when 'html'
    puts "<h1>#{sheet.first}</h1>"
    puts "<pre>#{sheet.last.gsub("\r",'').gsub("\n", "\n  ").wrap}</pre>"
  else
    puts sheet.first + ':'
    puts '  ' + sheet.last.gsub("\r",'').gsub("\n", "\n  ").wrap      
  end
end

#updateObject



150
151
152
153
154
155
156
157
158
# File 'lib/chit.rb', line 150

def update
  if CONFIG['main']['clone-from']
    g = Git.open(main_path)
    g.pull
  end
rescue
  puts "ERROR: can not update main chit."
  puts $!
end

#with_stdout_redirected_to(io) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/chit.rb', line 176

def with_stdout_redirected_to(io)
  orig_stdout = $stdout
  $stdout = io
  yield
  $stdout.close
ensure
  $stdout = orig_stdout
end