class AsciiParadise::Sparky

Constants

CHECKMARK_SYMBOL
#

CHECKMARK_SYMBOL

#
DEFAULT_SEPARATOR
#

DEFAULT_SEPARATOR

#
ENCODING_UTF8
#

ENCODING_UTF8

#
PROGRESSION_BLOCKS
#

AsciiParadise::Sparky::PROGRESSION_BLOCKS

These progression blocks are useful to “build up” to something.

#
SNOWMAN
#

SNOWMAN

#

Public Class Methods

new(i) click to toggle source
#

initialize

#
# File lib/ascii_paradise/sparky/sparkline.rb, line 16
def initialize(i)
  reset
  unless i.empty?
    @original_numbers = i
    numbers  = normalize_numbers(i)
    one_step = step_height(numbers)
    @ticks = numbers.map { |n|
      index = (n / one_step).to_i
      PROGRESSION_BLOCKS[index]
    }
  end
end

Public Instance Methods

format() { |tick, original_numbers, index| ... } click to toggle source
#

format

Formats all the ticks in the given Sparkline with a given block, returns itself.

Example:

Let's say you have an Array of open and closed issues and want to format it so the open ones are red and the closed ones are green, so you can quickly see how you are doing. This can be done via the colour component of “module Colours”. An example can be found at the end of the file here.

#
# File lib/ascii_paradise/sparky/sparkline.rb, line 50
def format
  @ticks = @ticks.map.with_index { |tick, index|
    yield(tick, @original_numbers[index], index)
  }
  self # Return Sparky itself too.
end
lightgreen(i) click to toggle source
#

lightgreen

#
# File lib/ascii_paradise/sparky/sparkline.rb, line 114
def lightgreen(i)
  Colourslightgreen(i)
end
normalize_numbers(i) click to toggle source
#

normalize_numbers

Returns the normalized equivalent of a given list

normalize_numbers([3, 4, 7])
# => [0, 1, 4]

@return [Fixnum] the normalized equivalent of the given _numbers

#
# File lib/ascii_paradise/sparky/sparkline.rb, line 67
def normalize_numbers(i)
  numbers = i.map(&:to_i)
  min = numbers.min
  numbers.map { |n| n - min }
end
reset() click to toggle source
#

reset

#
# File lib/ascii_paradise/sparky/sparkline.rb, line 32
def reset
  @ticks = []
end
step_height(i) click to toggle source
#

step_height

@return [Float] the numerical “height” of a single bar “step”

#
# File lib/ascii_paradise/sparky/sparkline.rb, line 78
def step_height(i)
  min, max = i.minmax
  actual_height = max - min
  step = actual_height / steps.to_f
  if step == 0
    1
  else
    step
  end
end
steps() click to toggle source
#

steps

@return [Fixnum] the count of steps between the smallest and highest bar.

#
# File lib/ascii_paradise/sparky/sparkline.rb, line 95
def steps
  PROGRESSION_BLOCKS.size - 1
end
to_s(sep = nil) click to toggle source
#

to_s

@param sep [String, nil] separator used to join the bars of the sparkline.

@return [String] the sparkline, seperated by sep (defaults to '')

#
# File lib/ascii_paradise/sparky/sparkline.rb, line 107
def to_s(sep = nil)
  @ticks.join(sep || DEFAULT_SEPARATOR)
end