JoshCheek / seeing_is_believing

Displays the results of every line of code in your file
1.3k stars 54 forks source link

Handle multiline strings better #81

Closed JoshCheek closed 7 years ago

JoshCheek commented 7 years ago

I keep doing stuff like this

require 'rdoc'

options = RDoc::Options.new.tap { |o| o.pipe = true }
output  = RDoc::Markup::ToHtml.new options
input   = RDoc::Markup.new

input.convert(<<RDOC, output).lines
== FOO

rdoc-image:https://s3.amazonaws.com/josh.cheek/images/scratch/sib-example1.gif
RDOC
# => ["\n",
#     "<h2 id=\"label-FOO\">FOO</h2>\n",
#     "\n",
#     "<p><img\n",
#     "src=\"https://s3.amazonaws.com/josh.cheek/images/scratch/sib-example1.gif\"></p>\n"]

So that it will display the output across lines. It would be nice if the pretty printed string in this case would display better so that I don't have to turn it into an array of lines.

JoshCheek commented 7 years ago

I'm thinking about adding this, I'm mostly unsure about the representation (inspected strings with plusses to imply concatenation). Since I know @jcinnamond and @avdi use it, I'll solicit opinions from y'all :)

screenshot 2016-11-09 17 37 07

If you want to play with it, here's the code that does it, I've modified it since the screenshot, it now displays the newlines. I also might add some sort of a conditional to not run it unless PP is defined.

diff --git a/lib/seeing_is_believing/the_matrix.rb b/lib/seeing_is_believing/the_matrix.rb
index 0bb9410..e20df3d 100644
--- a/lib/seeing_is_believing/the_matrix.rb
+++ b/lib/seeing_is_believing/the_matrix.rb
@@ -2,6 +2,12 @@ require_relative 'version'
 require_relative 'safe'
 require_relative 'event_stream/producer'

+class String
+  def pretty_print(pp)
+    pp.text inspect.gsub(/\\n(?!")/, '\n" +'+"\n"+'"')
+  end
+end
+
 sib_vars     = Marshal.load ENV["SIB_VARIABLES.MARSHAL.B64"].unpack('m0').first
 event_stream = IO.open sib_vars.fetch(:event_stream_fd), "w"
 $SiB = SeeingIsBelieving::EventStream::Producer.new(event_stream)