class Gopher::Rendering::Base
base class for rendering output. this class provides methods that can be used when rendering both text and gopher menus
Attributes
Public Class Methods
# 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
Public Instance Methods
add a line to the output @param [String] string text to add to the output
# File lib/gopher2000/rendering/base.rb, line 37 def <<(string) @result << clean_line(string.to_s) end
output a centered string in a box @param [String] str the string to output @param [Strnig] under the character to use to make the box
# 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
wrap text
into lines no wider than width
. Hacked from ActionView @see github.com/rails/rails/blob/196407c54f0736c275d2ad4e6f8b0ac55360ad95/actionpack/lib/action_view/helpers/text_helper.rb#L217
@param [String] text the text you want to wrap @param [Integer] width the desired width of the block – defaults to the
current output width
# 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
Add some empty lines to the output @param [Integer] n how many lines to add
# File lib/gopher2000/rendering/base.rb, line 75 def br(n=1) self << (LINE_ENDING * n) end
@param [String] str the text you want to use for your figlet @param [String] font name of the font. Defaults to ‘big’.
# 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
output a centered string with a nice underline below it, centered on the current output width
@param [String] str - the string to output @param [String] under - the desired underline character @param [Boolean] edge - 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
# 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
output a ‘small’ header, just the text with an underline @param [String] str - the string to output @param [String] under - the desired underline character
# File lib/gopher2000/rendering/base.rb, line 159 def small_header(str, under = '=') str = " " + str + " " text(str) underline(str.length, under) end
Adds text
to the result @param text text to add to the result. Adds the line,
then adds any required spacing
# File lib/gopher2000/rendering/base.rb, line 46 def text(text) self << text.force_encoding(DEFAULT_ENCODING) add_spacing end
return the output as a string @return rendered output
# File lib/gopher2000/rendering/base.rb, line 195 def to_s @result end
output an underline
@param [Integer] length the length of the underline – defaults to current
output width.
@param [String] char the character to output
# File lib/gopher2000/rendering/base.rb, line 186 def underline(length=@width, char='=') text(char * length) end
Protected Instance Methods
handle lines of a single period not at the end of the transmission
RFC 1436 states: Note: Lines beginning with periods must be prepended with an extra period to ensure that the transmission is not terminated early. The client should strip extra periods at the beginning of the line.
# File lib/gopher2000/rendering/base.rb, line 220 def clean_line(line) line.match?(/^\./) ? ['.', line].join('') : line end
borrowed and modified from ActionView – wrap text at specified width returning an array of lines for now in case we want to do nifty processing with them
File actionpack/lib/action_view/helpers/text_helper.rb, line 217
# File lib/gopher2000/rendering/base.rb, line 205 def word_wrap(text, width=70*args) text.split("\n").collect do |line| line.length > width ? line.gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n").strip : line end end
Private Instance Methods
# File lib/gopher2000/rendering/base.rb, line 225 def add_spacing br(@spacing) end