Class: CastOff::Suggestion

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/cast_off/suggestion.rb

Instance Method Summary collapse

Methods included from Util

#bug, #dlog, #todo, #vlog

Constructor Details

#initialize(iseq, io) ⇒ Suggestion

Returns a new instance of Suggestion.



5
6
7
8
9
10
11
# File 'lib/cast_off/suggestion.rb', line 5

def initialize(iseq, io)
  raise ArgumentError("invalid io object") unless io.respond_to?(:puts)
  @suggestion = []
  @handler = []
  @iseq = iseq
  @io = io
end

Instance Method Details

#add_handler(&b) ⇒ Object



27
28
29
30
# File 'lib/cast_off/suggestion.rb', line 27

def add_handler(&b)
  bug() unless b.is_a?(Proc)
  @handler << b
end

#add_suggestion(msg, titles, contents, pretty = true) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cast_off/suggestion.rb', line 32

def add_suggestion(msg, titles, contents, pretty = true)
  suggestion = []
  l_msg = msg.length
  column_size = titles.size
  contents = contents.inject([]) do |ary, c|
    bug() unless c.size == column_size
    c.map! do |v|
      v.split("\n")
    end
    max = c.inject(0) do |m, v|
      l = v.size
      m > l ? m : l
    end
    max.times do |i|
      ary << c.map{|v| v[i] || ''}
    end
    ary
  end
  l_titles = contents.inject(titles.map{|t| t.length}) do |a0, a1|
    bug() unless a0.size == a1.size
    a0.zip(a1).map do |(v0, v1)|
      length = v1.length
    v0 > length ? v0 : length
    end
  end
  title = titles.zip(l_titles).map{|(t, l)| t.center(l)}.join(" | ")
  l_title = title.length
  width = l_msg > l_title ? l_msg : l_title
  if width != l_title
    bonus = width - l_title
    adjust = column_size - bonus % column_size
    width += adjust
    bonus += adjust
    bonus /= column_size
    l_titles.map!{|l| l + bonus}
    title = titles.zip(l_titles).map{|(t, l)| t.center(l)}.join(" | ")
  end
  sep = "-" * width
  suggestion << " #{sep} "
  suggestion << "|#{msg.center(width)}|"
  suggestion << "|#{sep}|"
  suggestion << "|#{title.center(width)}|"
  suggestion << "|#{sep}|"
  if pretty
    side = "|"
  else
    side = ""
  end
  contents.each do |line|
    suggestion << "#{side}#{line.zip(l_titles).map{|c, l| pretty ? c.center(l) : c }.join(" | ")}#{side}"
  end
  suggestion << " #{sep} "
  @suggestion << suggestion.join("\n")
end

#dump_at_exitObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cast_off/suggestion.rb', line 13

def dump_at_exit()
  return if @handler.empty?
  at_exit do
    @handler.each{|h| h.call}
    if @suggestion.size() > 0
      @io.puts("<<<<<<<<<< Suggestion(#{target_name()}) >>>>>>>>>>")
      @suggestion.each do |s|
        @io.puts s
        @io.puts
      end
    end
  end
end