goldstar-ansi 1.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,7 @@
1
+ README.rdoc
2
+ Rakefile
3
+ ansi.gemspec
4
+ init.rb
5
+ lib/ansi.rb
6
+ test/ansi_test.rb
7
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,38 @@
1
+ = ANSI
2
+
3
+ == DESCRIPTION
4
+
5
+ Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.
6
+
7
+ == INSTALLATION
8
+
9
+ as a gem:
10
+ sudo gem install ssoroka-ansi
11
+
12
+ as a plugin:
13
+ script/plugin install git://github.com/ssoroka/ansi.git
14
+
15
+ == USAGE
16
+
17
+ require 'ansi' # if not installed as a plugin.
18
+
19
+ 1) Simply control the cursor:
20
+ >> puts "HELLO" + ANSI.right(30) + "THERE!"
21
+ HELLO THERE!
22
+
23
+ 2) use colors:
24
+
25
+ >> puts ANSI.color(:red) { "hello there" }
26
+ >> puts ANSI.color(:green) + "Everything is green now" + ANSI.no_color
27
+
28
+ 3) loops:
29
+
30
+ printf ANSI.clear_screen
31
+ puts "Processing users..."
32
+ max = User.count
33
+ User.all.each_with_index {|user, index|
34
+ user.update_something!
35
+ printf ANSI.left(50) + "Processed #{index}/#{max} users..."
36
+ }
37
+ puts " done!"
38
+
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('goldstar-ansi', '1.0.1.1') do |p|
6
+ p.description = 'Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.'
7
+ p.url = 'http://github.com/ssoroka/ansi'
8
+ p.author = 'Steven Soroka'
9
+ p.email = 'ssoroka78@gmail.com'
10
+ p.ignore_pattern = ["tmp/*", 'script/**']
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{|f| load f }
data/ansi.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{ansi}
5
+ s.version = "1.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Steven Soroka"]
9
+ s.date = %q{2011-02-06}
10
+ s.description = %q{Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.}
11
+ s.email = %q{ssoroka78@gmail.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/ansi.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "ansi.gemspec", "init.rb", "lib/ansi.rb", "test/ansi_test.rb", "Manifest"]
14
+ s.homepage = %q{http://github.com/ssoroka/ansi}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ansi", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{ansi}
18
+ s.rubygems_version = %q{1.4.1}
19
+ s.summary = %q{Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.}
20
+ s.test_files = ["test/ansi_test.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{goldstar-ansi}
5
+ s.version = "1.0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Steven Soroka"]
9
+ s.date = %q{2011-02-06}
10
+ s.description = %q{Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.}
11
+ s.email = %q{ssoroka78@gmail.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/ansi.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "ansi.gemspec", "init.rb", "lib/ansi.rb", "test/ansi_test.rb", "Manifest", "goldstar-ansi.gemspec"]
14
+ s.homepage = %q{http://github.com/ssoroka/ansi}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Goldstar-ansi", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{goldstar-ansi}
18
+ s.rubygems_version = %q{1.4.1}
19
+ s.summary = %q{Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.}
20
+ s.test_files = ["test/ansi_test.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/lib/ansi'
data/lib/ansi.rb ADDED
@@ -0,0 +1,98 @@
1
+ # based on http://en.wikipedia.org/wiki/ANSI_escape_code
2
+ # Written by Steven Soroka
3
+ module ANSI
4
+ class <<self
5
+ COLORS = {:black => 0, :red => 1, :green => 2, :yellow => 3, :blue => 4, :magenta => 5, :purple => 5,
6
+ :cyan => 6, :light_blue => 6, :lt_blue => 6, :white => 7}
7
+
8
+ # Some of these are not widely supported, like italics, underline, blink_fast, etc.
9
+ COLOR_OPTIONS = {:normal => 0, :bold => 1, :faint => 2, :italics => 3, :underline => 4, :blink => 5,
10
+ :blink_slow => 5, :blink_fast => 6, :double_underline => 21, :no_underline => 24, :no_blink => 25}
11
+
12
+ def back(count = 79)
13
+ esc_code("#{count}D")
14
+ end
15
+ alias_method :left, :back
16
+
17
+ def forward(count = 1)
18
+ esc_code("#{count}C")
19
+ end
20
+ alias_method :right, :forward
21
+
22
+ def up(count = 1)
23
+ esc_code("#{count}A")
24
+ end
25
+
26
+ def down(count = 1)
27
+ esc_code("#{count}B")
28
+ end
29
+
30
+ def color(color, options = {}, &block)
31
+ r = set_color(color, options.merge(:base => options[:bright] ? 90 : 30))
32
+ if block_given?
33
+ r += yield
34
+ r += no_color
35
+ end
36
+ r
37
+ end
38
+
39
+ def bg_color(color, options = {}, &block)
40
+ r = set_color(color, options.merge(:base => options[:bright] ? 100 : 40))
41
+ if block_given?
42
+ r += yield
43
+ r += no_color
44
+ end
45
+ r
46
+ end
47
+
48
+ def no_color
49
+ esc_code('m')
50
+ end
51
+
52
+ def clear_line(left_right_or_all = :right)
53
+ opt = {:left => 1, :right => 0, :line => 2, :all => 2}[left_right_or_all]
54
+ esc_code("#{opt}K")
55
+ end
56
+ alias_method :clear, :clear_line # maybe clear should be its own method that can clear the screen, too.
57
+
58
+ def hide_cursor(&blk)
59
+ out = esc_code("?25l")
60
+ out << (yield + show_cursor) if block_given?
61
+ out
62
+ end
63
+
64
+ def show_cursor
65
+ esc_code("?25h")
66
+ end
67
+
68
+ def save_position
69
+ esc_code("s")
70
+ end
71
+
72
+ def restore_position
73
+ esc_code("u")
74
+ end
75
+
76
+ def clear_screen(top_or_bottom_or_all = :all)
77
+ opt = {:top => 1, :bottom => 0, :all => 2}[top_or_bottom_or_all]
78
+ esc_code("#{opt}J")
79
+ end
80
+ private
81
+ def set_color(color, options)
82
+ raise Exception.new("Invalid color") unless COLORS.keys.include?(color)
83
+ opts = [COLORS[color] + options[:base]]
84
+ COLOR_OPTIONS.each{|k,v|
85
+ opts << v if options[k]
86
+ }
87
+ esc_code("#{opts.join(';')}m")
88
+ end
89
+
90
+ def esc_code(s)
91
+ "#{esc}#{s}" #.wrap('\[', '\]') # this wrap was supposed to avoid messing up wrapping long lines. maybe not needed.
92
+ end
93
+
94
+ def esc
95
+ "\033["
96
+ end
97
+ end
98
+ end
data/test/ansi_test.rb ADDED
@@ -0,0 +1,68 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__)+'/../lib/ansi'
3
+
4
+ class Ansi < Test::Unit::TestCase
5
+ def test_ansi_should_know_how_to_move_left
6
+ assert_equal ANSI.left(3), "\033[3D"
7
+ end
8
+
9
+ def test_ansi_should_know_how_to_move_right
10
+ assert_equal ANSI.right(3), "\033[3C"
11
+ end
12
+
13
+ def test_ansi_should_know_how_to_move_up
14
+ assert_equal ANSI.up(3), "\033[3A"
15
+ end
16
+
17
+ def test_ansi_should_know_how_to_move_down
18
+ assert_equal ANSI.down(3), "\033[3B"
19
+ end
20
+
21
+ def test_ansi_should_know_how_to_turn_color_off
22
+ assert_equal ANSI.no_color, "\033[m"
23
+ end
24
+
25
+ def test_ansi_should_clear_text_to_the_right
26
+ assert_equal ANSI.clear(:right), "\033[0K"
27
+ end
28
+
29
+ def test_ansi_should_clear_text_to_the_left
30
+ assert_equal ANSI.clear(:left), "\033[1K"
31
+ end
32
+
33
+ def test_ansi_should_clear_the_line
34
+ assert_equal ANSI.clear(:line), "\033[2K"
35
+ end
36
+
37
+ def test_ansi_should_hide_cursor
38
+ assert_equal ANSI.hide_cursor, "\033[?25l"
39
+ end
40
+
41
+ def test_ansi_should_show_cursor
42
+ assert_equal ANSI.show_cursor, "\033[?25h"
43
+ end
44
+
45
+ def test_ansi_color_should_display_blue
46
+ assert_equal ANSI.color(:blue), "\033[34m"
47
+ end
48
+
49
+ def test_ansi_color_should_blink
50
+ assert_equal ANSI.color(:black, :blink => true), "\033[30;5m"
51
+ end
52
+
53
+ def test_ansi_color_should_display_background_color
54
+ assert_equal ANSI.bg_color(:blue), "\033[44m"
55
+ end
56
+
57
+ def test_ansi_color_should_clear_the_screen
58
+ assert_equal ANSI.clear_screen(:all), "\033[2J"
59
+ end
60
+
61
+ def test_ansi_color_should_save_cursor_position
62
+ assert_equal ANSI.save_position, "\033[s"
63
+ end
64
+
65
+ def test_ansi_color_should_restore_cursor_position
66
+ assert_equal ANSI.restore_position, "\033[u"
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goldstar-ansi
3
+ version: !ruby/object:Gem::Version
4
+ hash: 89
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ - 1
11
+ version: 1.0.1.1
12
+ platform: ruby
13
+ authors:
14
+ - Steven Soroka
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-02-06 00:00:00 -05:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.
24
+ email: ssoroka78@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README.rdoc
31
+ - lib/ansi.rb
32
+ files:
33
+ - README.rdoc
34
+ - Rakefile
35
+ - ansi.gemspec
36
+ - init.rb
37
+ - lib/ansi.rb
38
+ - test/ansi_test.rb
39
+ - Manifest
40
+ - goldstar-ansi.gemspec
41
+ has_rdoc: true
42
+ homepage: http://github.com/ssoroka/ansi
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --line-numbers
48
+ - --inline-source
49
+ - --title
50
+ - Goldstar-ansi
51
+ - --main
52
+ - README.rdoc
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 11
70
+ segments:
71
+ - 1
72
+ - 2
73
+ version: "1.2"
74
+ requirements: []
75
+
76
+ rubyforge_project: goldstar-ansi
77
+ rubygems_version: 1.4.1
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.
81
+ test_files:
82
+ - test/ansi_test.rb