cute_print 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3c81ac7440cf8ecc4b2e2957630e8a8d103f1091
4
- data.tar.gz: 04d54e1adfc14c9ca499d2f267a1600c64962c9d
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzAxMjU2ZDA2ZjFlYjJmOThhOWZkYTlhZjMxNzVlYjI4ZGY2ZTNjMQ==
5
+ data.tar.gz: !binary |-
6
+ ODA2ZGNjNTQ0M2EzMTBlYmEyMTg2M2ZhZTg4NzEzOWM4MmE4Zjk5Nw==
5
7
  SHA512:
6
- metadata.gz: b8f5c915635c2e033d36af1bb55eea49b478e76e97e27993173e29055ae18f0084cc083cdeea0c188e148129f1b272b1ca119e1b5d941f6fae9ac8e8c1f02599
7
- data.tar.gz: eb6cdda8838c7261337e4c76a9549729fb7174fc20f6c882ddb459ffb2a5086e6a6ccc64202047fe2d661eb408cdc27d581c4870d292e2d7fe16c3af97e82bf7
8
+ metadata.gz: !binary |-
9
+ N2U5OGFkZTMwZDFiNTNhNjRlNjA3NTliNzM1OWFmMjg0ZGViZWRmZDE1MWE0
10
+ YzJiNjhmZmM2NDMzZWVkNWY1NzU2ZmVhODRiNzA2NWVlY2NmMzM5MjIyOTY0
11
+ NzBmMWFlYzRjZTRlOTgyYjRkMmMxMmM0ZmE3NTMzZmE0MjM3Mzc=
12
+ data.tar.gz: !binary |-
13
+ ZmU3NjFhOGNhODVkN2EwMWYwZDFhY2FlZDBiNzQwYjA3MDQxN2FiZjUwMGIy
14
+ ZWIzM2MzN2YxMzdlMGM4NTY5NWRhNWZjYjNlNjQwZjNlMjdjYzFhODAwNzA2
15
+ N2JlNGVjYTNhMDMyMDk2YWZjOWI1YzcwNWJmYjgzNDk5ZThkNGY=
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0
5
+ - 2.1
data/.yardopts CHANGED
@@ -4,3 +4,4 @@
4
4
  lib/**/*.rb
5
5
  -
6
6
  LICENSE
7
+ CHANGELOG.md
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ ### Development
2
+
3
+ Breaking changes:
4
+
5
+ * Configure.position_format removed
6
+ * Added Configure.location_format, which takes either :filename or
7
+ :path
8
+
9
+ Enhancements:
10
+
11
+ * Kernel#ql with no arguments prints source location
12
+ * Added Kernel#qq (pretty-print)
13
+ * Added Kernel#qql (pretty-print with source location)
14
+
15
+ ### 0.1.0 / 2014-09-04
16
+
17
+ Initial release
data/README.md CHANGED
@@ -1,13 +1,21 @@
1
1
  # cute_print
2
+ [![Gem Version](https://badge.fury.io/rb/cute_print.png)](http://badge.fury.io/rb/cute_print)
3
+ [![Dependency Status](https://gemnasium.com/wconrad/cute_print.svg)](https://gemnasium.com/wconrad/cute_print)
4
+ [![Build Status](https://travis-ci.org/wconrad/cute_print.png)](https://travis-ci.org/wconrad/cute_print)
5
+ [![Code Climate](https://codeclimate.com/github/wconrad/cute_print.png)](https://codeclimate.com/github/wconrad/cute_print)
2
6
 
3
- Write debug output to the console, with flair. Features:
7
+ Write debug output to stderr. Optionally print the source filename
8
+ and line number, or the source of the debug statement. Easily debug
9
+ the middle of a call chain.
4
10
 
5
- * Inspects its output, like Kernel#p
6
- * Writes to $stderr by default (good when $stdout is redirected)
7
- * Can print the filename and line number
8
- * Can print the source of the value
9
- * Can print a value in the middle of a call chain
10
- * Configurable output device
11
+ ## Features
12
+
13
+ * Inspects its output, like Kernel#p.
14
+ * Writes to $stderr by default (good when $stdout is redirected).
15
+ * Can print the filename and line number.
16
+ * Can print the source of the value.
17
+ * Can print a value in the middle of a call chain.
18
+ * Configurable output device.
11
19
 
12
20
  This is for those who prefer to debug by writing things to the
13
21
  console.
@@ -43,11 +51,15 @@ You can have the debug source printed along with the value:
43
51
  i = 1
44
52
  q {i + 2} # i + 2 is 3
45
53
 
46
- Kernel#ql will add the source position:
54
+ Kernel#ql will add the source location:
47
55
 
48
56
  ql "abc" # foo.rb:12: "abc"
49
57
  ql {1 + 2} # foo.rb:13: 1 + 2 is 3
50
58
 
59
+ When called with no arguments, #ql just prints the source location:
60
+
61
+ ql # foo.rb:14
62
+
51
63
  Call chains can be a pain to debug, but it's easy with Kernel#tapq and
52
64
  Kernel#tapql:
53
65
 
@@ -59,14 +71,15 @@ Kernel#tapql:
59
71
 
60
72
  ## Documentation
61
73
 
62
- [Full documentation (relishapp)][2]
74
+ There's more. Please see the [Full documentation][2] (relishapp).
63
75
 
64
76
  ## Rubies supported
65
77
 
66
78
  This gem is known to work with these Rubies:
67
79
 
68
- * ruby-1.9.3
69
- * ruby-2.1.2
80
+ * ruby-1.9.3-p547
81
+ * ruby-2.0.0-p481
82
+ * ruby-2.1.2p95
70
83
 
71
84
  ## Platforms supported
72
85
 
@@ -82,9 +95,15 @@ statement's source is derived from the _wrong_ gem.
82
95
 
83
96
  Differences between the _wrong_ gem and this gem:
84
97
 
98
+ * This gem can print the source location
99
+ * This gem writes to $stderr
100
+ * This gem's output is configurable
101
+ * This gem requires Ruby >= 1.9
102
+ * This gem debugs call chains
85
103
  * The _wrong_ gem supports color output
86
- * The _wrong_ gem writes only to stdout
104
+ * The _wrong_ gem writes to stdout
87
105
  * The _wrong_ gem supports Ruby 1.8
106
+ * The _wrong_ gem has assertions and more
88
107
 
89
108
  ## Versioning
90
109
 
@@ -105,5 +124,5 @@ version, e.g.:
105
124
  5. Create new Pull Request
106
125
 
107
126
  [1]: http://rubygems.org/gems/wrong
108
- [2]: https://www.relishapp.com/wconrad/cute-print/docs
127
+ [2]: https://www.relishapp.com/wconrad/cute-print/v/0-2-0/docs
109
128
  [3]: http://semver.org/spec/v2.0.0.html
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/cute_print.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: cute_print 0.1.0 ruby lib
5
+ # stub: cute_print 0.2.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "cute_print"
9
- s.version = "0.1.0"
9
+ s.version = "0.2.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Wayne Conrad"]
14
- s.date = "2014-09-02"
15
- s.description = "Write debug output to the console, with flair. Features: * Inspects its output, like Kernel#p * Writes to $stderr by default (good when $stdout is redirected) * Can print the filename and line number * Can print the source of the value * Can print a value in the middle of a call chain * Configurable output device This is for those who prefer to debug by writing things to the console."
14
+ s.date = "2014-09-05"
15
+ s.description = "Write debug output to stderr. Optionally print the source filename and line number, or the source of the debug statement. Easily debug the middle of a call chain."
16
16
  s.email = "wconrad@yagni.com"
17
17
  s.extra_rdoc_files = [
18
18
  "LICENSE",
@@ -21,25 +21,29 @@ Gem::Specification.new do |s|
21
21
  s.files = [
22
22
  ".config/cucumber.yml",
23
23
  ".rspec",
24
+ ".travis.yml",
24
25
  ".yardopts",
26
+ "CHANGELOG.md",
25
27
  "Gemfile",
26
28
  "Gemfile.lock",
27
29
  "LICENSE",
28
30
  "README.md",
29
31
  "Rakefile",
30
32
  "VERSION",
31
- "basic101.gemspec",
32
33
  "cute_print.gemspec",
33
34
  "features/.nav",
35
+ "features/call_chain.feature",
34
36
  "features/configuring/configure_output.feature",
35
37
  "features/configuring/configure_position_format.feature",
36
38
  "features/configuring/readme.md",
37
39
  "features/configuring/reset_configuration.feature",
38
- "features/inspect_call_chain.feature",
39
- "features/inspect_objects/inspect.feature",
40
- "features/inspect_objects/label_and_inspect.feature",
41
- "features/inspect_objects/print_source_location.feature",
42
- "features/inspect_objects/readme.md",
40
+ "features/inspect/inspect.feature",
41
+ "features/inspect/inspect_with_location.feature",
42
+ "features/inspect/inspect_with_source.feature",
43
+ "features/inspect/readme.md",
44
+ "features/pretty_print/pretty_print.feature",
45
+ "features/pretty_print/pretty_print_with_location.feature",
46
+ "features/pretty_print/pretty_print_with_source.feature",
43
47
  "features/readme.md",
44
48
  "features/support/env.rb",
45
49
  "features/support/helpers/example.rb",
@@ -51,6 +55,8 @@ Gem::Specification.new do |s|
51
55
  "lib/cute_print/core_ext/object.rb",
52
56
  "lib/cute_print/default_printer.rb",
53
57
  "lib/cute_print/finds_foreign_caller.rb",
58
+ "lib/cute_print/formatter.rb",
59
+ "lib/cute_print/location.rb",
54
60
  "lib/cute_print/mixin.rb",
55
61
  "lib/cute_print/printer.rb",
56
62
  "lib/cute_print/ruby_generator.rb",
data/features/.nav CHANGED
@@ -1,6 +1,10 @@
1
- - inspect_objects:
1
+ - inspect:
2
2
  - inspect.feature
3
- - label_and_inspect.feature
4
- - print_source_location.feature
5
- - inspect_call_chain.feature
3
+ - inspect_with_source.feature
4
+ - inspect_with_location.feature
5
+ - pretty_print:
6
+ - pretty_print.feature
7
+ - pretty_print_with_source.feature
8
+ - pretty_print_with_location.feature
9
+ - call_chain.feature
6
10
  - configuring:
@@ -1,30 +1,40 @@
1
- Feature: Configure position format
1
+ Feature: Configure location format
2
2
 
3
3
  You can change the format which Kernel#ql uses to print the source
4
- position. String#% is called on the string you supply, with
5
- a hash argument having these keys:
4
+ location. By default, just the filename is printed, but you can
5
+ cause the full path. to be printed instead.
6
6
 
7
- * :path
8
- * :filename
9
- * :line_number
7
+ Scenario: Write full path
8
+ Given a file named "example.rb" with:
9
+ """
10
+ require "cute_print"
11
+
12
+ CutePrint.configure do |c|
13
+ c.location_format = :path
14
+ end
10
15
 
11
- The default position format is:
16
+ ql 123
17
+ """
18
+ Then stderr should be
19
+ """
20
+ /tmp/.../example.rb:7:
21
+ 123
12
22
 
13
- "%<filename>s:%<line_number>d: "
23
+ """
14
24
 
15
- Scenario: Write full path
25
+ Scenario: Write filename
16
26
  Given a file named "example.rb" with:
17
27
  """
18
28
  require "cute_print"
19
29
 
20
30
  CutePrint.configure do |c|
21
- c.position_format = "%<path>s:%<line_number>d: "
31
+ c.location_format = :filename
22
32
  end
23
33
 
24
34
  ql 123
25
35
  """
26
36
  Then stderr should be
27
37
  """
28
- /tmp/.../example.rb:7: 123
38
+ example.rb:7: 123
29
39
 
30
- """
40
+ """
@@ -7,10 +7,7 @@ Feature: Reset configuration
7
7
  """
8
8
  require "cute_print"
9
9
 
10
- CutePrint.configure do |c|
11
- c.out = $stdout
12
- end
13
-
10
+ CutePrint.configure { |c| c.out = $stdout }
14
11
  q "abc"
15
12
  CutePrint.configure { |c| c.reset }
16
13
  q 123
@@ -1,4 +1,4 @@
1
- Feature: Inspect an object
1
+ Feature: Inspect
2
2
 
3
3
  Kernel#q inspects its arguments and prints them, one per line. This
4
4
  is just like Kernel#p, except that it writes to $stderr by default.
@@ -1,7 +1,8 @@
1
- Feature: Print the source location
1
+ Feature: Inspect with source location
2
2
 
3
- Kernel#ql works just like Kernel#q, except that it also prints the
4
- source location
3
+ Kernel#ql works just like Kernel#pp from the built-in pp library,
4
+ except that it writes to $stderr, _and_ it prints the source
5
+ location.
5
6
 
6
7
  Scenario: Inspect an object
7
8
  Given a file named "example.rb" with:
@@ -28,7 +29,7 @@ Feature: Print the source location
28
29
 
29
30
  """
30
31
 
31
- Scenario: Label and inspect and object
32
+ Scenario: Inspect with location
32
33
  Given a file named "example.rb" with:
33
34
  """
34
35
  require "cute_print"
@@ -39,3 +40,15 @@ Feature: Print the source location
39
40
  example.rb:2: (1 + 2) is 3
40
41
 
41
42
  """
43
+
44
+ Scenario: Just print the location
45
+ Given a file named "example.rb" with:
46
+ """
47
+ require "cute_print"
48
+ ql
49
+ """
50
+ Then stderr should be
51
+ """
52
+ example.rb:2
53
+
54
+ """
@@ -1,9 +1,9 @@
1
- Feature: Label and inspect an object
1
+ Feature: Inspect with source
2
2
 
3
3
  Kernel#q, if given a block, prints the source for the block and then
4
4
  the inspected result of running the block.
5
5
 
6
- Scenario: Label and inspect and object
6
+ Scenario: Label and inspect an object
7
7
  Given a file with:
8
8
  """
9
9
  require "cute_print"
@@ -0,0 +1,41 @@
1
+ Feature: Pretty-print
2
+
3
+ Kernel#qq pretty-prints its arguments and prints them, one per line. This
4
+ is just like Kernel#pp from the pp library, except that it writes to $stderr
5
+ by default.
6
+
7
+ Scenario: One argument
8
+ Given a file with:
9
+ """
10
+ require "cute_print"
11
+ a = [
12
+ "Once upon a time there were four little Rabbits, and their names were",
13
+ "Flopsy,",
14
+ "Mopsy,",
15
+ "Cotton-tail, and",
16
+ "Peter.",
17
+ ]
18
+ qq a
19
+ """
20
+ Then stderr should be
21
+ """
22
+ ["Once upon a time there were four little Rabbits, and their names were",
23
+ "Flopsy,",
24
+ "Mopsy,",
25
+ "Cotton-tail, and",
26
+ "Peter."]
27
+
28
+ """
29
+
30
+ Scenario: Two arguments
31
+ Given a file with:
32
+ """
33
+ require "cute_print"
34
+ qq 1, "foo"
35
+ """
36
+ Then stderr should be
37
+ """
38
+ 1
39
+ "foo"
40
+
41
+ """
@@ -0,0 +1,73 @@
1
+ Feature: Pretty-print with source location
2
+
3
+ Kernel#qql works just like Kernel#pp from the built-in pp library,
4
+ except that it writes to $stderr _and_ it labels the output with
5
+ the source location.
6
+
7
+ If the filename is long, then the source location is printed on a
8
+ line of its own.
9
+
10
+ Scenario: One argument
11
+ Given a file named "example.rb" with:
12
+ """
13
+ require "cute_print"
14
+ a = [
15
+ "Once upon a time there were four little Rabbits, ",
16
+ "and their names were",
17
+ "Flopsy,",
18
+ "Mopsy,",
19
+ "Cotton-tail, and",
20
+ "Peter.",
21
+ ]
22
+ qql a
23
+ """
24
+ Then stderr should be
25
+ """
26
+ example.rb:10: ["Once upon a time there were four little Rabbits, ",
27
+ "and their names were",
28
+ "Flopsy,",
29
+ "Mopsy,",
30
+ "Cotton-tail, and",
31
+ "Peter."]
32
+
33
+ """
34
+
35
+ Scenario: Two arguments
36
+ Given a file named "example.rb" with:
37
+ """
38
+ require "cute_print"
39
+ qql 1, "foo"
40
+ """
41
+ Then stderr should be
42
+ """
43
+ example.rb:2: 1
44
+ example.rb:2: "foo"
45
+
46
+ """
47
+
48
+ Scenario: Very long label
49
+
50
+ Given a file named "long____________________________filename.rb" with:
51
+ """
52
+ require "cute_print"
53
+ a = [
54
+ "Once upon a time there were four little Rabbits, ",
55
+ "and their names were",
56
+ "Flopsy,",
57
+ "Mopsy,",
58
+ "Cotton-tail, and",
59
+ "Peter.",
60
+ ]
61
+ qql a
62
+ """
63
+ Then stderr should be
64
+ """
65
+ long____________________________filename.rb:10:
66
+ ["Once upon a time there were four little Rabbits, ",
67
+ "and their names were",
68
+ "Flopsy,",
69
+ "Mopsy,",
70
+ "Cotton-tail, and",
71
+ "Peter."]
72
+
73
+ """
@@ -0,0 +1,27 @@
1
+ Feature: Pretty-print with source
2
+
3
+ Kernel#pp, if given a block, prints the source for the block and then
4
+ the pretty-printed result of running the block.
5
+
6
+ Scenario: Label and pretty-print an object
7
+ Given a file with:
8
+ """
9
+ require "cute_print"
10
+ a = [
11
+ "Once upon a time there were four little Rabbits, and their names were",
12
+ "Flopsy,",
13
+ "Mopsy,",
14
+ "Cotton-tail, and",
15
+ "Peter.",
16
+ ]
17
+ qq {a}
18
+ """
19
+ Then stderr should be
20
+ """
21
+ a is ["Once upon a time there were four little Rabbits, and their names were",
22
+ "Flopsy,",
23
+ "Mopsy,",
24
+ "Cotton-tail, and",
25
+ "Peter."]
26
+
27
+ """
@@ -22,10 +22,10 @@ module CutePrint
22
22
  def_delegator :@printer, "#{name}="
23
23
  end
24
24
 
25
- # @!attribute [rw] position_format
26
- # @return [String] The position format
27
- # @see Printer#position_format
28
- delegate_accessor :position_format
25
+ # @!attribute [rw] location_format
26
+ # @return [String] The location format
27
+ # @see Printer#location_format
28
+ delegate_accessor :location_format
29
29
 
30
30
  # @!attribute [rw] out
31
31
  # @return [#puts] The file to write to
@@ -3,6 +3,8 @@ module CutePrint
3
3
  # @api private
4
4
  module FindsForeignCaller
5
5
 
6
+ extend self
7
+
6
8
  def nearest_foreign_caller
7
9
  caller.find do |s|
8
10
  path = s.split(":").first
@@ -11,7 +13,7 @@ module CutePrint
11
13
  end
12
14
 
13
15
  def lib_path
14
- File.dirname(__FILE__)
16
+ File.join(File.dirname(__FILE__), '..')
15
17
  end
16
18
 
17
19
  end
@@ -0,0 +1,150 @@
1
+ require "pp"
2
+ require "stringio"
3
+
4
+ require_relative "location"
5
+ require_relative "ruby_parser"
6
+
7
+ module CutePrint
8
+ # @api private
9
+ class Formatter
10
+
11
+ DEFAULT_WIDTH = 79
12
+ DEFAULT_MAX_INLINE_LABEL_WIDTH = 40
13
+
14
+ def initialize(opts = {})
15
+ @inspector = :inspect
16
+ @method = opts.fetch(:method)
17
+ @out = opts.fetch(:out)
18
+ @block = opts.fetch(:block, nil)
19
+ @values = opts.fetch(:values, [])
20
+ @width = opts.fetch(:width, DEFAULT_WIDTH)
21
+ @max_inline_label_width = opts.fetch(:max_inline_label_width,
22
+ DEFAULT_MAX_INLINE_LABEL_WIDTH)
23
+ if @block && !@values.empty?
24
+ raise ArgumentError, "arguments and block are mutually exclusive"
25
+ end
26
+ end
27
+
28
+ def write
29
+ if labeled_values.empty? && !label.empty?
30
+ write_line label.chomp(": ")
31
+ else
32
+ labeled_values.each do |lines|
33
+ write_lines lines
34
+ end
35
+ end
36
+ end
37
+
38
+ def inspected_values
39
+ values.map do |value|
40
+ @inspector.call(value, value_width)
41
+ end
42
+ end
43
+
44
+ def labeled_values
45
+ if label_width > @max_inline_label_width
46
+ outline_labeled_values
47
+ else
48
+ inline_labeled_values
49
+ end
50
+ end
51
+
52
+ def inline_labeled_values
53
+ labeled_values = inspected_values.map do |lines|
54
+ lines.map.with_index do |line, i|
55
+ if i == 0
56
+ label + line
57
+ else
58
+ indent + line
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def outline_labeled_values
65
+ inspected_values.map do |lines|
66
+ [label.rstrip] + lines.map do |line|
67
+ " " + line
68
+ end
69
+ end
70
+ end
71
+
72
+ def with_location(format_key)
73
+ @location_format = LOCATION_FORMATS.fetch(format_key) {
74
+ raise ArgumentError, "Unknown location format: #{format_key.inspect}"
75
+ }
76
+ @location = Location.find
77
+ end
78
+
79
+ def inspect
80
+ @inspector = ->(value, value_width) do
81
+ [value.inspect]
82
+ end
83
+ end
84
+
85
+ def pretty_print
86
+ @inspector = ->(value, value_width) do
87
+ out = StringIO.new
88
+ PP.pp(value, out, value_width)
89
+ out.string.lines
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ LOCATION_FORMATS = {
96
+ filename: "%<filename>s:%<line_number>d: ",
97
+ path: "%<path>s:%<line_number>d: ",
98
+ }
99
+ private_constant :LOCATION_FORMATS
100
+
101
+ def value_width
102
+ @width - label_width
103
+ end
104
+
105
+ def values
106
+ if @block
107
+ [@block.call]
108
+ else
109
+ @values
110
+ end
111
+ end
112
+
113
+ def write_lines(lines)
114
+ lines.each do |line|
115
+ write_line line
116
+ end
117
+ end
118
+
119
+ def write_line(line)
120
+ @out.puts line
121
+ end
122
+
123
+ def label
124
+ @label ||= make_label
125
+ end
126
+
127
+ def label_width
128
+ label.size
129
+ end
130
+
131
+ def indent
132
+ " " * label_width
133
+ end
134
+
135
+ def make_label
136
+ [
137
+ (@location.format(@location_format) if @location),
138
+ ("#{block_code} is " if @block),
139
+ ].compact.join
140
+ end
141
+
142
+ def block_code
143
+ ruby_parser = RubyParser.from_block(@block)
144
+ parsed_code = ruby_parser.parse
145
+ method_call = parsed_code.first_call_to_method(@method)
146
+ method_call.block.to_ruby
147
+ end
148
+
149
+ end
150
+ end
@@ -0,0 +1,34 @@
1
+ require_relative "finds_foreign_caller"
2
+
3
+ module CutePrint
4
+ class Location
5
+
6
+ extend FindsForeignCaller
7
+
8
+ def self.find
9
+ path, line_number = nearest_foreign_caller.split(':')
10
+ line_number = line_number.to_i
11
+ new(path, line_number)
12
+ end
13
+
14
+ def initialize(path, line_number)
15
+ @path = path
16
+ @line_number = line_number
17
+ end
18
+
19
+ def format(template)
20
+ template % {
21
+ path: @path,
22
+ filename: filename,
23
+ line_number: @line_number,
24
+ }
25
+ end
26
+
27
+ private
28
+
29
+ def filename
30
+ File.basename(@path)
31
+ end
32
+
33
+ end
34
+ end
@@ -7,7 +7,6 @@ module CutePrint
7
7
  #
8
8
  # @note The methods in this module are part of the public API, but
9
9
  # the module itself is not.
10
-
11
10
  module Mixin
12
11
 
13
12
  # @see Printer#q
@@ -25,6 +24,11 @@ module CutePrint
25
24
  CutePrint::DefaultPrinter.printer.qq(*args, &block)
26
25
  end
27
26
 
27
+ # @see Printer#qql
28
+ def qql(*args, &block)
29
+ CutePrint::DefaultPrinter.printer.qql(*args, &block)
30
+ end
31
+
28
32
  # Debug a call chain by printing self and then returning self.
29
33
  # @return [Object] self
30
34
  def tapq
@@ -1,5 +1,4 @@
1
- require_relative "finds_foreign_caller"
2
- require_relative "ruby_parser"
1
+ require_relative "formatter"
3
2
  require_relative "stderr_out"
4
3
 
5
4
  module CutePrint
@@ -7,23 +6,18 @@ module CutePrint
7
6
 
8
7
  include FindsForeignCaller
9
8
 
10
- DEFAULT_POSITION_FORMAT = "%<filename>s:%<line_number>d: "
11
-
12
9
  # The object to write to. Defaults to $stderr.
13
10
  # @return [#puts]
14
11
  attr_accessor :out
15
12
 
16
- # The position format. To format a position, String#% is called
17
- # with a hash having these keys:
13
+ # The location format.
18
14
  #
19
- # * :path
15
+ # One of:
20
16
  # * :filename
21
- # * :line_number
17
+ # * :path
22
18
  #
23
- # The default format prints the filename and line number.
24
- # @see Printer::DEFAULT_POSITION_FORMAT
25
19
  # @return [String]
26
- attr_accessor :position_format
20
+ attr_accessor :location_format
27
21
 
28
22
  # Create an instance. If attributes are supplied, they override
29
23
  # the defaults. For example:
@@ -39,7 +33,7 @@ module CutePrint
39
33
  # Set all attributes to their defaults.
40
34
  def set_defaults
41
35
  @out = StderrOut.new
42
- @position_format = DEFAULT_POSITION_FORMAT
36
+ @location_format = :filename
43
37
  end
44
38
 
45
39
  # Inspect and write one or more objects.
@@ -50,12 +44,17 @@ module CutePrint
50
44
  # If called with a block, prints the source code of the block and
51
45
  # the inspected result of the block.
52
46
  def q(*values, &block)
53
- print(__method__, values, block) do |line|
54
- @out.puts line
55
- end
47
+ formatter = Formatter.new(
48
+ method: __method__,
49
+ out: @out,
50
+ block: block,
51
+ values: values
52
+ )
53
+ formatter.inspect
54
+ formatter.write
56
55
  end
57
56
 
58
- # Inspect and write one or more objects, with source position.
57
+ # Inspect and write one or more objects, with source location.
59
58
  #
60
59
  # If called without a block, prints the inspected arguments, one
61
60
  # on a line.
@@ -63,44 +62,52 @@ module CutePrint
63
62
  # If called with a block, prints the source code of the block and
64
63
  # the inspected result of the block.
65
64
  def ql(*values, &block)
66
- path, line_number = nearest_foreign_caller.split(":")
67
- line_number = line_number.to_i
68
- print(__method__, values, block) do |line|
69
- position = format_position(path, line_number)
70
- @out.puts "#{position}#{line}"
71
- end
65
+ formatter = Formatter.new(
66
+ method: __method__,
67
+ out: @out,
68
+ block: block,
69
+ values: values)
70
+ formatter.inspect
71
+ formatter.with_location @location_format
72
+ formatter.write
72
73
  end
73
74
 
74
- private
75
-
76
- def print(method, values, block)
77
- if block && !values.empty?
78
- raise ArgumentError, "arguments and block are mutually exclusive"
79
- end
80
- if block
81
- ruby_parser = RubyParser.from_block(block)
82
- parsed_code = ruby_parser.parse
83
- method_call = parsed_code.first_call_to_method(method)
84
- block_code = method_call.block.to_ruby
85
- yield "%s is %s" % [
86
- block_code,
87
- block.call.inspect,
88
- ]
89
- else
90
- values.each do |value|
91
- yield value.inspect
92
- end
93
- end
75
+ # Pretty-print and write one or more objects.
76
+ #
77
+ # If called without a block, pretty-prints the pretty-printed
78
+ # arguments, one on a line.
79
+ #
80
+ # If called with a block, prints the source code of the block and
81
+ # pretty-prints the result of the block.
82
+ def qq(*values, &block)
83
+ formatter = Formatter.new(
84
+ method: __method__,
85
+ out: @out,
86
+ block: block,
87
+ values: values)
88
+ formatter.pretty_print
89
+ formatter.write
94
90
  end
95
91
 
96
- def format_position(path, line_number)
97
- position_values = {
98
- path: path,
99
- filename: File.basename(path),
100
- line_number: line_number,
101
- }
102
- @position_format % position_values
92
+ # Pretty-print and write one or more objects, with source location.
93
+ #
94
+ # If called without a block, pretty-prints the pretty-printed
95
+ # arguments, one on a line.
96
+ #
97
+ # If called with a block, prints the source code of the block and
98
+ # pretty-prints the result of the block.
99
+ def qql(*values, &block)
100
+ formatter = Formatter.new(
101
+ method: __method__,
102
+ out: @out,
103
+ block: block,
104
+ values: values)
105
+ formatter.pretty_print
106
+ formatter.with_location @location_format
107
+ formatter.write
103
108
  end
104
109
 
110
+ private
111
+
105
112
  end
106
113
  end
@@ -3,6 +3,7 @@ module CutePrint
3
3
  # Writing to an instance of this class, rather than directly to
4
4
  # $stderr, allows the tests to capture output by assigning to
5
5
  # $stderr.
6
+ # @api private
6
7
  class StderrOut
7
8
 
8
9
  def puts(*args)
@@ -53,6 +53,31 @@ describe CutePrint do
53
53
  Then { @stderr == "#{@location}: [1, 2]\n" }
54
54
  end
55
55
 
56
+ describe "#qq" do
57
+ Given(:object) do
58
+ [
59
+ "Once upon a time there were four little Rabbits, and their names were",
60
+ "Flopsy,",
61
+ "Mopsy,",
62
+ "Cotton-tail, and",
63
+ "Peter.",
64
+ ]
65
+ end
66
+ Given(:expected_output) do
67
+ %Q'["Once upon a time there were four little Rabbits, and their names were",\n' +
68
+ %Q' "Flopsy,",\n' +
69
+ %Q' "Mopsy,",\n' +
70
+ %Q' "Cotton-tail, and",\n' +
71
+ %Q' "Peter."]\n'
72
+ end
73
+ When do
74
+ @stderr = capture_stderr do
75
+ qq object
76
+ end
77
+ end
78
+ Then { @stderr == expected_output }
79
+ end
80
+
56
81
  describe 'configure output' do
57
82
  Given(:io) { StringIO.new }
58
83
  Given do
data/tasks/jeweler.rake CHANGED
@@ -6,11 +6,21 @@ def readme
6
6
  File.read(File.join(File.dirname(__FILE__), '../README.md'))
7
7
  end
8
8
 
9
+ def remove_badges(s)
10
+ s.gsub(/^\[![^\n]+\n/, '')
11
+ end
12
+
13
+ def join_lines(s)
14
+ s.gsub(/\n/, ' ').strip
15
+ end
16
+
9
17
  def description
10
18
  unless (desc = readme[/\A#[^\n]*\n*(.*?)\n*^#/m, 1])
11
19
  raise "Failed to extract description from readme"
12
20
  end
13
- desc.gsub(/\n/, ' ').strip
21
+ desc = remove_badges(desc)
22
+ desc = join_lines(desc)
23
+ desc
14
24
  end
15
25
 
16
26
  Jeweler::Tasks.new do |gem|
metadata CHANGED
@@ -1,48 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cute_print
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wayne Conrad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-02 00:00:00.000000000 Z
11
+ date: 2014-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby_parser
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ruby2ruby
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '2.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.1'
41
- description: 'Write debug output to the console, with flair. Features: * Inspects
42
- its output, like Kernel#p * Writes to $stderr by default (good when $stdout is redirected)
43
- * Can print the filename and line number * Can print the source of the value * Can
44
- print a value in the middle of a call chain * Configurable output device This is
45
- for those who prefer to debug by writing things to the console.'
41
+ description: Write debug output to stderr. Optionally print the source filename and
42
+ line number, or the source of the debug statement. Easily debug the middle of a
43
+ call chain.
46
44
  email: wconrad@yagni.com
47
45
  executables: []
48
46
  extensions: []
@@ -50,27 +48,31 @@ extra_rdoc_files:
50
48
  - LICENSE
51
49
  - README.md
52
50
  files:
53
- - ".config/cucumber.yml"
54
- - ".rspec"
55
- - ".yardopts"
51
+ - .config/cucumber.yml
52
+ - .rspec
53
+ - .travis.yml
54
+ - .yardopts
55
+ - CHANGELOG.md
56
56
  - Gemfile
57
57
  - Gemfile.lock
58
58
  - LICENSE
59
59
  - README.md
60
60
  - Rakefile
61
61
  - VERSION
62
- - basic101.gemspec
63
62
  - cute_print.gemspec
64
63
  - features/.nav
64
+ - features/call_chain.feature
65
65
  - features/configuring/configure_output.feature
66
66
  - features/configuring/configure_position_format.feature
67
67
  - features/configuring/readme.md
68
68
  - features/configuring/reset_configuration.feature
69
- - features/inspect_call_chain.feature
70
- - features/inspect_objects/inspect.feature
71
- - features/inspect_objects/label_and_inspect.feature
72
- - features/inspect_objects/print_source_location.feature
73
- - features/inspect_objects/readme.md
69
+ - features/inspect/inspect.feature
70
+ - features/inspect/inspect_with_location.feature
71
+ - features/inspect/inspect_with_source.feature
72
+ - features/inspect/readme.md
73
+ - features/pretty_print/pretty_print.feature
74
+ - features/pretty_print/pretty_print_with_location.feature
75
+ - features/pretty_print/pretty_print_with_source.feature
74
76
  - features/readme.md
75
77
  - features/support/env.rb
76
78
  - features/support/helpers/example.rb
@@ -82,6 +84,8 @@ files:
82
84
  - lib/cute_print/core_ext/object.rb
83
85
  - lib/cute_print/default_printer.rb
84
86
  - lib/cute_print/finds_foreign_caller.rb
87
+ - lib/cute_print/formatter.rb
88
+ - lib/cute_print/location.rb
85
89
  - lib/cute_print/mixin.rb
86
90
  - lib/cute_print/printer.rb
87
91
  - lib/cute_print/ruby_generator.rb
@@ -113,12 +117,12 @@ require_paths:
113
117
  - lib
114
118
  required_ruby_version: !ruby/object:Gem::Requirement
115
119
  requirements:
116
- - - ">="
120
+ - - ! '>='
117
121
  - !ruby/object:Gem::Version
118
122
  version: '0'
119
123
  required_rubygems_version: !ruby/object:Gem::Requirement
120
124
  requirements:
121
- - - ">="
125
+ - - ! '>='
122
126
  - !ruby/object:Gem::Version
123
127
  version: '0'
124
128
  requirements: []
data/basic101.gemspec DELETED
@@ -1,64 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
- # stub: basic101 0.1.0 ruby lib
6
-
7
- Gem::Specification.new do |s|
8
- s.name = "basic101"
9
- s.version = "0.1.0"
10
-
11
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
- s.require_paths = ["lib"]
13
- s.authors = ["Wayne Conrad"]
14
- s.date = "2014-08-29"
15
- s.description = "Write debug output to stderr. Optionally print the source filename and line number, or the source of the debug statement. Easily print debug from the middle of call chains. This is for those who prefer to debug by writing things to the console. Since it writes to stderr, it's more useful than Kernel#p when working with a program that is run with stdout redirection. Output goes to $stderr by default, but that's configurable."
16
- s.email = "wconrad@yagni.com"
17
- s.extra_rdoc_files = [
18
- "LICENSE.txt",
19
- "README.md"
20
- ]
21
- s.files = [
22
- ".document",
23
- ".rspec",
24
- "Gemfile",
25
- "LICENSE.txt",
26
- "Rakefile",
27
- "VERSION",
28
- "lib/cute_print.rb",
29
- "spec/cute_print_spec.rb",
30
- "spec/spec_helper.rb"
31
- ]
32
- s.homepage = "http://github.com/wconrad/basic101"
33
- s.licenses = ["MIT"]
34
- s.rubygems_version = "2.2.2"
35
- s.summary = "print debug to stderr, with flair"
36
-
37
- if s.respond_to? :specification_version then
38
- s.specification_version = 4
39
-
40
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
- s.add_development_dependency(%q<bundler>, ["~> 1.7"])
42
- s.add_development_dependency(%q<jeweler>, ["~> 2.0"])
43
- s.add_development_dependency(%q<rake>, ["~> 10.3"])
44
- s.add_development_dependency(%q<redcarpet>, ["~> 3.1"])
45
- s.add_development_dependency(%q<rspec>, ["~> 3.0"])
46
- s.add_development_dependency(%q<yard>, ["~> 0.8.7"])
47
- else
48
- s.add_dependency(%q<bundler>, ["~> 1.7"])
49
- s.add_dependency(%q<jeweler>, ["~> 2.0"])
50
- s.add_dependency(%q<rake>, ["~> 10.3"])
51
- s.add_dependency(%q<redcarpet>, ["~> 3.1"])
52
- s.add_dependency(%q<rspec>, ["~> 3.0"])
53
- s.add_dependency(%q<yard>, ["~> 0.8.7"])
54
- end
55
- else
56
- s.add_dependency(%q<bundler>, ["~> 1.7"])
57
- s.add_dependency(%q<jeweler>, ["~> 2.0"])
58
- s.add_dependency(%q<rake>, ["~> 10.3"])
59
- s.add_dependency(%q<redcarpet>, ["~> 3.1"])
60
- s.add_dependency(%q<rspec>, ["~> 3.0"])
61
- s.add_dependency(%q<yard>, ["~> 0.8.7"])
62
- end
63
- end
64
-
File without changes