Class: MyHelp::Control

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

Constant Summary collapse

WrongFileName =
Class.new(RuntimeError)
WrongItemName =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeControl

Returns a new instance of Control.



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

def initialize()
  # for configuration setups
  # see https://stackoverflow.com/questions/6233124/where-to-place-access-config-file-in-gem

  @template_dir = File.expand_path("../../templates", __FILE__)
  @exe_dir = File.expand_path("../../exe", __FILE__)
  @local_help_dir = File.join(ENV['HOME'],'.my_help')
  @editor = ENV['EDITOR'] || 'emacs' #'code', 'emacs' #'vim' #default editor
  # @mini_account = File
  set_help_dir_if_not_exists
  load_conf
end

Instance Attribute Details

#editorObject

Returns the value of attribute editor.



7
8
9
# File 'lib/my_help/my_help_controll.rb', line 7

def editor
  @editor
end

#local_help_dirObject

Returns the value of attribute local_help_dir.



7
8
9
# File 'lib/my_help/my_help_controll.rb', line 7

def local_help_dir
  @local_help_dir
end

Instance Method Details

#delete_help(file) ⇒ Object

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


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/my_help/my_help_controll.rb', line 144

def delete_help(file)
  file = File.join(@local_help_dir,file+'.org')
  if File.exist?(file) == true
    print "Are you sure to delete "+file.blue+"?[Yn] ".red
    case STDIN.gets.chomp
    when 'Y'
      begin
        FileUtils.rm(file,:verbose=>true)
        return 0
      rescue => error
        puts error.to_s.red
        return 1
      end
    when 'n', 'q' ; return 0
    end
  else
    print file + " is a non-existent file"
  end
end

#edit_help(file) ⇒ Object



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

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

#init_help(file) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/my_help/my_help_controll.rb', line 115

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/my_help/my_help_controll.rb', line 55

def list_all
  output = "\nList 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?
    begin
      desc = help[:head][:cont].split("\n")[0]
    rescue => e
      puts e
      puts "No head in #{file_path}".red
      next
    end
    output << title.rjust(10)
    output << ": #{desc}\n"
  end
  output
end

#list_help(file) ⇒ Object



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

def list_help(file)
  output = ''
  file_path=File.join(@local_help_dir,file+'.org')
  begin
    help = auto_load(file_path)
  rescue => e
    raise WrongFileName, "No help named '#{file}' in the directory '#{local_help_dir}'."
  end
  help.each_pair do |key, conts|
    output << conts[:cont] if key==:head
    output << disp_opts( conts[:opts] )
  end
  output
end

#load_confObject



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

def load_conf
  file_name = '.my_help_conf.yml'
  # @conf_file = File.join(Dir.pwd, file_name)
  @conf_file = File.join(@local_help_dir, file_name)
  begin
    conf = YAML.load_file(@conf_file)
    @editor = conf[:editor]
  rescue => e
    puts e.to_s.red
    puts 'make .my_help_conf.yml'.green
    set_editor(@editor)
  end
end

#search_help(word) ⇒ Object



164
165
166
167
# File 'lib/my_help/my_help_controll.rb', line 164

def search_help(word)
  p find_char = word
  system "ls #{@local_help_dir} | grep #{find_char}"
end

#set_editor(editor) ⇒ Object



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

def set_editor(editor)
  @editor = editor
  file_name = '.my_help_conf.yml'
  @conf_file = File.join(@local_help_dir, file_name)
  conf = {editor: editor}
  File.open(@conf_file, 'w'){|f| YAML.dump(conf, f)}
  puts "set editor '#{@editor}'"
end

#set_help_dir_if_not_existsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/my_help/my_help_controll.rb', line 44

def set_help_dir_if_not_exists
  return if File::exist?(@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



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/my_help/my_help_controll.rb', line 92

def show_item(file, item)
  output = ''
  file_path=File.join(@local_help_dir,file+'.org')
  help = auto_load(file_path)
  select = select_item(help, item)
  output << help[:head][:cont]
  unless select then
    raise WrongItemName, "No item entry: #{item}"
  end
  output << '-'*5+"\n"+select.to_s.green+"\n"
  output << help[select][:cont]
end