Class: RSpecLive::TerminalSection

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-live/terminal.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, options = {}) ⇒ TerminalSection

Returns a new instance of TerminalSection.



44
45
46
47
48
49
# File 'lib/rspec-live/terminal.rb', line 44

def initialize(parent = nil, options = {})
  @content = ""
  @parent = parent
  options.each_pair { |key, value| instance_variable_set "@#{key}", value }
  @children = []
end

Instance Method Details

#add_section(options = {}) ⇒ Object



51
52
53
54
55
# File 'lib/rspec-live/terminal.rb', line 51

def add_section(options = {})
  new_section = TerminalSection.new(self, options)
  @children << new_section
  new_section
end

#clearObject



61
62
63
64
65
# File 'lib/rspec-live/terminal.rb', line 61

def clear
  @content = ""
  @children = []
  refresh
end

#content=(value) ⇒ Object



57
58
59
# File 'lib/rspec-live/terminal.rb', line 57

def content=(value)
  @content = value.to_s
end

#drawObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rspec-live/terminal.rb', line 78

def draw
  Curses.addstr "\n" if @display == :block
  draw_left_margin
  if @color
    Curses.attron(color_attribute @color) { draw_content }
  else
    draw_content
  end
  draw_right_margin
  @children.each(&:draw)
end

#draw_contentObject



90
91
92
93
94
95
96
97
# File 'lib/rspec-live/terminal.rb', line 90

def draw_content
  text = @content
  bullet = @bullet ? "#{@bullet} " : ""
  if @display == :block && @wrap
    text = bullet + wrap_with_margin(text, Terminal.width-1, bullet.length)
  end
  Curses.addstr text
end

#draw_left_marginObject



107
108
109
# File 'lib/rspec-live/terminal.rb', line 107

def draw_left_margin
  Curses.addstr(("=" * [0, (((Terminal.width - @content.length) / 2) - 1)].max) + " ") if @align == :center
end

#draw_right_marginObject



111
112
113
# File 'lib/rspec-live/terminal.rb', line 111

def draw_right_margin
  Curses.addstr(" " + ("=" * [0, (((Terminal.width - @content.length) / 2) - 2)].max)) if @align == :center
end

#refreshObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/rspec-live/terminal.rb', line 67

def refresh
  if @parent
    @parent.refresh
  else
    Curses.clear
    Curses.setpos 0, 0
    draw
    Curses.refresh
  end
end

#wrap(text, width) ⇒ Object



103
104
105
# File 'lib/rspec-live/terminal.rb', line 103

def wrap(text, width)
  text.scan(/\S.{0,#{width-2}}\S(?=\s|$)|\S+/).join("\n")
end

#wrap_with_margin(text, width, margin_width) ⇒ Object



99
100
101
# File 'lib/rspec-live/terminal.rb', line 99

def wrap_with_margin(text, width, margin_width)
  wrap(text, width - margin_width).split("\n").join("\n" + (" " * margin_width))
end