class WindowTerminal::Text
Aclass for use with the Window
object in rendering strings with Orientation
.
Attributes
orientation[R]
text[R]
y[R]
Public Class Methods
new(orientation,text,y)
click to toggle source
Initializes a Text
object.
# 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
Public Instance Methods
get_length(text=@text)
click to toggle source
Gets the length of the objects string while ignoring the character 27.
# 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)
click to toggle source
Renders a line of text.
# 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)
click to toggle source
Allows the @text instance variable to be mutable.
# File lib/accu-window.rb, line 96 def set_text(text) @text = text end
to_s()
click to toggle source
Makes to_s
return something more meaningful.
# File lib/accu-window.rb, line 203 def to_s "<ColoredText @orientation=#{@orientation.to_s}, @y=#{@y} >" end