Ангел обнови решението на 25.11.2014 02:24 (преди около 10 години)
+module UI
+ class Layout
+ def self.stylize(text, style)
+ case style
+ when :downcase then text.downcase
+ when :upcase then text.upcase
+ else text
+ end
+ end
+
+ def self.vertical_border(text, border, length)
+ if border
+ border.to_s + text.ljust(length) + border.to_s
+ else
+ text
+ end
+ end
+ end
+
+ class TextScreen
+ attr_reader :data
+
+ def initialize
+ @data = []
+ end
+
+ def label(text:, border: nil, style: nil)
+ @data << border.to_s + Layout.stylize(text, style) + border.to_s
+ end
+
+ def horizontal(border: nil, style: nil, &block)
+ screen = self.class.new
+ screen.instance_eval(&block)
+ @data << border.to_s + screen.data.join('') + border.to_s
+ end
+
+ def vertical(border: nil, style: nil, &block)
+ screen = self.class.new
+ screen.instance_eval(&block)
+ length = screen.data.map(&:length).max
+ @data << screen.data.map do |text|
+ Layout.vertical_border(text, border, length)
+ end.join("\n")
+ end
+
+ def self.draw(&block)
+ screen = self.new
+ screen.instance_eval(&block)
+ screen.data.join('')
+ end
+ end
+end