Решение на Четвърта задача от Нели Василева

Обратно към всички решения

Към профила на Нели Василева

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 7 успешни тест(а)
  • 4 неуспешни тест(а)

Код

module UI
class Node
attr_accessor :styles
attr_accessor :type
attr_accessor :output
attr_accessor :width
def initialize(type, styles = {})
@styles = styles
@type = type
@output = ''
@width = 0
end
def label(text:,**my)
my[:style] = styles[:style] if my[:style].nil?
styled = ! my[:style].nil? ? "#{text}".send(my[:style]) : "#{text}"
output << "#{my[:border]}" << styled << "#{my[:border]}"
output << "\n" if self.type == :vertical
@width = "#{text}".length if @width < "#{text}".length
end
def horizontal(**my, &block)
my[:style] = styles[:style] if my[:style].nil?
me = Node.new :horizontal, my
me.instance_eval &block
output << (my[:border].nil? ? me.output : apply_border(me))
@width = me.width if @width < me.width
output << "\n" if self.type == :vertical
end
def vertical(**my, &block)
my[:style] = styles[:style] if my[:style].nil?
me = Node.new :vertical, my
me.instance_eval &block
apply_border me if ! my[:border].nil? && me.width > 0
@width = me.width if @width < me.width
#self.type == :horizontal ? magic(me) : output << me.output
output << me.output
end
def apply_border(node)
builder = ''
node.output.each_line do |line|
builder << "#{node.styles[:border]}" << line.chomp.ljust(node.width)
builder << "#{node.styles[:border]}" << "\n"
end
node.width += node.styles[:border].length
node.output = builder
end
=begin
def magic(node)
node.output
#if there is a vertical section in a horizontal one
puts @output
rows = output.split("\n").zip(node.output.split("\n"))
builder = ''
rows.each do |first, second|
builder << (line = first.ljust(width) << second)
#@width = @width < line.length ? line.length : @width
end
output << builder
end
=end
end
class TextScreen
def self.draw
@@drawer = Node.new(:horizontal)
@@drawer.instance_eval &Proc.new
@@drawer.output
end
end
end

Лог от изпълнението

...F.F.F.F.

Failures:

  1) Command Line Toolkit handles complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb94b6b1c@/tmp/d20141126-26053-1d8y1gn/spec.rb:56> to render as "      123\n        45\n         6\n      7\n"
     # /tmp/d20141126-26053-1d8y1gn/spec.rb:56:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb94b049c@/tmp/d20141126-26053-1d8y1gn/spec.rb:99> to render as "      |||1||2|||3|       |||\n      ||      |||4|||5||||||\n      ||      ||   ||6||||||\n      ||7|                 |\n"
     # /tmp/d20141126-26053-1d8y1gn/spec.rb:99:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Command Line Toolkit propagates upcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb948a33c@/tmp/d20141126-26053-1d8y1gn/spec.rb:137> to render as "      someveryINTERESTINGget it?\n              TEXTGOES       \n                  HERE       \n"
     # /tmp/d20141126-26053-1d8y1gn/spec.rb:137:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Command Line Toolkit propagates downcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb9474dfc@/tmp/d20141126-26053-1d8y1gn/spec.rb:172> to render as "      SOMEVERYinterestingGET IT?\n              textgoes       \n                  here       \n"
     # /tmp/d20141126-26053-1d8y1gn/spec.rb:172:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.0232 seconds
11 examples, 4 failures

Failed examples:

rspec /tmp/d20141126-26053-1d8y1gn/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-1d8y1gn/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-1d8y1gn/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-1d8y1gn/spec.rb:171 # Command Line Toolkit propagates downcase to child components

История (1 версия и 0 коментара)

Нели обнови решението на 26.11.2014 16:56 (преди почти 10 години)

+module UI
+ class Node
+ attr_accessor :styles
+ attr_accessor :type
+ attr_accessor :output
+ attr_accessor :width
+
+ def initialize(type, styles = {})
+ @styles = styles
+ @type = type
+ @output = ''
+ @width = 0
+ end
+
+ def label(text:,**my)
+ my[:style] = styles[:style] if my[:style].nil?
+ styled = ! my[:style].nil? ? "#{text}".send(my[:style]) : "#{text}"
+ output << "#{my[:border]}" << styled << "#{my[:border]}"
+ output << "\n" if self.type == :vertical
+ @width = "#{text}".length if @width < "#{text}".length
+ end
+
+ def horizontal(**my, &block)
+ my[:style] = styles[:style] if my[:style].nil?
+ me = Node.new :horizontal, my
+ me.instance_eval &block
+ output << (my[:border].nil? ? me.output : apply_border(me))
+ @width = me.width if @width < me.width
+ output << "\n" if self.type == :vertical
+ end
+
+ def vertical(**my, &block)
+ my[:style] = styles[:style] if my[:style].nil?
+ me = Node.new :vertical, my
+ me.instance_eval &block
+ apply_border me if ! my[:border].nil? && me.width > 0
+ @width = me.width if @width < me.width
+ #self.type == :horizontal ? magic(me) : output << me.output
+ output << me.output
+ end
+
+ def apply_border(node)
+ builder = ''
+ node.output.each_line do |line|
+ builder << "#{node.styles[:border]}" << line.chomp.ljust(node.width)
+ builder << "#{node.styles[:border]}" << "\n"
+ end
+ node.width += node.styles[:border].length
+ node.output = builder
+ end
+=begin
+ def magic(node)
+ node.output
+
+ #if there is a vertical section in a horizontal one
+ puts @output
+ rows = output.split("\n").zip(node.output.split("\n"))
+ builder = ''
+ rows.each do |first, second|
+ builder << (line = first.ljust(width) << second)
+ #@width = @width < line.length ? line.length : @width
+ end
+ output << builder
+
+ end
+=end
+ end
+
+ class TextScreen
+ def self.draw
+ @@drawer = Node.new(:horizontal)
+ @@drawer.instance_eval &Proc.new
+ @@drawer.output
+ end
+ end
+end