Skip to content

Commit

Permalink
To move or rename a sheet, use '--mv/-m' switch
Browse files Browse the repository at this point in the history
$ chit zsh_if zsh/if -m
  • Loading branch information
robin committed Jun 22, 2008
1 parent aa31da4 commit 7a17e20
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ To search cheat sheets content with 'text', use the --search/-s switch

$ chit text --search

To move or rename a sheet, use '--mv/-m' switch

$ chit zsh_if zsh/if -m

== INSTALL:

sudo gem install robin-chit -s http://gems.github.com
Expand Down
44 changes: 37 additions & 7 deletions lib/chit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,44 @@ def parse_args(args)
is_private = (@sheet =~ /^@(.*)/)
@sheet = is_private ? $1 : @sheet

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

@fullpath = File.join(working_dir, "#{@sheet}.yml")
@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

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

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

def search_content
@git.grep(@sheet).each {|file, lines|
title = title_of_file(file.split(':')[1])
Expand Down Expand Up @@ -154,10 +176,8 @@ def rm(file)

def add(file)
unless File.exist?(file)
breaker = file.rindex(File::Separator)+1
path = file[0,breaker]
title = @sheet.split(File::Separator).join('::')
FileUtils.mkdir_p(path)
prepare_dir(file)
title = parse_title(@sheet)
yml = {"#{title}" => ''}.to_yaml
open(file, 'w') {|f| f << yml}
end
Expand All @@ -182,6 +202,16 @@ def edit(file)
end

private
def parse_title(sheet_name)
sheet_name.split(File::Separator).join('::')
end

def prepare_dir(file)
breaker = file.rindex(File::Separator)+1
path = file[0,breaker]
FileUtils.mkdir_p(path)
end

def editor
ENV['VISUAL'] || ENV['EDITOR'] || "vim"
end
Expand Down

0 comments on commit 7a17e20

Please sign in to comment.