Решение на Четвърта задача от Ясен Трифонов

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

Към профила на Ясен Трифонов

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 3 успешни тест(а)
  • 8 неуспешни тест(а)

Код

module UI
class TextScreen
attr_reader :text
def initialize(style = nil)
@style = style
@text = []
end
def self.draw(&block)
screen = TextScreen.new
screen.horizontal(&block) if block_given?
screen.text.join("\n") + "\n"
end
def label(text: '', style: nil, border: '')
style = override(style)
@text.push border + case style
when :downcase then text.downcase
when :upcase then text.upcase
else text
end + border
end
def horizontal(style: nil, border: '', &block)
screen = TextScreen.new(override(style))
screen.instance_eval(&block)
new_screen = Helper.add_border(screen.text, border)
new_screen.each { |line| @text.push line }
end
def vertical(style: nil, border: '', &block)
screen = TextScreen.new(override(style))
screen.instance_eval(&block)
new_screen = Helper.add_border(screen.text, border)
@text = Helper.new_size(@text, new_screen.size)
new_screen.each_with_index { |line, id| @text[id] << line }
p @text
end
def override(style)
style = @style if style == nil
style
end
end
class Helper
class << self
def add_border(screen, border)
screen = new_size(screen, screen.size)
screen.map { |line| border + line + border }
end
def max_line_length(screen)
screen.max_by do |line|
line.size
end.to_s.size
end
def new_size(screen, screen_size) # resize not accepted
screen = screen.map do |line|
line + ' ' * (max_line_length(screen) - line.size)
end
(screen_size - screen.size).times do
screen.push ' ' * max_line_length(screen)
end
screen
end
end
end
end

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

FF["1", "2", "3"]
.["45", " 6"]
["13 ", "245", "  6"]
["13 ", "245", "  6", "7  "]
F["|something|", "|some     |", "|soommee  |"]
.["|4|||5||", "   ||6||"]
["|1|||3|       |", "|2||||4|||5||||", "   ||   ||6||||"]
["|||1|||3|       |||", "|||2||||4|||5||||||", "||   ||   ||6||||||", "||7|              |"]
FF["TEXTGOES", "    HERE"]
["someINTERESTING", "veryTEXTGOES   ", "        HERE   "]
FF["textgoes", "    here"]
["SOMEinteresting", "VERYtextgoes   ", "        here   "]
F.

Failures:

  1) Command Line Toolkit arranges components horizontally by default
     Failure/Error: expect do
       expected #<Proc:0xb9d20244@/tmp/d20141126-26053-2ssq95/spec.rb:20> to render as "      123\n"
     # /tmp/d20141126-26053-2ssq95/spec.rb:20: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 adding horizontal group does not change the behavior
     Failure/Error: expect do
       expected #<Proc:0xb9d18634@/tmp/d20141126-26053-2ssq95/spec.rb:30> to render as "      123\n"
     # /tmp/d20141126-26053-2ssq95/spec.rb:30: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 handles complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9d0f8f4@/tmp/d20141126-26053-2ssq95/spec.rb:56> to render as "      123\n        45\n         6\n      7\n"
     # /tmp/d20141126-26053-2ssq95/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)>'

  4) Command Line Toolkit handles borders correctly in complex group nestings
     Failure/Error: expect do
       expected #<Proc:0xb9cdfd5c@/tmp/d20141126-26053-2ssq95/spec.rb:99> to render as "      |||1||2|||3|       |||\n      ||      |||4|||5||||||\n      ||      ||   ||6||||||\n      ||7|                 |\n"
     # /tmp/d20141126-26053-2ssq95/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)>'

  5) Command Line Toolkit applies upcase to simple components
     Failure/Error: expect do
       expected #<Proc:0xb9cc2978@/tmp/d20141126-26053-2ssq95/spec.rb:128> to render as "      SOMEvery\n"
     # /tmp/d20141126-26053-2ssq95/spec.rb:128: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)>'

  6) Command Line Toolkit propagates upcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb9cc32ec@/tmp/d20141126-26053-2ssq95/spec.rb:137> to render as "      someveryINTERESTINGget it?\n              TEXTGOES       \n                  HERE       \n"
     # /tmp/d20141126-26053-2ssq95/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)>'

  7) Command Line Toolkit applies downcase to simple components
     Failure/Error: expect do
       expected #<Proc:0xb9c3f050@/tmp/d20141126-26053-2ssq95/spec.rb:163> to render as "      SOMEvery\n"
     # /tmp/d20141126-26053-2ssq95/spec.rb:163: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)>'

  8) Command Line Toolkit propagates downcase to child components
     Failure/Error: expect do
       expected #<Proc:0xb9c3c120@/tmp/d20141126-26053-2ssq95/spec.rb:172> to render as "      SOMEVERYinterestingGET IT?\n              textgoes       \n                  here       \n"
     # /tmp/d20141126-26053-2ssq95/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.02653 seconds
11 examples, 8 failures

Failed examples:

rspec /tmp/d20141126-26053-2ssq95/spec.rb:19 # Command Line Toolkit arranges components horizontally by default
rspec /tmp/d20141126-26053-2ssq95/spec.rb:29 # Command Line Toolkit adding horizontal group does not change the behavior
rspec /tmp/d20141126-26053-2ssq95/spec.rb:55 # Command Line Toolkit handles complex group nestings
rspec /tmp/d20141126-26053-2ssq95/spec.rb:98 # Command Line Toolkit handles borders correctly in complex group nestings
rspec /tmp/d20141126-26053-2ssq95/spec.rb:127 # Command Line Toolkit applies upcase to simple components
rspec /tmp/d20141126-26053-2ssq95/spec.rb:136 # Command Line Toolkit propagates upcase to child components
rspec /tmp/d20141126-26053-2ssq95/spec.rb:162 # Command Line Toolkit applies downcase to simple components
rspec /tmp/d20141126-26053-2ssq95/spec.rb:171 # Command Line Toolkit propagates downcase to child components

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

Ясен обнови решението на 26.11.2014 16:39 (преди почти 10 години)

+module UI
+ class TextScreen
+ attr_reader :text
+
+ def initialize(style = nil)
+ @style = style
+ @text = []
+ end
+
+ def self.draw(&block)
+ screen = TextScreen.new
+ screen.instance_eval(&block) if block_given?
+ screen.text.join("\n") + "\n"
+ end
+
+ def label(text: '', style: nil, border: '')
+ style = override(style)
+ @text.push border + case style
+ when :downcase then text.downcase
+ when :upcase then text.upcase
+ else text
+ end + border
+ end
+
+ def vertical(style: nil, border: '', &block)
+ screen = TextScreen.new(override(style))
+ screen.instance_eval(&block)
+ new_screen = Helper.add_border(screen.text, border)
+ new_screen.each { |line| @text.push line }
+ end
+
+ def horizontal(style: nil, border: '', &block)
+ screen = TextScreen.new(override(style))
+ screen.instance_eval(&block)
+ new_screen = Helper.add_border(screen.text, border)
+ @text = Helper.new_size(@text, new_screen.size)
+ new_screen.each_with_index { |line, id| @text[id] << line }
+ end
+
+ def override(style)
+ style = @style if style == nil
+ style
+ end
+ end
+
+ class Helper
+ class << self
+ def add_border(screen, border)
+ screen = new_size(screen, screen.size)
+ screen.map { |line| border + line + border }
+ end
+
+ def max_line_length(screen)
+ screen.max_by { |line| line.size }.size
+ end
+
+ def new_size(screen, screen_size) # resize not accepted
+ screen = screen.map do |line|
+ line + ' ' * (max_line_length(screen) - line.size)
+ end
+ (screen_size - screen.size).times do
+ screen.push ' ' * max_line_length(screen)
+ end
+ screen
+ end
+ end
+ end
+end

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

module UI
class TextScreen
attr_reader :text
def initialize(style = nil)
@style = style
@text = []
end
def self.draw(&block)
screen = TextScreen.new
- screen.instance_eval(&block) if block_given?
+ screen.horizontal(&block) if block_given?
screen.text.join("\n") + "\n"
end
def label(text: '', style: nil, border: '')
style = override(style)
@text.push border + case style
when :downcase then text.downcase
when :upcase then text.upcase
else text
end + border
end
- def vertical(style: nil, border: '', &block)
+ def horizontal(style: nil, border: '', &block)
screen = TextScreen.new(override(style))
screen.instance_eval(&block)
new_screen = Helper.add_border(screen.text, border)
new_screen.each { |line| @text.push line }
end
- def horizontal(style: nil, border: '', &block)
+ def vertical(style: nil, border: '', &block)
screen = TextScreen.new(override(style))
screen.instance_eval(&block)
new_screen = Helper.add_border(screen.text, border)
@text = Helper.new_size(@text, new_screen.size)
new_screen.each_with_index { |line, id| @text[id] << line }
+ p @text
end
def override(style)
style = @style if style == nil
style
end
end
class Helper
class << self
def add_border(screen, border)
screen = new_size(screen, screen.size)
screen.map { |line| border + line + border }
end
def max_line_length(screen)
- screen.max_by { |line| line.size }.size
+ screen.max_by do |line|
+ line.size
+ end.to_s.size
end
def new_size(screen, screen_size) # resize not accepted
screen = screen.map do |line|
line + ' ' * (max_line_length(screen) - line.size)
end
(screen_size - screen.size).times do
screen.push ' ' * max_line_length(screen)
end
screen
end
end
end
end