Class: MyHelp::Control
- Inherits:
-
Object
- Object
- MyHelp::Control
- Defined in:
- lib/my_help/my_help_controll.rb
Constant Summary collapse
- WrongFileName =
Class.new(RuntimeError)
- WrongItemName =
Class.new(RuntimeError)
Instance Attribute Summary collapse
-
#editor ⇒ Object
Returns the value of attribute editor.
-
#local_help_dir ⇒ Object
Returns the value of attribute local_help_dir.
Instance Method Summary collapse
- #delete_help(file) ⇒ Object
- #edit_help(file) ⇒ Object
- #init_help(file) ⇒ Object
-
#initialize ⇒ Control
constructor
A new instance of Control.
- #list_all ⇒ Object
- #list_help(file) ⇒ Object
- #load_conf ⇒ Object
- #search_help(word) ⇒ Object
- #set_editor(editor) ⇒ Object
- #set_help_dir_if_not_exists ⇒ Object
- #show_item(file, item) ⇒ Object
Constructor Details
#initialize ⇒ 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.("../../templates", __FILE__) @exe_dir = File.("../../exe", __FILE__) @local_help_dir = File.join(ENV['HOME'],'.my_help') @editor = 'code' #'emacs' #'vim' # @mini_account = File set_help_dir_if_not_exists load_conf end |
Instance Attribute Details
#editor ⇒ Object
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_dir ⇒ Object
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
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/my_help/my_help_controll.rb', line 128 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 |
#edit_help(file) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/my_help/my_help_controll.rb', line 104 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
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/my_help/my_help_controll.rb', line 114 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_all ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/my_help/my_help_controll.rb', line 54 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/my_help/my_help_controll.rb', line 75 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_conf ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# 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 p e set_editor('code') end end |
#search_help(word) ⇒ Object
142 143 144 145 |
# File 'lib/my_help/my_help_controll.rb', line 142 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(Dir.pwd, file_name) @conf_file = File.join(@local_help_dir, file_name) conf = {editor: editor} File.open(@conf_file, 'w'){|f| YAML.dump(conf, f)} end |
#set_help_dir_if_not_exists ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/my_help/my_help_controll.rb', line 43 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
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/my_help/my_help_controll.rb', line 91 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 |