Sha256: 198d975c0ab982189bba45334359092e70674f25359b5a9fde6bfa3e6dc9e16c

Contents?: true

Size: 1.77 KB

Versions: 9

Compression:

Stored size: 1.77 KB

Contents

# frozen-string-literal: true

module Bioshogi
  module Formatter
    class Ki2Builder
      include Builder
      include KifKi2Shared

      class << self
        def default_params
          super.merge({
              :column_style => :auto, # auto: 自動的に揃える, fixed: 指定の幅(column_width)で揃える
              :column_max   => 8,     # 最大N列
              :column_width => 12,    # column_style が fixed のときのカラム幅
              :same_suffix  => " ",  # "同角" みたいなときに "同" と "角" の間に入れる文字列
            })
        end
      end

      private

      def body_hands
        send "#{@params[:column_style]}_width_body_hands"
      end

      # 固定幅で整列
      # 値が小さいと揃わない場合がある
      def fixed_width_body_hands
        @formatter.container.hand_logs.group_by.with_index {|_, i| i / @params[:column_max] }.values.collect { |v|
          v.collect { |e|
            s = e.to_ki2(with_location: true, same_suffix: @params[:same_suffix])
            mb_ljust(s, @params[:column_width])
          }.join.strip + "\n"
        }.join
      end

      # 自動整列
      # かならず揃う
      def auto_width_body_hands
        list = @formatter.container.hand_logs.collect do |e|
          e.to_ki2(with_location: true, same_suffix: @params[:same_suffix])
        end

        list2 = list.in_groups_of(@params[:column_max])
        column_widths = list2.transpose.collect do |e|
          e.collect { |e| e.to_s.toeuc.bytesize }.max
        end

        list2 = list2.collect do |a|
          a.collect.with_index do |e, i|
            mb_ljust(e.to_s, column_widths[i])
          end
        end
        list2.collect { |e| e.join(" ").strip + "\n" }.join
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bioshogi-0.0.15 lib/bioshogi/formatter/ki2_builder.rb
bioshogi-0.0.14 lib/bioshogi/formatter/ki2_builder.rb
bioshogi-0.0.10 lib/bioshogi/formatter/ki2_builder.rb
bioshogi-0.0.9 lib/bioshogi/formatter/ki2_builder.rb
bioshogi-0.0.8 lib/bioshogi/formatter/ki2_builder.rb
bioshogi-0.0.7 lib/bioshogi/formatter/ki2_builder.rb
bioshogi-0.0.5 lib/bioshogi/formatter/ki2_builder.rb
bioshogi-0.0.4 lib/bioshogi/formatter/ki2_builder.rb
bioshogi-0.0.3 lib/bioshogi/formatter/ki2_builder.rb