class TTY::Font
Constants
- FONTS_PATH
- VERSION
Public Class Methods
new(font = :standard, **options)
click to toggle source
# File lib/tty/font.rb, line 13 def initialize(font = :standard, **options) @font = font @data = load_font(FONTS_PATH.join("#{font}.yml")) @space = options.fetch(:letter_spacing) { @data['char_space'] } end
Public Instance Methods
inspect()
click to toggle source
Inspect font attributes
@api public
# File lib/tty/font.rb, line 41 def inspect vars = [ "name=#{@font.inspect}", "letter_spacing=#{@space}", "char_height=#{@data['char_height']}" ] '#<%s:0x%x %s>' % [self.class, (object_id << 1), vars.join(', ')] end
Also aliased as: to_s
write(text, **options)
click to toggle source
Write text in a font format
@param [String] text
the text to convert to font format
@api public
# File lib/tty/font.rb, line 25 def write(text, **options) result = Result.new chars = text.chars space = options.fetch(:letter_spacing) { @space } indexes = words_boundary_indexes(text) @data['char_height'].times do |line_no| result << create_line(chars, indexes, line_no, space) end result.to_s end
Private Instance Methods
create_line(chars, indexes, line_no, space)
click to toggle source
Create a new line for the output
@return [String]
@api private
# File lib/tty/font.rb, line 58 def create_line(chars, indexes, line_no, space) chars.each_with_index.map do |char, i| unless @data['chars'].key?(char) raise ArgumentError, "Font #{@font} doesn't support '#{char}' character" end @data['chars'][char][line_no] + (!indexes.include?(i) ? ' ' : '') * space end.join end
load_font(font_path)
click to toggle source
Load font if present
@param [String] font_path
The path to font location
@api private
# File lib/tty/font.rb, line 82 def load_font(font_path) ::YAML.load_file(font_path) rescue Errno::ENOENT raise ArgumentError, "Font '#{File.basename(font_path)}' not found" end
words_boundary_indexes(str)
click to toggle source
Search for all word right boundary indexes
@return [Array]
@api private
# File lib/tty/font.rb, line 72 def words_boundary_indexes(str) str.scan(/\b\w+\b/).map { |w| str.index(w) + w.size - 1 } end