Class: TTY::Font

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

Defined Under Namespace

Classes: Result

Constant Summary collapse

FONTS_PATH =
::Pathname.new(::File.join(__dir__, "fonts"))
VERSION =
"0.5.0"

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Font.



13
14
15
16
17
# 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

Instance Method Details

#inspectObject Also known as: to_s

Inspect font attributes



41
42
43
44
45
46
47
48
# 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

#write(text, **options) ⇒ Object

Write text in a font format

Parameters:

  • text (String)

    the text to convert to font format



25
26
27
28
29
30
31
32
33
34
35
36
# 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