From 9da8aeaa64ff21daa1b39e3493134d42d67eb71a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20F=C3=B6hring?= Date: Tue, 21 Jan 2014 19:11:54 +0100 Subject: [PATCH] =?UTF-8?q?=E2=96=81=E2=96=82=E2=96=83=E2=96=85=E2=96=82?= =?UTF-8?q?=E2=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 17 +++++++++++++++++ .ruby-gemset | 1 + .ruby-version | 1 + Gemfile | 6 ++++++ LICENSE.txt | 22 ++++++++++++++++++++++ README.md | 29 +++++++++++++++++++++++++++++ Rakefile | 9 +++++++++ bin/sparkr | 11 +++++++++++ lib/sparkr.rb | 9 +++++++++ lib/sparkr/cli.rb | 7 +++++++ lib/sparkr/sparkline.rb | 24 ++++++++++++++++++++++++ lib/sparkr/version.rb | 3 +++ sparkr.gemspec | 24 ++++++++++++++++++++++++ test/sparkr_test.rb | 21 +++++++++++++++++++++ test/test_helper.rb | 13 +++++++++++++ 15 files changed, 197 insertions(+) create mode 100644 .gitignore create mode 100644 .ruby-gemset create mode 100644 .ruby-version create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100755 bin/sparkr create mode 100644 lib/sparkr.rb create mode 100644 lib/sparkr/cli.rb create mode 100644 lib/sparkr/sparkline.rb create mode 100644 lib/sparkr/version.rb create mode 100644 sparkr.gemspec create mode 100644 test/sparkr_test.rb create mode 100644 test/test_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 0000000..9f8ad06 --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +Sparkr diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..f33eb12 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +ruby-2.0.0 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..8b22a71 --- /dev/null +++ b/Gemfile @@ -0,0 +1,6 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in homecoming.gemspec +gemspec + +gem 'simplecov', :require => false, :group => :test diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..9a079e5 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2014 René Föhring + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..37d7269 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Sparkr + +TODO: Write a gem description + +## Installation + +Add this line to your application's Gemfile: + + gem 'Sparkr' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install Sparkr + +## Usage + +TODO: Write usage instructions here + +## Contributing + +1. Fork it ( http://github.com//Sparkr/fork ) +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..fd8ff23 --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require "bundler/gem_tasks" + +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.pattern = "test/**/*_test.rb" +end + +task :default => :test \ No newline at end of file diff --git a/bin/sparkr b/bin/sparkr new file mode 100755 index 0000000..807f5c5 --- /dev/null +++ b/bin/sparkr @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby + +require 'bundler' +begin + Bundler.require +rescue Bundler::GemfileNotFound + require 'Sparkr' +end + +require 'Sparkr' +Sparkr::CLI.run(*ARGV) diff --git a/lib/sparkr.rb b/lib/sparkr.rb new file mode 100644 index 0000000..e88b6a9 --- /dev/null +++ b/lib/sparkr.rb @@ -0,0 +1,9 @@ +require "sparkr/cli" +require "sparkr/sparkline" +require "sparkr/version" + +module Sparkr + def self.sparkline(*args) + Sparkline.new(*args).to_s + end +end diff --git a/lib/sparkr/cli.rb b/lib/sparkr/cli.rb new file mode 100644 index 0000000..83274a1 --- /dev/null +++ b/lib/sparkr/cli.rb @@ -0,0 +1,7 @@ +module Sparkr + class CLI + def self.run(*args) + puts Sparkline.new(args.map(&:to_f)).to_s + end + end +end diff --git a/lib/sparkr/sparkline.rb b/lib/sparkr/sparkline.rb new file mode 100644 index 0000000..2da5dac --- /dev/null +++ b/lib/sparkr/sparkline.rb @@ -0,0 +1,24 @@ +require 'pry' + +module Sparkr + class Sparkline + TICKS = %w(▁ ▂ ▃ ▄ ▅ ▆ ▇ █) + + def initialize(numbers) + @step_count = TICKS.size + @step_height = (numbers.max - numbers.min) / @step_count + 1 + + indexes = numbers.map do |n| + ((n - numbers.min) / @step_height).to_i + end + @ticks = indexes.map do |index| + TICKS[index] + end + binding.pry + end + + def to_s + @ticks.join('') + end + end +end diff --git a/lib/sparkr/version.rb b/lib/sparkr/version.rb new file mode 100644 index 0000000..a0781dd --- /dev/null +++ b/lib/sparkr/version.rb @@ -0,0 +1,3 @@ +module Sparkr + VERSION = "0.0.1" +end diff --git a/sparkr.gemspec b/sparkr.gemspec new file mode 100644 index 0000000..853a46d --- /dev/null +++ b/sparkr.gemspec @@ -0,0 +1,24 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'sparkr/version' + +Gem::Specification.new do |spec| + spec.name = "sparkr" + spec.version = Sparkr::VERSION + spec.authors = ["René Föhring"] + spec.email = ["rf@bamaru.de"] + spec.summary = %w{Sparklines in your shell, in Ruby} + spec.description = %q{Sparklines in your shell, in Ruby} + spec.homepage = "" + spec.license = "MIT" + + spec.files = `git ls-files -z`.split("\x0") + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] + + spec.add_development_dependency "bundler", "~> 1.5" + spec.add_development_dependency "rake" + spec.add_development_dependency "pry" +end diff --git a/test/sparkr_test.rb b/test/sparkr_test.rb new file mode 100644 index 0000000..0a13df2 --- /dev/null +++ b/test/sparkr_test.rb @@ -0,0 +1,21 @@ +require File.expand_path(File.dirname(__FILE__) + '/test_helper') + +describe ::Sparkr do + it "should find work" do + puts Sparkr.sparkline([1,5,22,13,5]) + puts Sparkr.sparkline([0,30,55,80,33,150]) + puts Sparkr.sparkline([5.5,20]) + puts Sparkr.sparkline([1,2,3,4,100,5,10,20,50,300]) + puts Sparkr.sparkline([1,50,100]) + puts Sparkr.sparkline([2,4,8]) + puts Sparkr.sparkline([1,2,3,4,5]) + + assert_equal '▁▂█▅▂', Sparkr.sparkline([1,5,22,13,5]) + assert_equal '▁▂▃▅▂▇', Sparkr.sparkline([0,30,55,80,33,150]) + assert_equal '▁█', Sparkr.sparkline([5.5,20]) + assert_equal '▁▁▁▁▃▁▁▁▂█', Sparkr.sparkline([1,2,3,4,100,5,10,20,50,300]) + assert_equal '▁▄█', Sparkr.sparkline([1,50,100]) + assert_equal '▁▃█', Sparkr.sparkline([2,4,8]) + assert_equal '▁▂▄▆█', Sparkr.sparkline([1,2,3,4,5]) + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..90dfafe --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,13 @@ +require 'simplecov' +SimpleCov.start + +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) + +require 'minitest/spec' +require 'minitest/autorun' +require 'bundler' +Bundler.require + +def fixture_path(name) + File.join(File.dirname(__FILE__), "fixtures", name.to_s) +end