Module: Pictureframe

Defined in:
lib/pictureframe.rb,
lib/pictureframe/version.rb

Constant Summary collapse

VERSION =
"0.0.14"

Class Method Summary collapse

Class Method Details

.breakText(string, numChars) ⇒ Object



33
34
35
# File 'lib/pictureframe.rb', line 33

def self.breakText(string, numChars)
  string.scan(/#{'.{1,' + numChars.to_s}}/)
end

.expandLine(left, right, filler, width, text = "") ⇒ Object



37
38
39
40
41
42
# File 'lib/pictureframe.rb', line 37

def self.expandLine(left, right, filler, width, text = "")
  totalLength = left.length + right.length + text.length
  width = totalLength if totalLength > width
  numFiller = width - totalLength
  left + text + filler*numFiller + right
end

.frame(text, width = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pictureframe.rb', line 4

def self.frame(text, width = nil)
  text = text.to_s
  inputLocation = 3
  output = []
  lefts =  [".___", "| ._", "| | ", "| ._", "|___"]
  rights = ["___.", "_. |", " | |", "_. |", "___|"]
  insideLines = [text]

  if width
    textSpace = width - (lefts[0].length * 2)
    insideLines = textSpace < text.length ? breakText(text, textSpace) : [text]
  else
    totalLength = lefts[0].length + rights[0].length + text.length
    width = totalLength
  end

  lefts.each_with_index do |row, i|
    filler =  i == 2 ? " " : "_"
    output[i] = expandLine(lefts[i], rights[i], filler, width)
  end

  insideLines.reverse.each do |text|
    output = output.insert(inputLocation, expandLine("| | ", " | |", " ", width, text))
  end

  puts output.join("\n")
  return output
end