Sha256: a86103c57f0dc6032003f45fe1058fa8f0b097770817439a129106f1979c7cb0

Contents?: true

Size: 1.77 KB

Versions: 26

Compression:

Stored size: 1.77 KB

Contents

#!/usr/bin/env ruby
# encoding: utf-8

require 'isna'

# Percentage bar.
#
# items  = (1..100).to_a
# size   = (10..100).to_a
# length = size.shuffle.first
# 
# items.each do |item|
#   puts Percentage.new(100, item, length).to_bar
# end
#
class Percentage
  def initialize(product, divisor, scale = 100)
    @product        = product
    @scale          = scale - 5
    @pre_percentage = (divisor * @scale / product).to_f
    @percentage     = (@pre_percentage * 100 / @scale)
    @divisor        = divisor
  end
  def to_i
    @percentage.to_i
  end
  def to_f
    @percentage.to_f
  end

  # 0   ~  50   green
  # 50  ~  80   yellow
  # 80  ~  100  red
  def to_bar(color = true, bar = '|')
    filling = (bar * @pre_percentage.to_f)
    padding = 0
    if color
      if to_i >= 0 and to_i < 50
        filling = (bar * @pre_percentage.to_i).to_ansi.green.to_s
      end
      if to_i >= 50 and to_i < 80
        filling = (bar * @pre_percentage.to_i).to_ansi.yellow.to_s
      end
      if to_i >= 80
        filling = (bar * @pre_percentage.to_i).to_ansi.red.to_s
      end
      if filling.size > 0
        padding = 13
      end
    end
    bindings = [filling, to_f, '%']
    "[%-#{@scale + padding}s%5.1f%s]" % bindings
  end
end

behavior = ARGV[0].to_i # 0 update, 1 print

$stdout.sync = true

$break = false

trap('INT') do
  $break = true
end

at_exit do
  puts ''
end

SPLIT_REGEX = /\d+/

STDIN.each_line do |line|
  break if $break
  next unless line =~ SPLIT_REGEX
  chunks = line.scan(SPLIT_REGEX)
  current_value = chunks[0].to_f
  max_value = chunks[1].to_f
  if behavior == 0
    sleep 0.01
    print "\b" * 2000
    print Percentage.new(max_value, current_value, 50).to_bar + ' ' + line.chomp
  else
    puts Percentage.new(max_value, current_value, 50).to_bar + ' ' + line.chomp
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
ix-cli-0.0.27 bin/ix-gauge
ix-cli-0.0.26 bin/ix-gauge
ix-cli-0.0.25 bin/ix-gauge
ix-cli-0.0.24 bin/ix-gauge
ix-cli-0.0.23 bin/ix-gauge
ix-cli-0.0.22 bin/ix-gauge
ix-cli-0.0.21 bin/ix-gauge
ix-cli-0.0.20 bin/ix-gauge
ix-cli-0.0.19 bin/ix-gauge
ix-cli-0.0.18 bin/ix-gauge
ix-cli-0.0.17 bin/ix-gauge
ix-cli-0.0.16 bin/ix-gauge
ix-cli-0.0.15 bin/ix-gauge
ix-cli-0.0.14 bin/ix-gauge
ix-cli-0.0.13 bin/ix-gauge
ix-cli-0.0.12 bin/ix-gauge
ix-cli-0.0.11 bin/ix-gauge
ix-cli-0.0.10 bin/ix-gauge
ix-cli-0.0.9 bin/ix-gauge
ix-cli-0.0.7 bin/ix-gauge