Class: AsciiParadise::Box

Inherits:
Base
  • Object
show all
Defined in:
lib/ascii_paradise/static_ascii/box.rb

Overview

AsciiParadise::Box

Constant Summary collapse

TOKEN =
#

TOKEN

#
'='
DEFAULT_TITLE =
#

DEFAULT_TITLE

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

#
'Default Title'
MAXIMUM_ALLOWED_LENGTH =
#

MAXIMUM_ALLOWED_LENGTH

#
80
DEFAULT_INPUT =
#

DEFAULT_INPUT

#
{
  'foo'   => 'bar',
  'cat'   => 'tom',
  'mouse' => 'jerry',
  'donaudampffschif' => 'piggie'
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

animation_dir?, #animation_directory?, #clear_screen, #colour_parse_this_string, #debug?, #do_not_run_already, #do_not_use_clear, #do_use_random_colour, #do_wait_for_keypress_event, e, #e, #enable_debug, #is_animated?, #menu, #project_base_dir?, #register_sigint, #remove_trailing_ansci_escape_code, #report_how_many_animated_components_exist, #return_basename_of_this_file_without_the_extension, #return_random_colour, #rev, #royalblue, run, #set_use_this_colour, #sfancy, #sfile, #show_available_components, #show_help, #simp, #slategrey, #sort_files, #static_dir?, #steelblue, #swarn, #use_colours?

Constructor Details

#initialize(i = nil, run_already = true) ⇒ Box

#

initialize

#


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

Class Method Details

.[](i) ⇒ Object

#

AsciiParadise::Box[]

#


282
283
284
# File 'lib/ascii_paradise/static_ascii/box.rb', line 282

def self.[](i)
  new(i).result
end

Instance Method Details

#add(i) ⇒ Object

#

add

#


191
192
193
# File 'lib/ascii_paradise/static_ascii/box.rb', line 191

def add(i)
  @result << i+N
end

#add_bodyObject

#

add_body

#


181
182
183
184
185
186
# 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_this_string(i, use_this_token = ' ') ⇒ Object Also known as: center

#

center_this_string

#


174
175
176
# 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

#colourize_title(use_this_colour = :slateblue) ⇒ Object

#

colourize_title

#


265
266
267
# 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_entryObject

#

determine_longest_entry

This method will determine the longest entry.

#


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# 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
#
#


198
199
200
# File 'lib/ascii_paradise/static_ascii/box.rb', line 198

def draw_footer
  add pad(token? * (width?))
end

#draw_header(optional_title = title?) ) ⇒ Object

#

draw_header (header tag)

This method willd raw the title.

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

#


209
210
211
212
213
214
215
# 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?Boolean

#

input?

#

Returns:

  • (Boolean)


153
154
155
# File 'lib/ascii_paradise/static_ascii/box.rb', line 153

def input?
  @input
end

#no_titleObject

#

no_title

#


107
108
109
# File 'lib/ascii_paradise/static_ascii/box.rb', line 107

def no_title
  @show_title = false
end

#pad(i) ⇒ Object

#

pad

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

#


93
94
95
# File 'lib/ascii_paradise/static_ascii/box.rb', line 93

def pad(i)
  "# #{i} #"
end

#resetObject

#

reset

#


73
74
75
76
77
78
79
# 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_resultObject

#

reset_result

#


84
85
86
# File 'lib/ascii_paradise/static_ascii/box.rb', line 84

def reset_result
  @result = ''.dup
end

#result?Boolean Also known as: result

#

result?

#

Returns:

  • (Boolean)


100
101
102
# File 'lib/ascii_paradise/static_ascii/box.rb', line 100

def result?
  @result
end

#runObject

#

run (run tag)

#


272
273
274
275
276
277
# 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) ⇒ Object

#

sanitize_hash

Key and value must be a string value.

#


142
143
144
145
146
147
148
# 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) ⇒ Object

#

set_input

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

#


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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) ⇒ Object

#

set_title

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

#


258
259
260
# File 'lib/ascii_paradise/static_ascii/box.rb', line 258

def set_title(i = DEFAULT_TITLE)
  @title = i
end

#show_resultObject Also known as: report

#

show_result

#


248
249
250
# File 'lib/ascii_paradise/static_ascii/box.rb', line 248

def show_result
  e result?
end

#title?Boolean

#

title?

#

Returns:

  • (Boolean)


241
242
243
# File 'lib/ascii_paradise/static_ascii/box.rb', line 241

def title?
  @title
end

#token?Boolean

#

token?

#

Returns:

  • (Boolean)


160
161
162
# File 'lib/ascii_paradise/static_ascii/box.rb', line 160

def token?
  TOKEN
end

#width?Boolean

#

width?

#

Returns:

  • (Boolean)


167
168
169
# File 'lib/ascii_paradise/static_ascii/box.rb', line 167

def width?
  @line_width
end