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
output a 'small' header, just the text with an underline.
-
#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.
23 24 25 26 27 28 29 30 31 |
# File 'lib/gopher2000/rendering/base.rb', line 23 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.
21 22 23 |
# File 'lib/gopher2000/rendering/base.rb', line 21 def application @application end |
#params ⇒ Object
Returns the value of attribute params.
21 22 23 |
# File 'lib/gopher2000/rendering/base.rb', line 21 def params @params end |
#request ⇒ Object
Returns the value of attribute request.
21 22 23 |
# File 'lib/gopher2000/rendering/base.rb', line 21 def request @request end |
#result ⇒ Object
Returns the value of attribute result.
21 22 23 |
# File 'lib/gopher2000/rendering/base.rb', line 21 def result @result end |
#spacing(n) ⇒ Object
specify spacing between lines.
67 68 69 |
# File 'lib/gopher2000/rendering/base.rb', line 67 def spacing @spacing end |
#width(n) ⇒ Object
specify the desired width of text output – defaults to 70 chars
55 56 57 |
# File 'lib/gopher2000/rendering/base.rb', line 55 def width @width end |
Instance Method Details
#<<(string) ⇒ Object
add a line to the output
37 38 39 |
# File 'lib/gopher2000/rendering/base.rb', line 37 def <<(string) @result << clean_line(string.to_s) end |
#big_header(str, under = '=') ⇒ Object
output a centered string in a box
170 171 172 173 174 175 176 177 |
# File 'lib/gopher2000/rendering/base.rb', line 170 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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/gopher2000/rendering/base.rb', line 87 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
75 76 77 |
# File 'lib/gopher2000/rendering/base.rb', line 75 def br(n=1) self << (LINE_ENDING * n) end |
#figlet(str, font = 'big') ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/gopher2000/rendering/base.rb', line 121 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
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/gopher2000/rendering/base.rb', line 139 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
output a 'small' header, just the text with an underline
159 160 161 162 163 |
# File 'lib/gopher2000/rendering/base.rb', line 159 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
46 47 48 49 |
# File 'lib/gopher2000/rendering/base.rb', line 46 def text(text) self << text.force_encoding(DEFAULT_ENCODING) add_spacing end |
#to_s ⇒ Object
return the output as a string
195 196 197 |
# File 'lib/gopher2000/rendering/base.rb', line 195 def to_s @result end |
#underline(length = @width, char = '=') ⇒ Object
output an underline
186 187 188 |
# File 'lib/gopher2000/rendering/base.rb', line 186 def underline(length=@width, char='=') text(char * length) end |