cli-colorize 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/cli-colorize.rb +159 -0
  2. metadata +67 -0
@@ -0,0 +1,159 @@
1
+ #
2
+ # Author :: Chris St. John (<mailto:chris@stjohnstudios.com>)
3
+ # Date :: 08/02/2010
4
+ #
5
+
6
+ # A simple module for use when colorizing output to the terminal.
7
+ module CLIColorize
8
+
9
+ @@off = false
10
+ @@default_color = :blue
11
+
12
+ # ANSI control codes
13
+ # 0 Turn off all attributes
14
+ # 1 Set bright mode
15
+ # 4 Set underline mode
16
+ # 5 Set blink mode
17
+ # 7 Exchange foreground and background colors
18
+ # 8 Hide text (foreground color would be the same as background)
19
+ # 30 Black text
20
+ # 31 Red text
21
+ # 32 Green text
22
+ # 33 Yellow text
23
+ # 34 Blue text
24
+ # 35 Magenta text
25
+ # 36 Cyan text
26
+ # 37 White text
27
+ # 39 Default text color
28
+ # 40 Black background
29
+ # 41 Red background
30
+ # 42 Green background
31
+ # 43 Yellow background
32
+ # 44 Blue background
33
+ # 45 Magenta background
34
+ # 46 Cyan background
35
+ # 47 White background
36
+ # 49 Default background color
37
+
38
+ CTRLSTR_START = "\033["
39
+ CTRLSTR_END = "\033[0m"
40
+ CTRLSTR_DELIM = 'm'
41
+ CONFIG = {
42
+ :reset => '0',
43
+ :bright => '1',
44
+ :underline => '4',
45
+ :blink => '5',
46
+ :swap => '7',
47
+ :hide => '8'
48
+ }
49
+ SET_FORE = {
50
+ :black => '30',
51
+ :red => '31',
52
+ :green => '32',
53
+ :yellow => '33',
54
+ :blue => '34',
55
+ :magenta => '35',
56
+ :cyan => '36',
57
+ :white => '37',
58
+ :default => '39'
59
+ }
60
+ SET_BACK = {
61
+ :black => '40',
62
+ :red => '41',
63
+ :green => '42',
64
+ :yellow => '43',
65
+ :blue => '44',
66
+ :magenta => '45',
67
+ :cyan => '46',
68
+ :white => '47',
69
+ :default => '49'
70
+ }
71
+
72
+ # Set control characters around text to colorize output to the terminal.
73
+ def CLIColorize.colorize(text, color=nil)
74
+ unless color.is_a? Hash
75
+ color = @@default_color if color.nil?
76
+ "#{CTRLSTR_START+SET_FORE[color.to_sym]+CTRLSTR_DELIM}#{text}#{CTRLSTR_END}"
77
+ else
78
+ args = []
79
+ args << SET_FORE[color[:foreground].to_sym] unless color[:foreground].nil?
80
+ args << SET_BACK[color[:background].to_sym] unless color[:background].nil?
81
+ args << CONFIG[color[:config].to_sym] unless color[:config].nil?
82
+ control_string = args.join(';')
83
+ "#{CTRLSTR_START+control_string+CTRLSTR_DELIM}#{text}#{CTRLSTR_END}"
84
+ end
85
+ end
86
+
87
+ # Instance method that exposes the class method colorize to classes that `include 'CLIColorize'`
88
+ def colorize(text, color=nil)
89
+ CLIColorize.colorize(text, color)
90
+ end
91
+
92
+ # Call STDOUT.puts with the colorized text.
93
+ def CLIColorize.puts(text, color=nil)
94
+ STDOUT.puts CLIColorize.colorize(text, color)
95
+ end
96
+
97
+ # Call STDOUT.print with the colorized text.
98
+ def CLIColorize.print(text, color=nil)
99
+ STDOUT.print CLIColorize.colorize(text, color)
100
+ end
101
+
102
+ # Use safe_colorize in conjunction with CLIColorize.off and CLIColorize.on to conditionally
103
+ # determine whether or not output will be given the control characters for colorization.
104
+ # This is designed to work with a command-line switch to the script that uses this module.
105
+ def CLIColorize.safe_colorize(text, color=nil)
106
+ return text if @@off
107
+ colorize(text, color)
108
+ end
109
+
110
+ # Instance method that exposes the class method safe_colorize to classes that `include 'CLIColorize'`
111
+ def safe_colorize(text, color=nil)
112
+ CLIColorize.safe_colorize(text, color)
113
+ end
114
+
115
+ # Makes the safe_colorize method return text with colorization control codes. This is the default state.
116
+ def safe_colorize_active
117
+ CLIColorize.on
118
+ end
119
+
120
+ # Makes the safe_colorize method return text without colorization control codes.
121
+ def safe_colorize_deactive
122
+ CLIColorize.off
123
+ end
124
+
125
+ # Call STDOUT.puts with the colorized text.
126
+ def CLIColorize.safe_puts(text, color=nil)
127
+ STDOUT.puts CLIColorize.safe_colorize(text, color)
128
+ end
129
+
130
+ # Call STDOUT.print with the colorized text.
131
+ def CLIColorize.safe_print(text, color=nil)
132
+ STDOUT.print CLIColorize.safe_colorize(text, color)
133
+ end
134
+
135
+ # Instance method that exposes the class method safe_puts to classes that `include 'CLIColorize'`
136
+ def safe_puts(text, color=nil)
137
+ CLIColorize.safe_puts(text, color)
138
+ end
139
+
140
+ # Instance method that exposes the class method safe_puts to classes that `include 'CLIColorize'`
141
+ def safe_print(text, color=nil)
142
+ CLIColorize.safe_print(text, color)
143
+ end
144
+
145
+ def CLIColorize.default_color; @@default_color; end
146
+ def CLIColorize.default_color=(color)
147
+ @@default_color = color.to_sym
148
+ end
149
+ def default_color; CLIColorize.default_color; end
150
+ def default_color=(color)
151
+ CLIColorize.default_color=(color)
152
+ end
153
+
154
+ private
155
+ # Call CLIColorize.off to turn off colorizing (for instance to make the output safe
156
+ # for evaluation or for output sometimes not headed to the terminal).
157
+ def CLIColorize.off; @@off = true; end
158
+ def CLIColorize.on; @@off = false; end
159
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cli-colorize
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Chris St. John
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-29 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: The cli-colorize gem is a command-line interface colorization library.
23
+ email: chris@stjohnstudios.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/cli-colorize.rb
32
+ has_rdoc: true
33
+ homepage: https://github.com/stjohncj/cli-colorize
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.7
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: command-line interface colorization library
66
+ test_files: []
67
+