Class: AsciiParadise::Sparky

Inherits:
Object
  • Object
show all
Defined in:
lib/ascii_paradise/sparky/cli.rb,
lib/ascii_paradise/sparky/constants.rb,
lib/ascii_paradise/sparky/sparkline.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

ENCODING_UTF8 =
#

ENCODING_UTF8

#
'utf-8'
DEFAULT_SEPARATOR =
#

DEFAULT_SEPARATOR

#
''
PROGRESSION_BLOCKS =
#

AsciiParadise::Sparky::PROGRESSION_BLOCKS

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

#
%w(
         
)
CHECKMARK_SYMBOL =
#

CHECKMARK_SYMBOL

#
AsciiParadise::CHECKMARK_SYMBOL
SNOWMAN =
#

SNOWMAN

#
AsciiParadise::SNOWMAN

Instance Method Summary collapse

Constructor Details

#initialize(i) ⇒ Sparky

#

initialize

#


16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#formatObject

#

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.

#


50
51
52
53
54
55
# 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) ⇒ Object

#

lightgreen

#


114
115
116
# File 'lib/ascii_paradise/sparky/sparkline.rb', line 114

def lightgreen(i)
  Colours.lightgreen(i)
end

#normalize_numbers(i) ⇒ Fixnum

#

normalize_numbers

Returns the normalized equivalent of a given list

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

Returns:

  • (Fixnum)

    the normalized equivalent of the given _numbers



67
68
69
70
71
# 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

#resetObject

#

reset

#


32
33
34
# File 'lib/ascii_paradise/sparky/sparkline.rb', line 32

def reset
  @ticks = []
end

#step_height(i) ⇒ Float

#

step_height

#

Returns:

  • (Float)

    the numerical “height” of a single bar “step”



78
79
80
81
82
83
84
85
86
87
# 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

#stepsFixnum

#

steps

highest bar.

#

Returns:

  • (Fixnum)

    the count of steps between the smallest and



95
96
97
# File 'lib/ascii_paradise/sparky/sparkline.rb', line 95

def steps
  PROGRESSION_BLOCKS.size - 1
end

#to_s(sep = nil) ⇒ String

#

to_s

sparkline.

#

Parameters:

  • sep (String, nil) (defaults to: nil)

    separator used to join the bars of the

Returns:

  • (String)

    the sparkline, seperated by sep (defaults to ”)



107
108
109
# File 'lib/ascii_paradise/sparky/sparkline.rb', line 107

def to_s(sep = nil)
  @ticks.join(sep || DEFAULT_SEPARATOR)
end