icps

notes

Display

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env ruby
# encoding: utf-8
class Display
    def self.display_content(contents, idxs=nil)
        return "error" if contents.first.size != idxs.size
        max = content_transpose_max_size(contents)
        contents.map do |content|
            content.each_index.map do|i|
                space = max[i] - string_size(content[i]) + 1
                show  = content[i]
                if idxs and idxs[i] == 1
                    "#{" "*space}#{show}"
                else
                   "#{show}#{" "*space}"
                end
            end.join
        end
    end

    private

    def self.string_size(string)
        string.each_byte.map do |c|
            c < 127 ? 1 : 2
        end.compact.inject(:+)
    end

    def self.content_transpose_max_size(contents)
        contents.transpose.map do |content|
            content.map do |c|
                c ? string_size(c) : 0
            end.max
        end
    end
end
#require "/data/ruby/display/display.rb"