Module: WhirledPeas::Utils::TitleFont
- Defined in:
- lib/whirled_peas/utils/title_font.rb
Constant Summary collapse
- PREFERRED_DEFAULT_FONT =
:ansishadow
- FONT_CACHE =
File.join(ENV['HOME'] || '.', '.whirled_peas.fonts')
Class Method Summary collapse
- .fonts ⇒ Object
- .load_fonts_from_cache ⇒ Object
- .load_fonts_from_figlet ⇒ Object
- .to_s(string, font_key) ⇒ Object
- .validate!(font_key) ⇒ Object
Class Method Details
.fonts ⇒ Object
21 22 23 24 25 |
# File 'lib/whirled_peas/utils/title_font.rb', line 21 def fonts return @fonts if @fonts File.exist?(FONT_CACHE) ? load_fonts_from_cache : load_fonts_from_figlet @fonts end |
.load_fonts_from_cache ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/whirled_peas/utils/title_font.rb', line 27 def load_fonts_from_cache require 'json' @fonts = {} File.open(FONT_CACHE, 'r') do |fp| JSON.parse(fp.read).each do |key, value| @fonts[key.to_sym] = value end end @fonts end |
.load_fonts_from_figlet ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/whirled_peas/utils/title_font.rb', line 38 def load_fonts_from_figlet require 'ruby_figlet' require 'json' @fonts = {} RubyFiglet::Figlet.available.strip.split("\n").each do |font| # Load the font to be sure it exists on the system next unless available?(font) string_key = font.downcase.gsub(/\W/, '') string_key = "_#{string_key}" if string_key[0] !~ /[a-z]/ string_key.gsub!(/\d+$/, '') next if @fonts.key?(string_key) @fonts[string_key.to_sym] = font end @fonts[:default] ||= @fonts[PREFERRED_DEFAULT_FONT] || @fonts.values.first File.open(FONT_CACHE, 'w') { |fp| fp.write(JSON.generate(@fonts)) } @fonts end |
.to_s(string, font_key) ⇒ Object
16 17 18 19 |
# File 'lib/whirled_peas/utils/title_font.rb', line 16 def to_s(string, font_key) require 'ruby_figlet' RubyFiglet::Figlet.new(string, fonts[font_key]).to_s end |
.validate!(font_key) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/whirled_peas/utils/title_font.rb', line 8 def validate!(font_key) return if font_key.nil? return font_key if fonts.key?(font_key) allowed = fonts.keys.map(&:inspect).sort = "Unrecognized font: '#{font_key.inspect}', expecting one of #{allowed.join(', ')}" raise ArgumentError, end |