Module: AsciiPaint

Defined in:
lib/ascii_paint/config.rb,
lib/ascii_paint.rb,
lib/ascii_paint/config.rb,
lib/ascii_paint/version.rb,
lib/ascii_paint/block_string.rb,
lib/ascii_paint/block_string.rb,
lib/ascii_paint/block_character.rb

Overview

An instance of class AsciiPaint::Config is used to set global configuration options for ASCII paint. Pass a block to AsciiPaint.config to access this object.

Defined Under Namespace

Classes: BlockCharacter, BlockString, CharacterNotFound, Config

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.block_paint(block_string, out_filename, conf = {}, &block) ⇒ Object

TODO: support filename arguments TODO: write tests TODO: write docs



44
45
46
47
48
# File 'lib/ascii_paint/block_string.rb', line 44

def self.block_paint(block_string, out_filename, conf = {}, &block)
  block_string = block_string.join("\n") if block_string.respond_to? :join
  ascii_art = BlockString.new(block_string).to_a
  paint(ascii_art, out_filename, conf, &block)
end

.config {|@configuration| ... } ⇒ Config

Passes an instance of Config to the given block. Used to set global configuration.

Yields:

  • (@configuration)

Returns:

  • (Config)

    the global configuration



192
193
194
195
196
# File 'lib/ascii_paint/config.rb', line 192

def self.config
  @configuration ||= Config.default
  yield(@configuration) if block_given?
  @configuration
end

.paint(ascii_art, out_filename, configuration = {}) ⇒ String

Paints a PNG based on the given ASCII art.

Example:

text = "
!!!!! @@@@@ $$$$$ %%%%% ^^^^^       @@@@@ $$$$$ %%%%% ^   ^ !!!!! 
!   ! @     $       %     ^         @   @ $   $   %   ^^  ^   !   
!!!!! @@@@@ $       %     ^         @@@@@ $$$$$   %   ^ ^ ^   !   
!   !     @ $       %     ^         @     $   $   %   ^  ^^   !   
!   ! @@@@@ $$$$$ %%%%% ^^^^^ !!!!! @     $   $ %%%%% ^   ^   !   
"

AsciiPaint.paint(text, 'out.png')

Parameters:

  • ascii_art (#to_s, Array<String>)

    multiline string, string array or filename with the ASCII art to paint

  • out_filename (#to_s)

    the name of the painted PNG file

  • conf (Hash<Symbol, value>)

    configuration settings. Keys should be the names of attributes of Config, such as :character_height.

Returns:

  • (String)

    the name of the painted PNG file



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ascii_paint.rb', line 32

def self.paint(ascii_art, out_filename, configuration = {})
  global_configuration = self.config
  local_configuration = global_configuration.dup.set_options(configuration)

  ascii_array = ascii_art_to_array(ascii_art)
  image = ascii_to_image(ascii_array, local_configuration)
  save_image(image, out_filename, local_configuration)

  if block_given?
    begin
      yield out_filename
    ensure
      FileUtils.rm(out_filename)
    end

    true
  else
    out_filename
  end
end