multiline 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41422946606c5f4d1aa35d8495b150be9ff55c126cda6066397162cf2496e113
4
- data.tar.gz: 19433995427155593ea5a5ee044ff452373c16b8365006d18b8e12acc716ae13
3
+ metadata.gz: 120e9b321bc8ee21dd0baef7c9795c76b18efaef94c5873f043b30b4769515c4
4
+ data.tar.gz: d8363e7df7ecbac0ae47017261dd27245562126d1c5005a9b55d4dc4e0dbe12e
5
5
  SHA512:
6
- metadata.gz: 3cdd9b623cf7b6c983efa0188ebbbd827fd0fbaeab16961c09aa40dda0f1d468c0d5a3fc184abc69f7ae02e86fabbf5a5037c435a68f8ebac37243db7b6094c9
7
- data.tar.gz: 4c8038cc860e4a2587cf773886638e0d4052e8efe1cc08cb3517720cf099ac2b6687c44c3e5f576154497ac4ccdd04d487af0c2449f3079d7e5a8a090a73fe67
6
+ metadata.gz: fedf659662908421d78f321fd4a81093dab8e1e907da6bca566dcf48391976ff285698291f47d07f35de6e3d9d79db3dd3fe003e15a43b114369ed71438693b4
7
+ data.tar.gz: 77afa7ad629335f4b502cff4157799b31af504260727371c027e8d59a77f1665242bc9fc9111d48863f2b8b53d321732a7f6c23dd32289f893d8125c5d657a1f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- multiline (0.1.0)
4
+ multiline (0.2.0)
5
5
  unicode-display_width
6
6
  unicode-emoji
7
7
 
data/lib/multiline.rb CHANGED
@@ -2,53 +2,9 @@ require "unicode/display_width"
2
2
  require "unicode/emoji"
3
3
  require "multiline/version"
4
4
 
5
+ require "multiline/string"
6
+ require "multiline/array"
7
+
5
8
  module Multiline
6
9
  class Error < StandardError; end
7
-
8
- class String
9
- attr_reader :row, :col, :buf
10
- def initialize(str = "")
11
- @buf = str.split(/\n/)
12
- @row = @buf.count
13
- @col = @buf.map{|line| Unicode::DisplayWidth.of(line) }.max
14
- @buf = @buf.map{|line| line + " " * (@col - Unicode::DisplayWidth.of(line)) }
15
- end
16
-
17
- def concat(*arguments, align: :center)
18
- arguments.each do |arg|
19
- @col += arg.col
20
- @row = [@row, arg.row].max
21
- case arg
22
- when ::String
23
- when ::Multiline::String
24
- case align
25
- when :top
26
- start_row = 0
27
- end_row = arg.row
28
- when :center
29
- start_row = (@row - arg.row) / 2
30
- end_row = (@row + arg.row) / 2
31
- when :bottom
32
- start_row = @row - arg.row
33
- end_row = @row
34
- end
35
-
36
- @buf.each_with_index do |line, index|
37
- if index >= start_row && index < end_row
38
- line.concat(arg.buf[index - start_row])
39
- else
40
- line.concat(' ' * arg.col)
41
- end
42
- end
43
- else
44
- raise ArgumentError, "Invalid string"
45
- end
46
- end
47
- self.to_s
48
- end
49
-
50
- def to_s
51
- @buf.join("\n") + "\n"
52
- end
53
- end
54
10
  end
@@ -0,0 +1,24 @@
1
+ module Multiline
2
+ class Array < ::Array
3
+ def join(sep = $,, align: :center)
4
+ buf = Multiline::String.new
5
+ self.each_with_index do |str, index|
6
+ case str
7
+ when ::String
8
+ buf.concat(Multiline::String.new(str), align: align)
9
+ when Multiline::String
10
+ buf.concat(str, align: align)
11
+ end
12
+ break if index == (self.length - 1)
13
+
14
+ case sep
15
+ when ::String
16
+ buf.concat(Multiline::String.new(sep), align: align)
17
+ when Multiline::String
18
+ buf.concat(sep, align: align)
19
+ end
20
+ end
21
+ buf
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,57 @@
1
+ module Multiline
2
+ class String
3
+ attr_reader :row, :col, :buf
4
+ def initialize(str = "")
5
+ @buf = str.split(/\n/)
6
+ @row = @buf.count || 0
7
+ @col = @buf.map{|line| Unicode::DisplayWidth.of(line) }.max || 0
8
+ @buf = @buf.map{|line| line + " " * (@col - Unicode::DisplayWidth.of(line)) }
9
+ end
10
+
11
+ def +(other)
12
+ buf = Multiline::String.new
13
+ buf.concat(self, other)
14
+ buf
15
+ end
16
+
17
+ def concat(*arguments, align: :center)
18
+ arguments.each do |arg|
19
+ @col += arg.col
20
+ @row = [@row, arg.row].max
21
+
22
+ case arg
23
+ when ::String
24
+ when ::Multiline::String
25
+ case align
26
+ when :top
27
+ start_row = 0
28
+ end_row = arg.row
29
+ when :center
30
+ start_row = (@row - arg.row) / 2
31
+ end_row = (@row + arg.row) / 2
32
+ when :bottom
33
+ start_row = @row - arg.row
34
+ end_row = @row
35
+ end
36
+
37
+ for index in 0...row do
38
+ buf[index] ||= ""
39
+ line = buf[index]
40
+ if index >= start_row && index < end_row
41
+ line.concat(arg.buf[index - start_row])
42
+ else
43
+ line.concat(' ' * arg.col)
44
+ end
45
+ end
46
+ else
47
+ raise ArgumentError, "Invalid string"
48
+ end
49
+ end
50
+ self.to_s
51
+ end
52
+
53
+ def to_s
54
+ @buf.join("\n") + "\n"
55
+ end
56
+ end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module Multiline
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multiline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshio Maki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-06 00:00:00.000000000 Z
11
+ date: 2021-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unicode-display_width
@@ -57,6 +57,8 @@ files:
57
57
  - bin/console
58
58
  - bin/setup
59
59
  - lib/multiline.rb
60
+ - lib/multiline/array.rb
61
+ - lib/multiline/string.rb
60
62
  - lib/multiline/version.rb
61
63
  - multiline.gemspec
62
64
  homepage: https://github.com/kirikak2/multiline