spinning_cursor 0.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.1.3"
12
+ gem "jeweler", "~> 1.8.3"
13
+ end
@@ -0,0 +1,27 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.8.3)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rdoc
10
+ json (1.6.6)
11
+ rake (0.9.2.2)
12
+ rdoc (3.12)
13
+ json (~> 1.4)
14
+ shoulda (3.0.1)
15
+ shoulda-context (~> 1.0.0)
16
+ shoulda-matchers (~> 1.0.0)
17
+ shoulda-context (1.0.0)
18
+ shoulda-matchers (1.0.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 1.1.3)
25
+ jeweler (~> 1.8.3)
26
+ rdoc (~> 3.12)
27
+ shoulda
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Adnan Abdulhussein
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,111 @@
1
+ == Spinning Cursor
2
+
3
+ Spinning Cursor is a tiny library that allows you to easily produce a
4
+ waiting/loading message for your Ruby command line program, when a more
5
+ complex solution, such as a progress bar, doesn't fit your needs.
6
+
7
+ Inspired by Chris Wanstrath's Choice[http://https://github.com/defunkt/choice],
8
+ Spinning Cursor provides you with a DSL for easy use of the library.
9
+
10
+ == Installation
11
+
12
+ As easy as RubyGems:
13
+
14
+ $ gem install spinning_cursor --pre
15
+
16
+ == Usage
17
+
18
+ It's so simple it hurts!
19
+
20
+ === Example
21
+
22
+ # my_awesome_ruby_class.rb
23
+
24
+ require 'spinning_cursor' # you'll definitely need this bit
25
+
26
+ class MyAwesomeRubyClass
27
+ def amazing_task
28
+ SpinningCursor.start do
29
+ banner "An amazing task is happening"
30
+ type :spinner
31
+ action do
32
+ # Zzz
33
+ sleep 10
34
+ end
35
+ message "Huh?! I'm awake!"
36
+ end
37
+ end
38
+ end
39
+
40
+ great_instance = MyAwesomeRubyClass.new
41
+ great_instance.amazing_task
42
+
43
+ It's as easy as that!
44
+
45
+ === Options
46
+
47
+ * +banner+ - This displays before the cursor. Defaults to "Loading".
48
+ * +type+ - The type of spinner (currently only +:dots+ and +:spinner+).
49
+ Defaults to +:spinner+.
50
+ * +action+ - The stuff you want to do whilst the spinner is spinning.
51
+ * +message+ - The message you want to show the user once the task is finished.
52
+ Defaults to "Done".
53
+
54
+ === But the +action+ block would get too messy!
55
+
56
+ Fear not, lost soul. There are two ways to prevent messy code as a result of
57
+ the block.
58
+
59
+ 1. Call a method
60
+ 2. Start and stop the cursor manually
61
+
62
+ The first option is the simplest, but the second isn't so bad either.
63
+ It's pretty simple, just do:
64
+
65
+ SpinningCursor.start do
66
+ banner "Loading"
67
+ type :dots
68
+ message "Done"
69
+ end
70
+
71
+ # Complex code that takes a long time
72
+ sleep 20
73
+
74
+ SpinningCursor.stop
75
+
76
+ *Notice* the absense of the +action+ option. The start method will only keep
77
+ the cursor running if an +action+ block isn't passed into it.
78
+
79
+ === I want to be able to change the finish message conditionally!
80
+
81
+ Do you? Well that's easy too (I'm starting to see a pattern here...)!
82
+
83
+ Use the +set_message+ method to change the message during the execution:
84
+
85
+ SpinningCursor.start do
86
+ banner "Calculating your favour colour, please wait"
87
+ type :dots
88
+ action do
89
+ sleep 20
90
+ if you_are_romantic
91
+ SpinningCursor.set_message "Your favourite colour is pink."
92
+ elsif you_are_peaceful
93
+ SpinningCursor.set_message "Your favourite colour is blue."
94
+ else
95
+ SpinningCursor.set_message "Can't figure it out =[!"
96
+ end
97
+ end
98
+ end
99
+
100
+ You get the +message+. (see what I did there?)
101
+
102
+ == Contributing to Spinning Cursor
103
+
104
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
105
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
106
+ * Fork the project.
107
+ * Start a feature/bugfix branch.
108
+ * Commit and push until you are happy with your contribution.
109
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
110
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
111
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "spinning_cursor"
18
+ gem.homepage = "http://github.com/Prydonius/spinning_cursor"
19
+ gem.license = "MIT"
20
+ gem.summary = "A loader for command line tools in Ruby."
21
+ gem.description = "Spinning Cursor is a tiny library that allows you to easily produce a
22
+ waiting/loading message for your Ruby command line program."
23
+ gem.email = "adnan@prydoni.us"
24
+ gem.authors = ["Adnan Abdulhussein"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ task :default => :test
37
+
38
+ require 'rdoc/task'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "spinning_cursor #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('LICENSE*')
46
+ rdoc.rdoc_files.include('VERSION')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ rdoc.main = "README.rdoc"
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0.rc1
@@ -0,0 +1,53 @@
1
+ require "spinning_cursor/cursor"
2
+ require "spinning_cursor/parser"
3
+
4
+ module SpinningCursor
5
+ extend self
6
+
7
+ #
8
+ # Sends passed block to Parser, and starts cursor thread
9
+ # It will execute the action block and kill the cursor
10
+ # thread if an action block is passed.
11
+ #
12
+ def start(&block)
13
+ if defined? @@curs
14
+ if @@curs.alive?
15
+ stop
16
+ end
17
+ end
18
+
19
+ @@parsed = Parser.new(block)
20
+ @@curs = Thread.new {
21
+ Cursor.new((@@parsed.type nil), (@@parsed.banner nil))
22
+ }
23
+ if @@parsed.action.nil?
24
+ return
25
+ end
26
+ @@parsed.originator.instance_eval &@@parsed.action
27
+ stop
28
+ end
29
+
30
+ #
31
+ # Kills the cursor thread and prints the finished message
32
+ #
33
+ def stop
34
+ @@curs.kill
35
+ reset_line
36
+ puts (@@parsed.message nil)
37
+ end
38
+
39
+ #
40
+ # Determines whether the cursor thread is still running
41
+ #
42
+ def alive?
43
+ @@curs.alive?
44
+ end
45
+
46
+ #
47
+ # Sets the finish message (to be used inside the action for
48
+ # non-deterministic output)
49
+ #
50
+ def set_message(msg)
51
+ @@parsed.message msg
52
+ end
53
+ end
@@ -0,0 +1,64 @@
1
+ module SpinningCursor
2
+ if RUBY_PLATFORM =~ /(win|w)32$/
3
+ # Contains a string to clear the line in the shell
4
+ CLR = " \r"
5
+ # Haven't yet found a good solution for Windows...
6
+ else
7
+ # Unix
8
+ CLR = "\e[0K"
9
+ end
10
+
11
+ #
12
+ # Manages line reset in the console
13
+ #
14
+ def reset_line(text = "")
15
+ print "\r#{CLR}#{text}"
16
+ end
17
+
18
+ #
19
+ # This class contains the cursor types (and their helper methods)
20
+ #
21
+ class Cursor
22
+ #
23
+ # Start the printing
24
+ #
25
+ def initialize(type = :spinner, banner = "Loading")
26
+ @banner = banner
27
+ $stdout.sync = true
28
+ print @banner
29
+ send type
30
+ end
31
+
32
+ #
33
+ # Prints three dots and clears the line
34
+ #
35
+ def dots
36
+ i = 1
37
+ loop do
38
+ sleep 1
39
+ if i % 4 == 0
40
+ SpinningCursor.reset_line @banner
41
+ i += 1
42
+ next
43
+ end
44
+ i += 1
45
+ print "."
46
+ end
47
+ end
48
+
49
+ #
50
+ # Cycles through '|', '/', '-', '\', resembling a spinning cursor
51
+ #
52
+ def spinner
53
+ spinners = ['|', '/', '-', '\\']
54
+ i = 0
55
+ loop do
56
+ print " " unless @banner.empty?
57
+ print spinners[i % 4]
58
+ sleep 0.5
59
+ SpinningCursor.reset_line @banner
60
+ i += 1
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,51 @@
1
+ module SpinningCursor
2
+ class Parser
3
+ attr_reader :originator
4
+
5
+ #
6
+ # Parses proc
7
+ #
8
+ def initialize(proc)
9
+ @banner = "Loading"
10
+ @type = :spinner
11
+ @message = "Done"
12
+
13
+ if not proc.nil?
14
+ # Store the originating class for use in method_missing
15
+ @originator = eval "self", proc.binding
16
+ instance_eval &proc
17
+ end
18
+
19
+ self
20
+ end
21
+
22
+ #
23
+ # Getter and setter for the action block
24
+ #
25
+ def action(&block)
26
+ @action = block unless block.nil?
27
+
28
+ @action
29
+ end
30
+
31
+ #
32
+ # Getters and setters for +banner+, +type+ and +message+
33
+ # attributes.
34
+ # Note:: for getting, pass nil e.g. <tt>banner nil</tt>
35
+ #
36
+ %w[banner type message].each do |method|
37
+ define_method(method) do |string|
38
+ var = "@#{method}"
39
+ return instance_variable_get(var) if string.nil?
40
+ instance_variable_set(var, string)
41
+ end
42
+ end
43
+
44
+ #
45
+ # Pass any other methods to the calling class
46
+ #
47
+ def method_missing(method, *args, &block)
48
+ @originator.send method, *args, &block
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'spinning_cursor'
16
+
17
+ # Allows capturing of stdout
18
+ # (http://thinkingdigitally.com/archive/capturing-output-from-puts-in-ruby/)
19
+
20
+ require 'stringio'
21
+
22
+ module Kernel
23
+ def capture_stdout
24
+ out = StringIO.new
25
+ $stdout = out
26
+ yield out
27
+ ensure
28
+ $stdout = STDOUT
29
+ end
30
+ end
31
+
32
+ class Test::Unit::TestCase
33
+ end
@@ -0,0 +1,43 @@
1
+ require 'helper'
2
+
3
+ class TestSpinningCursorCursor < Test::Unit::TestCase
4
+ context "dots" do
5
+ should "reset line after printing three dots" do
6
+ capture_stdout do |out|
7
+ dots = Thread.new do
8
+ SpinningCursor::Cursor.new :dots, ""
9
+ end
10
+ sleep 5
11
+ dots.kill
12
+ # \r\e[0K is move cursor to the start of the line and clear line
13
+ # in bash
14
+ assert_equal "...\r\e[0K", out.string
15
+ end
16
+ end
17
+ end
18
+
19
+ context "spinner" do
20
+ should "cycle through correctly" do
21
+ capture_stdout do |out|
22
+ spinner = Thread.new do
23
+ SpinningCursor::Cursor.new :spinner, ""
24
+ end
25
+ sleep 0.1
26
+ assert_equal "|", out.string
27
+ buffer = "|\r\e[0K"
28
+ sleep 0.5
29
+ assert_equal "#{buffer}/", out.string
30
+ buffer += "/\r\e[0K"
31
+ sleep 0.5
32
+ assert_equal "#{buffer}-", out.string
33
+ buffer += "-\r\e[0K"
34
+ sleep 0.5
35
+ assert_equal "#{buffer}\\", out.string
36
+ buffer += "\\\r\e[0K"
37
+ sleep 0.5
38
+ assert_equal "#{buffer}|", out.string
39
+ spinner.kill
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,34 @@
1
+ require 'helper'
2
+
3
+ class TestSpinningCursorParser < Test::Unit::TestCase
4
+ context "parser" do
5
+ should "check calling class for any missing methods" do
6
+ def banner_text
7
+ "Banner"
8
+ end
9
+ parser = SpinningCursor::Parser.new Proc.new { banner banner_text }
10
+ assert_equal banner_text, (parser.banner nil)
11
+ end
12
+ end
13
+
14
+ context "banner, type, message and action methods" do
15
+ setup do
16
+ @parser = SpinningCursor::Parser.new Proc.new { }
17
+ end
18
+
19
+ should "act as getters and setters" do
20
+ @parser.banner "a new banner"
21
+ assert_equal "a new banner", (@parser.banner nil)
22
+
23
+ @parser.type :dots
24
+ assert_equal :dots, (@parser.type nil)
25
+
26
+ @parser.message "a message"
27
+ assert_equal "a message", (@parser.message nil)
28
+
29
+ proc = Proc.new { }
30
+ @parser.action &proc
31
+ assert_equal proc, @parser.action
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,58 @@
1
+ require 'helper'
2
+
3
+ class TestSpinningCursor < Test::Unit::TestCase
4
+ context "when an action block is passed it" do
5
+ should "start the cursor, run block content and kill the cursor" do
6
+ # Hide any output
7
+ capture_stdout do |out|
8
+ SpinningCursor.start do
9
+ action { sleep 1 }
10
+ end
11
+ # Give it some time to abort
12
+ sleep 0.1
13
+ assert_equal false, SpinningCursor.alive?
14
+ end
15
+ end
16
+
17
+ should "evalute the block from the calling class" do
18
+ @num = 1
19
+ capture_stdout do |out|
20
+ SpinningCursor.start do
21
+ action { @num += 1 }
22
+ end
23
+
24
+ assert_equal 2, @num
25
+ end
26
+ end
27
+ end
28
+
29
+ context "when an action block isn't passed it" do
30
+ should "start the cursor, and keep it going until stop is called" do
31
+ capture_stdout do |out|
32
+ SpinningCursor.start do
33
+ banner "no action block"
34
+ end
35
+ sleep 2
36
+ assert_equal true, SpinningCursor.alive?
37
+ SpinningCursor.stop
38
+ sleep 0.1
39
+ assert_equal false, SpinningCursor.alive?
40
+ end
41
+ end
42
+ end
43
+
44
+ context "whilst running it" do
45
+ should "allow you to change the end message" do
46
+ capture_stdout do |out|
47
+ SpinningCursor.start do
48
+ action do
49
+ SpinningCursor.set_message "Failed!"
50
+ end
51
+ message "Finished!"
52
+ end
53
+
54
+ assert_equal true, (out.string.end_with? "Failed!\n")
55
+ end
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spinning_cursor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.rc1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - Adnan Abdulhussein
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.1.3
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.3
78
+ description: ! 'Spinning Cursor is a tiny library that allows you to easily produce
79
+ a
80
+
81
+ waiting/loading message for your Ruby command line program.'
82
+ email: adnan@prydoni.us
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files:
86
+ - LICENSE.txt
87
+ - README.rdoc
88
+ files:
89
+ - .document
90
+ - Gemfile
91
+ - Gemfile.lock
92
+ - LICENSE.txt
93
+ - README.rdoc
94
+ - Rakefile
95
+ - VERSION
96
+ - lib/spinning_cursor.rb
97
+ - lib/spinning_cursor/cursor.rb
98
+ - lib/spinning_cursor/parser.rb
99
+ - test/helper.rb
100
+ - test/test_cursors.rb
101
+ - test/test_parser.rb
102
+ - test/test_spinning_cursor.rb
103
+ homepage: http://github.com/Prydonius/spinning_cursor
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ segments:
117
+ - 0
118
+ hash: 837562280990932011
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>'
123
+ - !ruby/object:Gem::Version
124
+ version: 1.3.1
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.21
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: A loader for command line tools in Ruby.
131
+ test_files: []