Class: Gopher::Rendering::Base
- Inherits:
-
AbstractRenderer
- Object
- AbstractRenderer
- Gopher::Rendering::Base
- 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
Instance Attribute Summary collapse
-
#application ⇒ Object
Returns the value of attribute application.
-
#params ⇒ Object
Returns the value of attribute params.
-
#request ⇒ Object
Returns the value of attribute request.
-
#result ⇒ Object
Returns the value of attribute result.
-
#spacing(n) ⇒ Object
specify spacing between lines.
-
#width(n) ⇒ Object
specify the desired width of text output – defaults to 70 chars.
Instance Method Summary collapse
-
#<<(string) ⇒ Object
add a line to the output.
-
#big_header(str, under = '=') ⇒ Object
output a centered string in a box.
-
#block(text, width = @width) ⇒ Object
wrap
text
into lines no wider thanwidth
. -
#br(n = 1) ⇒ Object
Add some empty lines to the output.
- #figlet(str, font = 'big') ⇒ Object
-
#header(str, under = '=', edge = false) ⇒ Object
output a centered string with a nice underline below it, centered on the current output width.
-
#initialize(app = nil) ⇒ Base
constructor
A new instance of Base.
- #small_header(str, under = '=') ⇒ Object
-
#text(text) ⇒ Object
Adds
text
to the result @param text text to add to the result. -
#to_s ⇒ Object
return the output as a string.
-
#underline(length = @width, char = '=') ⇒ Object
output an underline.
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
#application ⇒ Object
Returns the value of attribute application.
19 20 21 |
# File 'lib/gopher2000/rendering/base.rb', line 19 def application @application end |
#params ⇒ Object
Returns the value of attribute params.
19 20 21 |
# File 'lib/gopher2000/rendering/base.rb', line 19 def params @params end |
#request ⇒ Object
Returns the value of attribute request.
19 20 21 |
# File 'lib/gopher2000/rendering/base.rb', line 19 def request @request end |
#result ⇒ Object
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.
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
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
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
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
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
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
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
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
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_s ⇒ Object
return the output as a string
191 192 193 |
# File 'lib/gopher2000/rendering/base.rb', line 191 def to_s @result end |
#underline(length = @width, char = '=') ⇒ Object
output an underline
182 183 184 |
# File 'lib/gopher2000/rendering/base.rb', line 182 def underline(length=@width, char='=') text(char * length) end |