Class: TTY::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/font.rb,
lib/tty/font/version.rb

Constant Summary collapse

FONTS_PATH =
Pathname.new(File.join(__dir__, 'font'))
VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(font = :standard, **options) ⇒ Font

Returns a new instance of Font.



12
13
14
15
16
# File 'lib/tty/font.rb', line 12

def initialize(font = :standard, **options)
  @font  = font
  @data  = load_font(FONTS_PATH.join("#{font}.yml"))
  @space = options.fetch(:letter_spacing) { 0 }
end

Instance Method Details

#inspectObject Also known as: to_s

Inspect font attributes



40
41
42
43
44
45
46
47
# File 'lib/tty/font.rb', line 40

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

#write(text, **options) ⇒ Object

Write text in a font format

Parameters:

  • text (String)

    the text to convert to font format



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tty/font.rb', line 24

def write(text, **options)
  output = []
  chars = text.chars
  space = options.fetch(:letter_spacing) { @space }
  indexes = words_boundary_indexes(text)

  @data['char_height'].times do |line|
    output << add_line(chars, indexes, line, space)
  end

  output.join("\n")
end