Class: MyHelp::Control

Inherits:
Object
  • Object
show all
Defined in:
lib/my_help/my_help_controll.rb

Instance Method Summary collapse

Constructor Details

#initializeControl

Returns a new instance of Control.



3
4
5
6
7
8
# File 'lib/my_help/my_help_controll.rb', line 3

def initialize()
  @template_dir = File.expand_path("../../templates", __FILE__)
  @exe_dir = File.expand_path("../../exe", __FILE__)
  @local_help_dir = File.join(ENV['HOME'],'.my_help')
  set_help_dir_if_not_exists
end

Instance Method Details

#delete_help(file) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/my_help/my_help_controll.rb', line 101

def delete_help(file)
  file = File.join(@local_help_dir,file+'.org')
  print "Are you sure to delete "+file.blue+"?[Ynq] ".red
  case STDIN.gets.chomp
  when 'Y'
    begin
      FileUtils.rm(file,:verbose=>true)
    rescue => error
      puts error.to_s.red
    end
  when 'n', 'q' ; exit
  end
end

#disp_opts(conts) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/my_help/my_help_controll.rb', line 51

def disp_opts( conts )
  col = 0
  conts.each_pair do |key, item|
    col_length = case col
                 when 0; print item.rjust(5)+", "
                 when 1; print item.ljust(15)+": "
                 else; print item
                 end
    col += 1
  end
  print("\n")
end

#edit_help(file) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/my_help/my_help_controll.rb', line 77

def edit_help(file)
  target_help = File.join(@local_help_dir,file+'.org')
  if local_help_entries.member?(target_help)
    system "emacs #{target_help}"
  else
    puts "file #{target_help} does not exits in #{@local_help_dir}."
    puts "init #{file} first."
  end
end

#init_help(file) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/my_help/my_help_controll.rb', line 87

def init_help(file)
  if file.nil?
    puts "specify NAME".red
    exit
  end
  p target_help=File.join(@local_help_dir,file+'.org')
  if File::exists?(target_help)
    puts "File exists. delete it first to initialize it."
    exit
  end
  p template = File.join(@template_dir,'help_template.org')
  FileUtils::Verbose.cp(template,target_help)
end

#list_allObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/my_help/my_help_controll.rb', line 64

def list_all
  print "List all helps\n"
  local_help_entries.each do |file|
    file_path=File.join(@local_help_dir,file)
    title = file.split('.')[0]
    help = auto_load(file_path)
    next if help.nil?
    desc = help[:head][:cont].split("\n")[0]
    print title.rjust(10).blue
    print ": #{desc}\n".blue
  end
end

#list_help(file) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/my_help/my_help_controll.rb', line 42

def list_help(file)
  file_path=File.join(@local_help_dir,file+'.org')
  help = auto_load(file_path)
  help.each_pair do |key, conts|
    print conts[:cont] if key==:head
    disp_opts( conts[:opts] )
  end
end

#select_item(help, item) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/my_help/my_help_controll.rb', line 30

def select_item(help, item)
  o_key = ''
  help.each_pair do |key, cont|
    next if key==:license or key==:head
    if cont[:opts][:short] == item or cont[:opts][:long] == item
      o_key = key
      break
    end
  end
  o_key
end

#set_help_dir_if_not_existsObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/my_help/my_help_controll.rb', line 10

def set_help_dir_if_not_exists
  return if File::exists?(@local_help_dir)
  FileUtils.mkdir_p(@local_help_dir, :verbose=>true)
  Dir.entries(@template_dir).each{|file|
    next if file=='help_template.org'
    file_path=File.join(@local_help_dir,file)
    next if File::exists?(file_path)
    FileUtils.cp((File.join(@template_dir,file)),@local_help_dir,:verbose=>true)
  }
end

#show_item(file, item) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/my_help/my_help_controll.rb', line 21

def show_item(file, item)
  file_path=File.join(@local_help_dir,file+'.org')
  help = auto_load(file_path)
  select = select_item(help, item)
  print help[:head][:cont]
  puts '-'*5+"\n"+select.to_s.red
  print help[select][:cont]
end