Class: MyHelp::List

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

Overview

Your code goes here…

Instance Method Summary collapse

Constructor Details

#initialize(path = "", ext = ".org", layer = 1) ⇒ List

Returns a new instance of List.



8
9
10
11
12
# File 'lib/my_help/list.rb', line 8

def initialize(path = "", ext = ".org", layer = 1)
  @path = path
  @ext = ext
  @layer = layer
end

Instance Method Details

#find_near(input_item) ⇒ Object



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

def find_near(input_item)
  candidates = []
  @help_info[:items].each_pair do |item, val|
    candidates << item if item.include?(input_item)
  end
  if candidates.size == 0
    "Can't find similar item name with : #{input_item}"
  else
    contents = candidates.collect do |near_item|
      ColorizedString["item : #{near_item} \n"].colorize(:cyan) +
      @help_info[:items][near_item]
    end
    contents.join("\n")
  end
end

#list(help_options = "", level = 0) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/my_help/list.rb', line 14

def list(help_options = "", level = 0)
  name, item = help_options.split(" ")
  if item == nil && name == nil
    list_helps()
  else
    path = File.exists?(name + @ext) ? name + @ext :
      File.join(@path, name + @ext)
    list_help_with(path, name, item)
  end
end

#list_help_with(path, name, item) ⇒ Object

defaultで@path/name.@extのヘルプを読み込んで,itemを表示



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/my_help/list.rb', line 46

def list_help_with(path, name, item)
  @help_info = read_help(path)
  output = ColorizedString["my_help called with name : #{name}, item : #{item}\n"].colorize(:cyan)

  if item == nil
    @help_info[:items].each_pair do |item, val|
      item, desc = item.split(":")
      desc ||= ""
      output << "- %20s : %s\n" % [item, desc]
    end
  else
    output << find_near(item)
  end
  return output
end

#list_helpsObject



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

def list_helps()
  files = File.join(@path, "*#{@ext}")
  Dir.glob(files).inject("") do |out, file|
    #        p [out, file]
    help_info = read_help(file)
    head = help_info[:items]["head"] ?
             help_info[:items]["head"].split("\n")[0] :
             ''
    out << "%10s: %s\n" % [help_info[:name], head]
  end
end

#read_help(file) ⇒ Object



25
26
27
28
29
30
# File 'lib/my_help/list.rb', line 25

def read_help(file)
  info = {}
  info[:items] = Org2Hash.new(File.read(file)).contents
  info[:name] = File.basename(file).split(".")[0]
  return info
end