Class: WindowTerminal::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/accu-window.rb

Overview

Aclass for use with the Window object in rendering strings with Orientation.

Direct Known Subclasses

ColoredText, WrappedText

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(orientation, text, y) ⇒ Text

Initializes a Text object.



86
87
88
89
90
91
92
# File 'lib/accu-window.rb', line 86

def initialize(orientation,text,y)
  raise "Orientation must be passed!" if not orientation.is_a? Orientation
  raise "String must be passed!" if not text.is_a? String
  @orientation = orientation
  @text = text
  @y = y
end

Instance Attribute Details

#orientationObject (readonly)

Returns the value of attribute orientation.



83
84
85
# File 'lib/accu-window.rb', line 83

def orientation
  @orientation
end

#textObject (readonly)

Returns the value of attribute text.



83
84
85
# File 'lib/accu-window.rb', line 83

def text
  @text
end

#yObject (readonly)

Returns the value of attribute y.



83
84
85
# File 'lib/accu-window.rb', line 83

def y
  @y
end

Instance Method Details

#get_length(text = @text) ⇒ Object

Gets the length of the objects string while ignoring the character 27.



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/accu-window.rb', line 102

def get_length(text=@text)
  #--
  length = 0
  text.each_char {|char|
    if char.ord != 27  then
      length += 1
    end
  }
  #puts length
  return length
  #++
end

#render_line(line, width, *args) ⇒ Object

Renders a line of text.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/accu-window.rb', line 116

def render_line(line,width,*args)
  #--
  line = line.dup
  if get_length() == @text.length and get_length.even? then
    @text << " "
  end
  if args[0] == nil then
    args[0] = 0
  end
  start = args[0]
  if @orientation.x == 0 then
    length = get_length()
    real_length = @text.length
    difference = (length - real_length).abs
    # Calculate centering.
    centered_width = (get_length() / 2).floor
    centered_length = ((width - 1) / 2).floor
    
    range = (centered_length-centered_width)..(centered_length+centered_width)
    # Add stretch space for colored text.
    if difference > 0 then
      char_existing = line[range.begin]
      append_string = ""
      #puts length,real_length
      (difference+(difference*2.5).ceil).times {|v|
        append_string << char_existing
      }
      #puts line[(range.begin)..(real_length -1)]
      line = line[0..range.begin] + append_string + line[(range.begin)..(width)]
      range = (range.begin+difference)..(range.end+difference)
    end
    line[range] = @text
    if get_length(line) > width then
      #puts "wut"
      # RAIG GLITCH AHX AHOGWHGOHWOGHWOGHGH
      line[get_length(line)-start-1] = ""
    end
  elsif @orientation.x == -1 then
    length = get_length()
    real_length = @text.length
    difference = (length - real_length).abs
    #puts difference
    range = (start)..(length+1)
    #puts range
    # Add stretch space for colored text.
    if difference > 0 then
      char_existing = line[range.begin]
      append_string = ""
      puts length,real_length
      (difference+(difference*2.5).ceil).times {|v|
        append_string << char_existing
      }
      #puts line[(range.begin)..(real_length -1)]
      puts "|" + line[0..range.begin-1] + "|"
      line = line[0..(range.begin-1)] + append_string + line[(range.begin)..(width)]
      #range = (range.begin+difference)..(range.end+difference)
    end
    line[range] = @text
  elsif @orientation.x == 1 then
    length = get_length()
    real_length = @text.length
    max_length = width - start - 1
    difference = (length - real_length).abs
    #puts difference
    range = (max_length - length + 1)..(max_length)
    #puts range
    # Add stretch space for colored text.
    if difference > 0 then
      char_existing = line[range.begin]
      append_string = ""
      puts length,real_length
      (difference+(difference*3).ceil).times {|v|
        append_string << char_existing
      }
      puts "|" + line[(range.end+1)..(range.end + start)] + "|"
      #puts (length/2.5).ceil
      line = line[0..(range.begin)] + append_string + line[(range.end-((length/2)-2).ceil)..(range.end + start)]
      range = (range.begin+difference*3.5)..(range.end)
    end
    line[range] = @text
  end
  return line
  #++
end

#set_text(text) ⇒ Object

Allows the @text instance variable to be mutable.



96
97
98
# File 'lib/accu-window.rb', line 96

def set_text(text)
  @text = text
end

#to_sObject

Makes to_s return something more meaningful.



203
204
205
# File 'lib/accu-window.rb', line 203

def to_s
  "<ColoredText @orientation=#{@orientation.to_s}, @y=#{@y} >"
end