ripl-color_result 0.1.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/.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems' unless Object.const_defined?(:Gem)
3
+ require File.dirname(__FILE__) + "/lib/ripl/color_result"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ripl-color_result"
7
+ s.version = Ripl::ColorResult::VERSION
8
+ s.authors = ["Jan Lelis"]
9
+ s.email = "mail@janlelis.de"
10
+ s.homepage = "http://github.com/janlelis/ripl-color_result"
11
+ s.summary = "A ripl plugin to colorize result."
12
+ s.description = "This ripl plugin colorizes your result."
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+ s.add_dependency 'ripl', '>= 0.2.1'
15
+ s.files = Dir.glob(%w[{lib,test}/**/*.rb bin/* [A-Z]*.{txt,rdoc} ext/**/*.{rb,c} **/deps.rip]) + %w{Rakefile .gemspec}
16
+ s.extra_rdoc_files = ["README.rdoc", "COPYING"]
17
+ s.license = 'MIT'
18
+ end
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,2 @@
1
+ == 0.1.0
2
+ * Initial release.
data/COPYING ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (C) 2006-2009 Paul Duncan <pabs@pablotron.org>
2
+ Copyright (C) 2009 Jens Wille <jens.wille@gmail.com>
3
+ Copyright (C) 2010 Jan Lelis <mail@janlelis.de>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a
6
+ copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included
14
+ in all copies or substantial portions of the of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
+ DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ == Description
2
+ This {ripl}[http://github.com/cldwalker/ripl] plugin colorizes your results.
3
+
4
+ == Install
5
+ Install the gem with:
6
+
7
+ sudo gem install ripl-colorize_output
8
+
9
+ == Usage
10
+
11
+ Add to your ~/.riplrc
12
+
13
+ require 'ripl/colorize_output'
14
+
15
+ You can choose a <tt>:color_output_engine</tt>. By default, the bundled +Wirble+-like colorization is used. Possible other values are:
16
+
17
+ * <tt>:ap</tt> - using the awesome_print gem
18
+ * <tt>:coderay</tt> - using the coderay gem
19
+
20
+ Example (in your ~/.riplrc)
21
+
22
+ Ripl.config[:color_output_engine] = :coderay
23
+
24
+ == Credits
25
+
26
+ The default colorization uses code from Wirble
27
+ * Copyright (C) 2006-2009 Paul Duncan <pabs@pablotron.org>
28
+ * Copyright (C) 2009 Jens Wille <jens.wille@gmail.com>
29
+
30
+ Copyright (c) 2010 Jan Lelis, http://rbjl.net. See COPYING for details.
31
+
32
+ J-_-L
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rake'
2
+ require 'fileutils'
3
+
4
+ def gemspec
5
+ @gemspec ||= eval(File.read('.gemspec'), binding, '.gemspec')
6
+ end
7
+
8
+ desc "Build the gem"
9
+ task :gem=>:gemspec do
10
+ sh "gem build .gemspec"
11
+ FileUtils.mkdir_p 'pkg'
12
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
13
+ end
14
+
15
+ desc "Install the gem locally"
16
+ task :install => :gem do
17
+ sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}}
18
+ end
19
+
20
+ desc "Generate the gemspec"
21
+ task :generate do
22
+ puts gemspec.to_ruby
23
+ end
24
+
25
+ desc "Validate the gemspec"
26
+ task :gemspec do
27
+ gemspec.validate
28
+ end
29
+
30
+ desc 'Run tests'
31
+ task :test do |t|
32
+ sh 'bacon -q -Ilib -I. test/*_test.rb'
33
+ end
34
+
35
+ task :default => :test
data/deps.rip ADDED
@@ -0,0 +1 @@
1
+ ripl >=0.2.1
@@ -0,0 +1,290 @@
1
+ # Default colorization module:
2
+ # Code taken from the (blackwinter) wirble gem, adjusted api. See COPYING for credits.
3
+
4
+ class << Ripl::ColorResult::Default = Module.new
5
+ def tokenize(str)
6
+ raise 'missing block' unless block_given?
7
+ chars = str.split(//)
8
+
9
+ # $stderr.puts "DEBUG: chars = #{chars.join(',')}"
10
+
11
+ state, val, i, lc = [], '', 0, nil
12
+ while i <= chars.size
13
+ repeat = false
14
+ c = chars[i]
15
+
16
+ # $stderr.puts "DEBUG: state = #{state}"
17
+
18
+ case state[-1]
19
+ when nil
20
+ case c
21
+ when ':'
22
+ state << :symbol
23
+ when '"'
24
+ state << :string
25
+ when '#'
26
+ state << :object
27
+ when /[a-z]/i
28
+ state << :keyword
29
+ repeat = true
30
+ when /[0-9-]/
31
+ state << :number
32
+ repeat = true
33
+ when '{'
34
+ yield :open_hash, '{'
35
+ when '['
36
+ yield :open_array, '['
37
+ when ']'
38
+ yield :close_array, ']'
39
+ when '}'
40
+ yield :close_hash, '}'
41
+ when /\s/
42
+ yield :whitespace, c
43
+ when ','
44
+ yield :comma, ','
45
+ when '>'
46
+ yield :refers, '=>' if lc == '='
47
+ when '.'
48
+ yield :range, '..' if lc == '.'
49
+ when '='
50
+ # ignore these, they're used elsewhere
51
+ nil
52
+ else
53
+ # $stderr.puts "DEBUG: ignoring char #{c}"
54
+ end
55
+ when :symbol
56
+ case c
57
+ # XXX: should have =, but that messes up foo=>bar
58
+ when /[a-z0-9_!?]/
59
+ val << c
60
+ else
61
+ yield :symbol_prefix, ':'
62
+ yield state[-1], val
63
+ state.pop; val = ''
64
+ repeat = true
65
+ end
66
+ when :string
67
+ case c
68
+ when '"'
69
+ if lc == "\\"
70
+ val[-1] = ?"
71
+ else
72
+ yield :open_string, '"'
73
+ yield state[-1], val
74
+ state.pop; val = ''
75
+ yield :close_string, '"'
76
+ end
77
+ else
78
+ val << c
79
+ end
80
+ when :keyword
81
+ case c
82
+ when /[a-z0-9_]/i
83
+ val << c
84
+ else
85
+ # is this a class?
86
+ st = val =~ /^[A-Z]/ ? :class : state[-1]
87
+
88
+ yield st, val
89
+ state.pop; val = ''
90
+ repeat = true
91
+ end
92
+ when :number
93
+ case c
94
+ when /[0-9e-]/
95
+ val << c
96
+ when '.'
97
+ if lc == '.'
98
+ val[/\.$/] = ''
99
+ yield state[-1], val
100
+ state.pop; val = ''
101
+ yield :range, '..'
102
+ else
103
+ val << c
104
+ end
105
+ else
106
+ yield state[-1], val
107
+ state.pop; val = ''
108
+ repeat = true
109
+ end
110
+ when :object
111
+ case c
112
+ when '<'
113
+ yield :open_object, '#<'
114
+ state << :object_class
115
+ when ':'
116
+ state << :object_addr
117
+ when '@'
118
+ state << :object_line
119
+ when '>'
120
+ yield :close_object, '>'
121
+ state.pop; val = ''
122
+ end
123
+ when :object_class
124
+ case c
125
+ when ':'
126
+ yield state[-1], val
127
+ state.pop; val = ''
128
+ repeat = true
129
+ else
130
+ val << c
131
+ end
132
+ when :object_addr
133
+ case c
134
+ when '>'
135
+ when '@'
136
+ yield :object_addr_prefix, ':'
137
+ yield state[-1], val
138
+ state.pop; val = ''
139
+ repeat = true
140
+ else
141
+ val << c
142
+ end
143
+ when :object_line
144
+ case c
145
+ when '>'
146
+ yield :object_line_prefix, '@'
147
+ yield state[-1], val
148
+ state.pop; val = ''
149
+ repeat = true
150
+ else
151
+ val << c
152
+ end
153
+ else
154
+ raise "unknown state #{state}"
155
+ end
156
+
157
+ unless repeat
158
+ i += 1
159
+ lc = c
160
+ end
161
+ end
162
+ end
163
+
164
+ #
165
+ # Terminal escape codes for colors.
166
+ #
167
+ COLORS = {
168
+ :nothing => '0;0',
169
+ :black => '0;30',
170
+ :red => '0;31',
171
+ :green => '0;32',
172
+ :brown => '0;33',
173
+ :blue => '0;34',
174
+ :cyan => '0;36',
175
+ :purple => '0;35',
176
+ :light_gray => '0;37',
177
+ :dark_gray => '1;30',
178
+ :light_red => '1;31',
179
+ :light_green => '1;32',
180
+ :yellow => '1;33',
181
+ :light_blue => '1;34',
182
+ :light_cyan => '1;36',
183
+ :light_purple => '1;35',
184
+ :white => '1;37',
185
+ }
186
+
187
+ #
188
+ # Return the escape code for a given color.
189
+ #
190
+ def get_color(key)
191
+ COLORS.key?(key) && "\033[#{COLORS[key]}m"
192
+ end
193
+
194
+ #
195
+ # Default Wirble color scheme.
196
+ #
197
+ DEFAULT_SCHEME = {
198
+ # delimiter colors
199
+ :comma => :blue,
200
+ :refers => :blue,
201
+
202
+ # container colors (hash and array)
203
+ :open_hash => :green,
204
+ :close_hash => :green,
205
+ :open_array => :green,
206
+ :close_array => :green,
207
+
208
+ # object colors
209
+ :open_object => :light_red,
210
+ :object_class => :white,
211
+ :object_addr_prefix => :blue,
212
+ :object_line_prefix => :blue,
213
+ :close_object => :light_red,
214
+
215
+ # symbol colors
216
+ :symbol => :yellow,
217
+ :symbol_prefix => :yellow,
218
+
219
+ # string colors
220
+ :open_string => :red,
221
+ :string => :cyan,
222
+ :close_string => :red,
223
+
224
+ # misc colors
225
+ :number => :cyan,
226
+ :keyword => :green,
227
+ :class => :light_green,
228
+ :range => :red,
229
+ }
230
+
231
+ #
232
+ # Fruity testing colors.
233
+ #
234
+ TESTING_SCHEME = {
235
+ :comma => :red,
236
+ :refers => :red,
237
+ :open_hash => :blue,
238
+ :close_hash => :blue,
239
+ :open_array => :green,
240
+ :close_array => :green,
241
+ :open_object => :light_red,
242
+ :object_class => :light_green,
243
+ :object_addr => :purple,
244
+ :object_line => :light_purple,
245
+ :close_object => :light_red,
246
+ :symbol => :yellow,
247
+ :symbol_prefix => :yellow,
248
+ :number => :cyan,
249
+ :string => :cyan,
250
+ :keyword => :white,
251
+ :range => :light_blue,
252
+ }
253
+
254
+ #
255
+ # Set color map to hash
256
+ #
257
+ def scheme=(hash)
258
+ @scheme = hash
259
+ end
260
+
261
+ #
262
+ # Get current color map
263
+ #
264
+ def scheme
265
+ @scheme ||= {}.update(DEFAULT_SCHEME)
266
+ end
267
+
268
+ #
269
+ # Return a string with the given color.
270
+ #
271
+ def colorize_string(str, color)
272
+ col, nocol = [color, :nothing].map { |key| get_color(key) }
273
+ col ? "#{col}#{str}#{nocol}" : str
274
+ end
275
+
276
+ #
277
+ # Colorize the results of inspect
278
+ #
279
+ def colorize_code(str)
280
+ ret, nocol = '', get_color(:nothing)
281
+ tokenize(str) do |tok, val|
282
+ ret << colorize_string(val, scheme[tok])
283
+ end
284
+ ret
285
+ rescue # catch any errors from the tokenizer (just in case)
286
+ str
287
+ end
288
+ end
289
+
290
+ # J-_-L
@@ -0,0 +1,36 @@
1
+ module Ripl
2
+ module ColorResult
3
+ VERSION = '0.1.0'
4
+
5
+ # def inspect_result(result)
6
+ # result.inspect
7
+ # end
8
+
9
+ def format_prompt(result)
10
+ @options[:result_prompt]
11
+ end
12
+
13
+ def format_result(result)
14
+ format_prompt(result) + inspect_result(result)
15
+ end
16
+
17
+ def inspect_result(result)
18
+ case (@options[:color_result_engine] ||= :default).to_sym
19
+ when :coderay
20
+ require 'coderay'
21
+ CodeRay.scan( result.inspect, :ruby ).term
22
+ when :ap, :awesome_print
23
+ require 'ap'
24
+ result.awesome_inspect
25
+ else # :default
26
+ require 'ripl/color_result/default'
27
+ Default.colorize_code( result.inspect )
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+
34
+ Ripl::Shell.send :include, Ripl::ColorResult if defined? Ripl::Shell
35
+
36
+ # J-_-L
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ripl-color_result
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jan Lelis
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-14 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: ripl
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 21
30
+ segments:
31
+ - 0
32
+ - 2
33
+ - 1
34
+ version: 0.2.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: This ripl plugin colorizes your result.
38
+ email: mail@janlelis.de
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.rdoc
45
+ - COPYING
46
+ files:
47
+ - lib/ripl/color_result/default.rb
48
+ - lib/ripl/color_result.rb
49
+ - README.rdoc
50
+ - CHANGELOG.rdoc
51
+ - deps.rip
52
+ - Rakefile
53
+ - .gemspec
54
+ - COPYING
55
+ has_rdoc: true
56
+ homepage: http://github.com/janlelis/ripl-color_result
57
+ licenses:
58
+ - MIT
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 23
79
+ segments:
80
+ - 1
81
+ - 3
82
+ - 6
83
+ version: 1.3.6
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.7
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: A ripl plugin to colorize result.
91
+ test_files: []
92
+