Class: SlackProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_progress_bar.rb,
lib/slack_progress_bar/cli.rb,
lib/slack_progress_bar/config.rb,
lib/slack_progress_bar/version.rb,
lib/slack_progress_bar/generator.rb

Defined Under Namespace

Classes: CLI, Config, Generator

Constant Summary collapse

VERSION =
Gem::Version.new("0.1.0")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(counts: {}, total: counts.values.sum, size: 14, rounded: true) ⇒ SlackProgressBar

Create a new progress bar.

The “counts” argument is a hash of string or symbol keys and integer values. Only keys present in either config.letters or config.aliases will be rendered in the resulting progress bar. If not provided, it’s assumed that no progress has been made.

The “total” argument is optional, defaulting to the sum of all provided counts. If a total is provided that is greater than the sum of the counts, the last configured color will be used to fill in the remainder of the bar. If a total is provided that is less than the sum of the counts provided, the greater value will be used to render the progress bar.

The “size” argument specifies how many emoji will be used to render the progress bar. The default is 14 because the default progress bar has rounded ends that each represent only one stripe. The remaining 12 emoji each represent four stripes, for a total of 50 stripes, making each stripe count for 2% of the total. Any size may be given but each stripe may represent an unusual, floating point percentage of the total.

The “rounded” argument accepts a boolean for whether the progress bar should have rounded ends. This is a matter of personal preference and will affect the whitespace on either side of the progress bar since the rounded end emoji images are mostly negative space.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/slack_progress_bar.rb', line 42

def initialize(counts: {}, total: counts.values.sum, size: 14, rounded: true)
  @counts = {}
  @total = total
  @size = size
  @rounded = rounded

  counts.each do |key, count|
    letter =
      case key
      when *config.letters then key
      when *config.aliases.keys then config.aliases.fetch(key)
      end
    @counts[letter] ||= 0
    @counts[letter] += count
  end
end

Instance Attribute Details

#countsObject (readonly)

Returns the value of attribute counts.



16
17
18
# File 'lib/slack_progress_bar.rb', line 16

def counts
  @counts
end

#sizeObject (readonly)

Returns the value of attribute size.



16
17
18
# File 'lib/slack_progress_bar.rb', line 16

def size
  @size
end

#totalObject (readonly)

Returns the value of attribute total.



16
17
18
# File 'lib/slack_progress_bar.rb', line 16

def total
  @total
end

Class Method Details

.configObject



7
8
9
# File 'lib/slack_progress_bar.rb', line 7

def self.config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



11
12
13
14
# File 'lib/slack_progress_bar.rb', line 11

def self.configure
  yield config
  config.validate!
end

Instance Method Details

#to_sObject



59
60
61
# File 'lib/slack_progress_bar.rb', line 59

def to_s
  emoji
end