Sha256: 1cde57697b0a649cb147b1474020de9fd8bf60f5e1f1cb05fe6ab9c57b425dc1
Contents?: true
Size: 1.45 KB
Versions: 1
Compression:
Stored size: 1.45 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.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 # rubocop:enable Metrics/AbcSize, Metrics/MethodLength def table_settings { column_widths: [lyrics_column_width, nil], multiline: true } end def lyrics_column_width [ song.table_width - chords_column_width - LYRICS_CHORDS_SEPARATOR.length, max_lyrics_column_width ].min end def max_lyrics_column_width song.verses.flat_map(&:lines).flat_map(&:lyrics).map(&:length).max end def chords_column_width song.verses.flat_map(&:lines).flat_map(&:chords).map(&:length).max end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
songbook-0.4.0 | lib/songbook/render_song.rb |