Sha256: 55a21a9561df448692ad2f56acfeb369c5e242a8fdc8803b0aa84578fa3c5bf4

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require 'tty-table'

module Songbook
  class RenderSong
    SEPARATOR = [nil, nil].freeze
    LYRICS_CHORDS_SEPARATOR = '   '

    attr_reader :song

    def initialize(song)
      @song = song
    end

    def call
      result = <<~RESULT
        #{song.title}\n
        #{song.details}
        #{verses_table}
      RESULT

      result.split("\n").map(&:rstrip).join("\n")
    end

    private

    # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
    def verses_table
      table = TTY::Table.new do |t|
        song.verses.each do |verse|
          t << [verse.formatted_title, nil]

          verse.lines.each do |line|
            t << [line.chords, line.lyrics]
          end

          t << SEPARATOR
        end
      end

      table.render(:basic, table_settings) do |renderer|
        renderer.border do
          center LYRICS_CHORDS_SEPARATOR
        end
      end
    end
    # rubocop:enable Metrics/AbcSize, Metrics/MethodLength

    def table_settings
      { resize: true, width: table_width }
    end

    def table_width
      chords_column_width + LYRICS_CHORDS_SEPARATOR.length + lyrics_column_width
    end

    def lyrics_column_width
      song.verses.flat_map(&:lines).flat_map(&:lyrics).map(&:length).max
    end

    def chords_column_width
      chord_column_texts =
        song.verses.flat_map(&:lines).flat_map(&:chords) + song.verses.map(&:formatted_title)

      chord_column_texts.map(&:length).max
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
songbook-0.5.1 lib/songbook/render_song.rb