ssoroka-ansi 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +36 -0
- data/Rakefile +14 -0
- data/ansi.gemspec +30 -0
- data/lib/ansi.rb +98 -0
- data/test/ansi_test.rb +68 -0
- metadata +65 -0
data/README.rdoc
ADDED
@@ -0,0 +1,36 @@
|
|
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-ruby
|
11
|
+
|
12
|
+
as a plugin:
|
13
|
+
script/plugin install git://github.com/ssoroka/ansi.git
|
14
|
+
|
15
|
+
== USAGE
|
16
|
+
|
17
|
+
1) Simply control the cursor:
|
18
|
+
>> puts "HELLO" + ANSI.right(30) + "THERE!"
|
19
|
+
HELLO THERE!
|
20
|
+
|
21
|
+
2) use colors:
|
22
|
+
|
23
|
+
>> puts ANSI.color(:red) { "hello there" }
|
24
|
+
>> puts ANSI.color(:green) + "Everything is green now" + ANSI.no_color
|
25
|
+
|
26
|
+
3) loops:
|
27
|
+
|
28
|
+
printf ANSI.clear_screen
|
29
|
+
puts "Processing users..."
|
30
|
+
max = User.count
|
31
|
+
User.all.each_with_index {|user, index|
|
32
|
+
user.update_something!
|
33
|
+
printf ANSI.left(50) + "Processed #{index}/#{max} users..."
|
34
|
+
}
|
35
|
+
puts " done!"
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('ansi', '1.0.0') 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
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{ansi}
|
3
|
+
s.version = "1.0.0"
|
4
|
+
|
5
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
6
|
+
s.authors = ["Steven Soroka"]
|
7
|
+
s.date = %q{2008-11-12}
|
8
|
+
s.description = %q{Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.}
|
9
|
+
s.email = %q{ssoroka78@gmail.com}
|
10
|
+
s.extra_rdoc_files = ["lib/ansi.rb", "README.rdoc"]
|
11
|
+
s.files = ["ansi.gemspec", "init.rb", "lib/ansi.rb", "Manifest", "Rakefile", "README.rdoc", "test/ansi_test.rb"]
|
12
|
+
s.has_rdoc = true
|
13
|
+
s.homepage = %q{http://github.com/ssoroka/ansi}
|
14
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ansi", "--main", "README.rdoc"]
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
s.rubyforge_project = %q{ansi}
|
17
|
+
s.rubygems_version = %q{1.2.0}
|
18
|
+
s.summary = %q{Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.}
|
19
|
+
s.test_files = ["test/ansi_test.rb"]
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 2
|
24
|
+
|
25
|
+
if current_version >= 3 then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
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,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ssoroka-ansi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steven Soroka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-12 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.
|
17
|
+
email: ssoroka78@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- lib/ansi.rb
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- ansi.gemspec
|
27
|
+
- init.rb
|
28
|
+
- lib/ansi.rb
|
29
|
+
- Manifest
|
30
|
+
- Rakefile
|
31
|
+
- README.rdoc
|
32
|
+
- test/ansi_test.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/ssoroka/ansi
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --line-numbers
|
38
|
+
- --inline-source
|
39
|
+
- --title
|
40
|
+
- Ansi
|
41
|
+
- --main
|
42
|
+
- README.rdoc
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "1.2"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project: ansi
|
60
|
+
rubygems_version: 1.2.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: Use ANSI codes in printed output, including colors and controlling the cursor, clearing the line, and clearing the screen.
|
64
|
+
test_files:
|
65
|
+
- test/ansi_test.rb
|