class Songbook::RenderSong

Constants

LYRICS_CHORDS_SEPARATOR
SEPARATOR
TABLE_WIDTH

Attributes

song[R]

Public Class Methods

new(song) click to toggle source
# File lib/songbook/render_song.rb, line 13
def initialize(song)
  @song = song
end

Public Instance Methods

call() click to toggle source
# File lib/songbook/render_song.rb, line 17
    def call
      result = <<~RESULT
        #{song.title}\n
        #{song.details}
        #{verses_table}
      RESULT

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

Private Instance Methods

chords_column_width() click to toggle source
# File lib/songbook/render_song.rb, line 66
def chords_column_width
  song.verses.flat_map(&:lines).flat_map(&:chords).map(&:length).max
end
lyrics_column_width() click to toggle source
# File lib/songbook/render_song.rb, line 55
def lyrics_column_width
  [
    TABLE_WIDTH - chords_column_width - LYRICS_CHORDS_SEPARATOR.length,
    max_lyrics_column_width
  ].min
end
max_lyrics_column_width() click to toggle source
# File lib/songbook/render_song.rb, line 62
def max_lyrics_column_width
  song.verses.flat_map(&:lines).flat_map(&:lyrics).map(&:length).max
end
table_settings() click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/MethodLength

# File lib/songbook/render_song.rb, line 51
def table_settings
  { column_widths: [lyrics_column_width, nil], multiline: true }
end
verses_table() click to toggle source

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

# File lib/songbook/render_song.rb, line 30
def verses_table
  table = TTY::Table.new do |t|
    song.verses.each do |verse|
      t << ["[#{verse.title}]", nil]

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

      t << SEPARATOR
    end
  end

  table.render(:basic, table_settings) do |renderer|
    renderer.border do
      center LYRICS_CHORDS_SEPARATOR
    end
  end
end