Class: Multiline::String
- Inherits:
-
Object
- Object
- Multiline::String
- Defined in:
- lib/multiline/string.rb
Instance Attribute Summary collapse
-
#buf ⇒ Object
readonly
Returns the value of attribute buf.
-
#col ⇒ Object
readonly
Returns the value of attribute col.
-
#row ⇒ Object
readonly
Returns the value of attribute row.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #concat(*arguments, align: :center) ⇒ Object
-
#initialize(str = "", buf_row = nil) ⇒ String
constructor
A new instance of String.
- #to_s ⇒ Object
Constructor Details
#initialize(str = "", buf_row = nil) ⇒ String
Returns a new instance of String.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/multiline/string.rb', line 4 def initialize(str = "", buf_row = nil) @buf = str.split(/\n/) @row = [@buf.length, buf_row].compact.max rescue binding.irb @col = @buf.map{|line| Unicode::DisplayWidth.of(line, Multiline.config.display_width_ambiguous, Multiline.config.display_width_overwrite) }.max || 0 @buf = @buf.map{|line| line + " " * (@col - Unicode::DisplayWidth.of(line, Multiline.config.display_width_ambiguous, Multiline.config.display_width_overwrite)) } end |
Instance Attribute Details
#buf ⇒ Object (readonly)
Returns the value of attribute buf.
3 4 5 |
# File 'lib/multiline/string.rb', line 3 def buf @buf end |
#col ⇒ Object (readonly)
Returns the value of attribute col.
3 4 5 |
# File 'lib/multiline/string.rb', line 3 def col @col end |
#row ⇒ Object (readonly)
Returns the value of attribute row.
3 4 5 |
# File 'lib/multiline/string.rb', line 3 def row @row end |
Instance Method Details
#+(other) ⇒ Object
15 16 17 18 19 |
# File 'lib/multiline/string.rb', line 15 def +(other) buf = Multiline::String.new buf.concat(self, other) buf end |
#concat(*arguments, align: :center) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 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 |
# File 'lib/multiline/string.rb', line 21 def concat(*arguments, align: :center) arguments.each do |arg| org_col = @col org_row = @row @col += arg.col @row = [@row, arg.row].max case arg when ::String when ::Multiline::String case align when :top start_row = 0 end_row = arg.row when :center start_row = (@row - arg.row) / 2 end_row = (@row + arg.row) / 2 when :bottom start_row = @row - arg.row end_row = @row end for index in 0...row do buf[index] ||= " " * org_col line = buf[index] if index >= start_row && index < end_row line.concat(arg.buf[index - start_row]) else line.concat(' ' * arg.col) end end else raise ArgumentError, "Invalid string" end end self.to_s end |
#to_s ⇒ Object
59 60 61 |
# File 'lib/multiline/string.rb', line 59 def to_s @buf.join("\n") + "\n" end |