class SoPerf::Row
Attributes
color[RW]
contents[RW]
raw_range[RW]
Public Class Methods
new(name, raw_range, color = ColorHelper.pick_a_color)
click to toggle source
# File lib/soperf/row.rb, line 16 def initialize(name, raw_range, color = ColorHelper.pick_a_color) @name = name.to_s @color = color @raw_range = raw_range @raw_range = (raw_range.last..raw_range.first) if raw_range.first > raw_range.last @use_color = true @raw_duration = raw_range.last-raw_range.first @pretty_duration = translate_time(@raw_duration) end
reset_color_index()
click to toggle source
# File lib/soperf/row.rb, line 26 def self.reset_color_index @@color_index = -1 end
Public Instance Methods
add_duration(str)
click to toggle source
# File lib/soperf/row.rb, line 58 def add_duration(str) look_ahead_spaces = (@pretty_duration.to_s).length+1 label = ColorHelper.color(@pretty_duration.to_s, color) if str.rindex(TICK_ON_LINE_END_CHAR) index = str.rindex(TICK_ON_LINE_END_CHAR) char = TICK_ON_LINE_END_CHAR elsif str.rindex (SINGLE_SPACE_LINE) index = str.rindex (SINGLE_SPACE_LINE) char = SINGLE_SPACE_LINE else return str end if /#{char}\s{#{@pretty_duration.length}}/.match(str[index+1..index+look_ahead_spaces]) return str.gsub /#{char}.{0,#{look_ahead_spaces}}/, "#{char} #{label}" else last_line_index = str.rindex(LINE_CHAR) return str + " #{label}" if last_line_index.nil? if str[last_line_index-look_ahead_spaces..last_line_index].index(char) str[last_line_index+1] = " #{label}" return str else return str.gsub /#{char}.{0,#{look_ahead_spaces}}/, "#{char} #{label}" end end end
name(colored = false)
click to toggle source
# File lib/soperf/row.rb, line 46 def name(colored = false) colored ? ColorHelper.color(@name, color) : @name end
point_in_range?(range, int)
click to toggle source
# File lib/soperf/row.rb, line 30 def point_in_range?(range, int) range.member?(int) ? 1 : 0 end
raw_array(duration, range=raw_range, space_vals)
click to toggle source
# File lib/soperf/row.rb, line 34 def raw_array(duration, range=raw_range, space_vals) contents = Array.new(duration+1) { 0 } starting_index = closest(space_vals, range.first) ending_index = closest(space_vals, range.last) (starting_index..ending_index).each { |i| contents[i]=1 } contents end
render(render_job)
click to toggle source
# File lib/soperf/row.rb, line 50 def render(render_job) @render_job = render_job duration = (render_job.duration*render_job.scale).to_i @use_color = render_job.use_color arr = raw_array(duration, raw_range, render_job.each_space_values) translate(arr, render_job.scaled_tick_locations) end
scaled_range(scale)
click to toggle source
# File lib/soperf/row.rb, line 42 def scaled_range(scale) (scale*raw_range.first..scale*raw_range.last) end
translate(contents, tick_locations)
click to toggle source
# File lib/soperf/row.rb, line 85 def translate(contents, tick_locations) tick_locations = tick_locations.values_at(* tick_locations.each_index.select { |i| i.even? }) str = '' contents.each_with_index do |x, i| look_ahead = (i==contents.length-1 ? nil : contents[i+1]) look_behind = (i==0 ? nil : contents[i-1]) if x==0 str<<WHITESPACE_CHAR elsif x==1 if i==0 str<<TICK_ON_LINE_START_CHAR elsif i==contents.length-1 str<<TICK_ON_LINE_END_CHAR elsif look_ahead == look_behind && look_behind == 0 str<<SINGLE_SPACE_LINE elsif look_behind == 0 str<<TICK_ON_LINE_START_CHAR elsif look_ahead == 0 str<<TICK_ON_LINE_END_CHAR else str<<TICK_CHAR end end end tick_locations.each do |i| str[i]=TICK_ON_LINE_CHAR if str[i]==TICK_CHAR str[i]=LINE_CHAR if str[i]==WHITESPACE_CHAR end str[0] =TICK_ON_LINE_CHAR if str[0]==TICK_CHAR str[0] =LINE_CHAR if str[0]==WHITESPACE_CHAR str[-1]=TICK_ON_LINE_CHAR if str[-1]==TICK_CHAR str[-1]=LINE_CHAR if str[-1]==WHITESPACE_CHAR result = ColorHelper.colorize(add_duration(str), color) @use_color ? result : result.uncolorize end