class AsciiParadise::Box

Constants

DEFAULT_INPUT
#

DEFAULT_INPUT

#
DEFAULT_TITLE
#

DEFAULT_TITLE

The default title to use will be denoted here. This title will appear on top of the ASCII-box.

#
MAXIMUM_ALLOWED_LENGTH
#

MAXIMUM_ALLOWED_LENGTH

#
TOKEN
#

TOKEN

#

Public Class Methods

[](i) click to toggle source
#

AsciiParadise::Box[]

#
# File lib/ascii_paradise/static_ascii/box.rb, line 282
def self.[](i)
  new(i).result
end
new( i = nil, run_already = true ) { || ... } click to toggle source
#

initialize

#
# File lib/ascii_paradise/static_ascii/box.rb, line 51
def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_input(i)
  if block_given?
    yielded = yield
    if yielded == :no_title
      no_title
    elsif yielded.is_a? Hash # Handle hash input.
      if yielded.has_key? :title
        set_title(yielded.delete(:title))
      end
    end
  end
  run if run_already
end

Public Instance Methods

add(i) click to toggle source
#

add

#
# File lib/ascii_paradise/static_ascii/box.rb, line 191
def add(i)
  @result << i+N
end
add_body() click to toggle source
#

add_body

#
# File lib/ascii_paradise/static_ascii/box.rb, line 181
def add_body
  input?.each_pair {|key, value|
    string = key.ljust(@max)+' : '+value.ljust(@max)
    add pad(center(string))
  } if input?
end
center(i, use_this_token = ' ')
Alias for: center_this_string
center_this_string(i, use_this_token = ' ') click to toggle source
#

center_this_string

#
# File lib/ascii_paradise/static_ascii/box.rb, line 174
def center_this_string(i, use_this_token = ' ')
  i.center(width?, use_this_token)
end
Also aliased as: center
colourize_title(use_this_colour = :slateblue) click to toggle source
#

colourize_title

#
# File lib/ascii_paradise/static_ascii/box.rb, line 265
def colourize_title(use_this_colour = :slateblue)
  @title = ::Colours.send(use_this_colour, @title)
end
determine_longest_entry() click to toggle source
#

determine_longest_entry

This method will determine the longest entry.

#
# File lib/ascii_paradise/static_ascii/box.rb, line 222
def determine_longest_entry
  if input?
    @max = 0
    input?.each_pair {|key, value|
      if key.size > @max
        @max = key.size
      elsif value.size > @max
        @max = value.size
      end
    }
    if @max > MAXIMUM_ALLOWED_LENGTH
      @max = MAXIMUM_ALLOWED_LENGTH
    end
  end
end
draw_header(optional_title = title?) click to toggle source
#

draw_header (header tag)

This method willd raw the title.

If Konsole is available then we will also colourize the input.

#
# File lib/ascii_paradise/static_ascii/box.rb, line 209
def draw_header(optional_title = title?)
  optional_title = ''.dup if optional_title.nil?
  optional_title = " #{optional_title} " # Pad it here.
  optional_title = ''.dup unless @show_title  # Reset here when we won't show the title.
  optional_title = optional_title.center(20, token?)
  add pad( center(optional_title, token?) )
end
input?() click to toggle source
#

input?

#
# File lib/ascii_paradise/static_ascii/box.rb, line 153
def input?
  @input
end
no_title() click to toggle source
#

no_title

#
# File lib/ascii_paradise/static_ascii/box.rb, line 107
def no_title
  @show_title = false
end
pad(i) click to toggle source
#

pad

This method will pad input properly with a leading and a trailing '#'.

#
# File lib/ascii_paradise/static_ascii/box.rb, line 93
def pad(i)
  "# #{i} #"
end
report()
Alias for: show_result
reset() click to toggle source
#

reset

#
Calls superclass method AsciiParadise::Base#reset
# File lib/ascii_paradise/static_ascii/box.rb, line 73
def reset # (reset tag)
  super()
  reset_result
  @line_width = 80 # How long each line should be at maximum.
  @show_title = true # if true then we show the title in the header.
  set_title
end
reset_result() click to toggle source
#

reset_result

#
# File lib/ascii_paradise/static_ascii/box.rb, line 84
def reset_result
  @result = ''.dup
end
result()
Alias for: result?
result?() click to toggle source
#

result?

#
# File lib/ascii_paradise/static_ascii/box.rb, line 100
def result?
  @result
end
Also aliased as: result
run() click to toggle source
#

run (run tag)

#
# File lib/ascii_paradise/static_ascii/box.rb, line 272
def run
  reset_result
  draw_header title?
  add_body # Add the body of the box.
  draw_footer
end
sanitize_hash(hash) click to toggle source
#

sanitize_hash

Key and value must be a string value.

#
# File lib/ascii_paradise/static_ascii/box.rb, line 142
def sanitize_hash(hash)
  _ = {}
  hash.each_pair {|key, value|
    _[key.to_s] = value.to_s.chomp
  }
  _
end
set_input(i = DEFAULT_INPUT) click to toggle source
#

set_input

The input should be a Hash. It may however had also be an Array, in which case we will convert it.

#
# File lib/ascii_paradise/static_ascii/box.rb, line 117
def set_input(i = DEFAULT_INPUT)
  i = DEFAULT_INPUT if i.nil?
  if i.is_a? Array # Check for commandline-arguments here.
    joined = i.join(' ')
    if joined.include? '--title'
      joined =~ /--title (\w+)/
      set_title($1.to_s.dup)
      i = nil # reset here
    end 
  end
  if i.is_a? String
    i = { i => '' }
  elsif i.is_a? Array # Convert Arrays into a Hash.
    i = Hash[*i]
  end
  i = sanitize_hash(i) if i
  @input = i
  determine_longest_entry
end
set_title(i = DEFAULT_TITLE) click to toggle source
#

set_title

Set the title to use for the ASCII Box. The title will appear on top of the box.

#
# File lib/ascii_paradise/static_ascii/box.rb, line 258
def set_title(i = DEFAULT_TITLE)
  @title = i
end
show_result() click to toggle source
#

show_result

#
# File lib/ascii_paradise/static_ascii/box.rb, line 248
def show_result
  e result?
end
Also aliased as: report
title?() click to toggle source
#

title?

#
# File lib/ascii_paradise/static_ascii/box.rb, line 241
def title?
  @title
end
token?() click to toggle source
#

token?

#
# File lib/ascii_paradise/static_ascii/box.rb, line 160
def token?
  TOKEN
end
width?() click to toggle source
#

width?

#
# File lib/ascii_paradise/static_ascii/box.rb, line 167
def width?
  @line_width
end