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.



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

#applicationObject

Returns the value of attribute application.



21
22
23
# File 'lib/gopher2000/rendering/base.rb', line 21

def application
  @application
end

#paramsObject

Returns the value of attribute params.



21
22
23
# File 'lib/gopher2000/rendering/base.rb', line 21

def params
  @params
end

#requestObject

Returns the value of attribute request.



21
22
23
# File 'lib/gopher2000/rendering/base.rb', line 21

def request
  @request
end

#resultObject

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.

Examples:

to make something double-spaced, you could call:

spacing(2)

Parameters:

  • n (Integer)

    desired line spacing



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

Parameters:

  • n (Integer)

    desired width for text output



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

Parameters:

  • string (String)

    text to add 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

Parameters:

  • str (String)

    the string to output

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

    the character to use to make the 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

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:



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

Parameters:

  • n (Integer) (defaults to: 1)

    how many lines to add



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

Parameters:

  • str (String)

    the text you want to use for your figlet

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

    name of the font. Defaults to 'big'.



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

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



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

Parameters:

  • str (String)
    • the string to output

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



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_sObject

return the output as a string

Returns:

  • rendered output



195
196
197
# File 'lib/gopher2000/rendering/base.rb', line 195

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



186
187
188
# File 'lib/gopher2000/rendering/base.rb', line 186

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