Class: Gopher::Rendering::Base

Inherits:
AbstractRenderer show all
Defined in:
lib/gopher2000/rendering/base.rb

Overview

base class for rendering output. this class provides methods that can be used when rendering both text and gopher menus

Direct Known Subclasses

Menu, Text

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) ⇒ Base

Returns a new instance of Base.



21
22
23
24
25
26
27
28
29
# File 'lib/gopher2000/rendering/base.rb', line 21

def initialize(app=nil)
  @application = app
  @result = ""
  @spacing = 1

  # default to 70 per RFC1436 3.9
  # "the user display string should be kept under 70 characters in length"
  @width = 70
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



19
20
21
# File 'lib/gopher2000/rendering/base.rb', line 19

def application
  @application
end

#paramsObject

Returns the value of attribute params.



19
20
21
# File 'lib/gopher2000/rendering/base.rb', line 19

def params
  @params
end

#requestObject

Returns the value of attribute request.



19
20
21
# File 'lib/gopher2000/rendering/base.rb', line 19

def request
  @request
end

#resultObject

Returns the value of attribute result.



19
20
21
# File 'lib/gopher2000/rendering/base.rb', line 19

def result
  @result
end

#spacing(n) ⇒ Object

specify spacing between lines.

Examples:

to make something double-spaced, you could call:

spacing(2)

Parameters:

  • n (Integer)

    desired line spacing



65
66
67
# File 'lib/gopher2000/rendering/base.rb', line 65

def spacing
  @spacing
end

#width(n) ⇒ Object

specify the desired width of text output – defaults to 70 chars

Parameters:

  • n (Integer)

    desired width for text output



53
54
55
# File 'lib/gopher2000/rendering/base.rb', line 53

def width
  @width
end

Instance Method Details

#<<(string) ⇒ Object

add a line to the output

Parameters:

  • string (String)

    text to add to the output



35
36
37
# File 'lib/gopher2000/rendering/base.rb', line 35

def <<(string)
  @result << string.to_s
end

#big_header(str, under = '=') ⇒ Object

output a centered string in a box

Parameters:

  • str (String)

    the string to output

  • under (Strnig) (defaults to: '=')

    the character to use to make the box



166
167
168
169
170
171
172
173
# File 'lib/gopher2000/rendering/base.rb', line 166

def big_header(str, under = '=')
  br
  underline(@width, under)
  header(str, under, true)

  # enforcing some extra space around headers for now
  br
end

#block(text, width = @width) ⇒ Object

wrap text into lines no wider than width. Hacked from ActionView

Parameters:

  • text (String)

    the text you want to wrap

  • width (Integer) (defaults to: @width)

    the desired width of the block – defaults to the current output width

See Also:



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gopher2000/rendering/base.rb', line 85

def block(text, width=@width)

  # this is a hack - recombine lines, then re-split on newlines
  # doing this because word_wrap is returning an array of lines, but
  # those lines have newlines in them where we should wrap
  lines = word_wrap(text, width).join("\n").split("\n")

  lines.each do |line|
    text line.lstrip.rstrip
  end

  self.to_s
end

#br(n = 1) ⇒ Object

Add some empty lines to the output

Parameters:

  • n (Integer) (defaults to: 1)

    how many lines to add



73
74
75
# File 'lib/gopher2000/rendering/base.rb', line 73

def br(n=1)
  self << (LINE_ENDING * n)
end

#figlet(str, font = 'big') ⇒ Object

Parameters:

  • str (String)

    the text you want to use for your figlet

  • font (String) (defaults to: 'big')

    name of the font. Defaults to ‘big’.



117
118
119
120
121
122
123
# File 'lib/gopher2000/rendering/base.rb', line 117

def figlet(str, font = 'big')
		a = Artii::Base.new(:font => font)
		a.asciify(str).split("\n").each do |l|
 text l
		end
		self.to_s
end

#header(str, under = '=', edge = false) ⇒ Object

output a centered string with a nice underline below it, centered on the current output width

Parameters:

  • str (String)
    • the string to output

  • under (String) (defaults to: '=')
    • the desired underline character

  • edge (Boolean) (defaults to: false)
    • should we output an edge? if so, there will be a

    character to the left/right edges of the string, so you can draw a box around the text



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/gopher2000/rendering/base.rb', line 135

def header(str, under = '=', edge = false)
  w = @width
  if edge
    w -= 2
  end

  tmp = str.center(w)
  if edge
    tmp = "#{under}#{tmp}#{under}"
  end

  text(tmp)
  underline(@width, under)
end

#small_header(str, under = '=') ⇒ Object

Parameters:

  • str (String)
    • the string to output

  • under (String) (defaults to: '=')
    • the desired underline character



155
156
157
158
159
# File 'lib/gopher2000/rendering/base.rb', line 155

def small_header(str, under = '=')
		str = " " + str + " "
		text(str)
		underline(str.length, under)
end

#text(text) ⇒ Object

Adds text to the result @param text text to add to the result. Adds the line,

then adds any required spacing


44
45
46
47
# File 'lib/gopher2000/rendering/base.rb', line 44

def text(text)
  self << text
  add_spacing
end

#to_sObject

return the output as a string

Returns:

  • rendered output



191
192
193
# File 'lib/gopher2000/rendering/base.rb', line 191

def to_s
  @result
end

#underline(length = @width, char = '=') ⇒ Object

output an underline

Parameters:

  • length (Integer) (defaults to: @width)

    the length of the underline – defaults to current output width.

  • char (String) (defaults to: '=')

    the character to output



182
183
184
# File 'lib/gopher2000/rendering/base.rb', line 182

def underline(length=@width, char='=')
  text(char * length)
end