terminal_color 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.txt +83 -0
  2. data/lib/terminal_color/ansi.rb +94 -0
  3. metadata +57 -0
@@ -0,0 +1,83 @@
1
+ = Terminal Color
2
+ Terminal Color is a tiny Ruby library for colorizing, or colourising, string output to the terminal.
3
+
4
+ == Examples
5
+ require 'terminal_color/ansi'
6
+
7
+ puts Terminal::Color::ANSI.color("Hello world!", :green)
8
+
9
+ This isn't limited to strings
10
+
11
+ puts Terminal::Color::ANSI.color(Time.now, :yellow)
12
+
13
+ Typing that chain of modules is going to get boring pretty quick
14
+
15
+ class String; include Terminal::Color::ANSI; end
16
+ puts "Warning".color(:black, :yellow_background)
17
+
18
+ That's not limited to strings either
19
+
20
+ class Time; include Terminal::Color::ANSI; end
21
+ puts Time.now.color(:bold, :white, :black_background)
22
+
23
+ We're not fussy about how you want to spell colour
24
+
25
+ puts Terminal::Colour::ANSI.colour("Tally Ho!", :red, :white_background)
26
+ puts "pip pip".colour(:blue)
27
+
28
+ You can make colours run on
29
+
30
+ puts "this is red".color(:red, false)
31
+ puts "so is this!"
32
+ puts "lets put a stop to it now".style(:reset)
33
+
34
+ style is just an alias to color, it's there because "foo".color(:bold) looks strange
35
+
36
+ There are a few colours to choose from
37
+ black
38
+ red
39
+ green
40
+ yellow
41
+ blue
42
+ magenta
43
+ cyan
44
+ white
45
+
46
+ You can get them as a background by appending _background or _bg, and on some platforms a brighter version by prepending bright_. These work together so you can have bright_yellow_background for example.
47
+
48
+ puts "Warning".color(:black, :bright_yellow_background)
49
+
50
+ There are some other not-strictly-colour options
51
+ reset
52
+ reset_forground (or reset_fg)
53
+ reset_background (or reset_bg)
54
+ negative (or inverse, or reverse)
55
+ bold
56
+ underline
57
+
58
+ You can use raw ANSI codes too
59
+ puts Terminal::Color::ANSI.color("Italic, maybe", 3)
60
+
61
+ == Licence
62
+
63
+ The MIT License
64
+
65
+ Copyright (c) 2009 Matthew Sadler
66
+
67
+ Permission is hereby granted, free of charge, to any person obtaining a copy
68
+ of this software and associated documentation files (the "Software"), to deal
69
+ in the Software without restriction, including without limitation the rights
70
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
71
+ copies of the Software, and to permit persons to whom the Software is
72
+ furnished to do so, subject to the following conditions:
73
+
74
+ The above copyright notice and this permission notice shall be included in
75
+ all copies or substantial portions of the Software.
76
+
77
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
80
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
81
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
82
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
83
+ THE SOFTWARE.
@@ -0,0 +1,94 @@
1
+ module Terminal # :nodoc:
2
+ module Color # :nodoc:
3
+ module ANSI
4
+ BLACK = BLACK_FOREGROUND = 30
5
+ RED = RED_FOREGROUND = 31
6
+ GREEN = GREEN_FOREGROUND = 32
7
+ YELLOW = YELLOW_FOREGROUND = 33
8
+ BLUE = BLUE_FOREGROUND = 34
9
+ MAGENTA = MAGENTA_FOREGROUND = 35
10
+ CYAN = CYAN_FOREGROUND = 36
11
+ WHITE = WHITE_FOREGROUND = 37
12
+
13
+ BRIGHT_BLACK = BRIGHT_BLACK_FOREGROUND = 90
14
+ BRIGHT_RED = BRIGHT_RED_FOREGROUND = 91
15
+ BRIGHT_GREEN = BRIGHT_GREEN_FOREGROUND = 92
16
+ BRIGHT_YELLOW = BRIGHT_YELLOW_FOREGROUND = 93
17
+ BRIGHT_BLUE = BRIGHT_BLUE_FOREGROUND = 94
18
+ BRIGHT_MAGENTA = BRIGHT_MAGENTA_FOREGROUND = 95
19
+ BRIGHT_CYAN = BRIGHT_CYAN_FOREGROUND = 96
20
+ BRIGHT_WHITE = BRIGHT_WHITE_FOREGROUND = 97
21
+
22
+ BLACK_BACKGROUND = 40
23
+ RED_BACKGROUND = 41
24
+ GREEN_BACKGROUND = 42
25
+ YELLOW_BACKGROUND = 43
26
+ BLUE_BACKGROUND = 44
27
+ MAGENTA_BACKGROUND = 45
28
+ CYAN_BACKGROUND = 46
29
+ WHITE_BACKGROUND = 47
30
+
31
+ BRIGHT_BLACK_BACKGROUND = 100
32
+ BRIGHT_RED_BACKGROUND = 101
33
+ BRIGHT_GREEN_BACKGROUND = 102
34
+ BRIGHT_YELLOW_BACKGROUND = 103
35
+ BRIGHT_BLUE_BACKGROUND = 104
36
+ BRIGHT_MAGENTA_BACKGROUND = 105
37
+ BRIGHT_CYAN_BACKGROUND = 106
38
+ BRIGHT_WHITE_BACKGROUND = 107
39
+
40
+ RESET = 0
41
+ RESET_FOREGROUND = 39
42
+ RESET_BACKGROUND = 49
43
+
44
+ BOLD = 1
45
+ NEGATIVE = INVERSE = REVERSE = 7
46
+ UNDERLINE = 4
47
+
48
+ def self.escape(*args) # :nodoc:
49
+ args = [0] if args.empty?
50
+ "\e[#{args.join(";")}m"
51
+ end
52
+
53
+ # :call-seq: ANSI.color(string, color, ...) -> colored_string
54
+ #
55
+ # Returns a colored version of +string+
56
+ # puts Terminal::Color::ANSI.color("Hello world!", :green)
57
+ # puts Terminal::Color::ANSI.color("Warning", :black, :yellow_background)
58
+ #
59
+ def self.color(string, *args)
60
+ reset = if args.last then true else args.pop end
61
+ args.map! {|arg| parse_color(arg)}
62
+
63
+ "#{escape(*args)}#{string}#{escape(RESET) if reset}"
64
+ end
65
+ class << self
66
+ alias colour color
67
+ alias style color
68
+ end
69
+
70
+ # :call-seq: obj.color(color, ...) -> colored_string
71
+ #
72
+ # Returns a colored string representation of +obj+
73
+ # puts "Hello world!".color(:green)
74
+ # puts "Warning".color(:black, :yellow_background)
75
+ #
76
+ def color(*args)
77
+ Terminal::Color::ANSI.color(self.to_s, *args)
78
+ end
79
+ alias colour color
80
+ alias style color
81
+
82
+ private
83
+ def self.parse_color(string)
84
+ return string if string.is_a?(Integer)
85
+
86
+ string = string.to_s.upcase
87
+ string.sub!(/_BG$/, "_BACKGROUND")
88
+ string.sub!(/_FG$/, "_FOREGROUND")
89
+ const_get(string)
90
+ end
91
+ end
92
+ end
93
+ Colour = Color
94
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: terminal_color
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Sadler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-15 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A tiny Ruby library for colorizing terminal output.
17
+ email: mat@sourcetagsandcodes.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ files:
25
+ - lib/terminal_color/ansi.rb
26
+ - README.txt
27
+ has_rdoc: true
28
+ homepage: http://github.com/matsadler/terminal_color
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --main
34
+ - README.txt
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Terminal colors.
56
+ test_files: []
57
+