Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Pry <#.*> coloring falls apart when it has multiple lines #717

Merged
1 commit merged into from Sep 26, 2012

Conversation

Projects
None yet
2 participants
Contributor

lloeki commented Sep 25, 2012

Take e.g this return value, made of an Array containing Rule instances:

=> [#<Rule:0x00000101d04650
  @chain_id=1,
  @filter_argument="tablet",
  @filter_method="client_class",
  @id=1,
  @order=1,
  @target="DROP">,
 #<Rule:0x00000101d03fe8
  @chain_id=2,
  @filter_argument="tablet",
  @filter_method="client_class",
  @id=2,
  @order=1,
  @target="DROP">,
 #<Rule:0x00000101d037a0
  @chain_id=2,
  @filter_argument=42,
  @filter_method="current_user",
  @id=3,
  @order=2,
  @resource_id=21,
  @target="ACCEPT">]

Pry will fail at coloring it consistently: the second and third objects will be colored in green, with <# and > delimiters in yellow, while the first object will have only its first line colored green, and the remainder with the default color. A single (and hence first) object outside an Array will show this behavior too, but this is less noticeable since it does not look so inconsistent.

The cause lies in this code in lib/pry.rb:

# avoid colour-leak from CodeRay and any of the users' previous output
colorized = colorized.sub(/(\n*)$/, "\e[0m\\1") if Pry.color

As $ matches the first end of line, this clears the color after the first newline. One should instead use \Z to match end of string, as such:

colorized = colorized.sub(/(\n*)\Z/, "\e[0m\\1") if Pry.color

The colored output is then correct.

@ghost

ghost commented Sep 25, 2012

This looks good. I'm not sure about the failures reported by Travis otherwise I'd merge happily.
/cc @ConradIrwin @banister @rf- @kyrylo

Owner

kyrylo commented Sep 25, 2012

Are you sure that you need to use the very \Z and not \z? You mentioned:

One should instead use \Z to match end of string, as such

http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UL

\z Matches the end of the string.
\Z Matches the end of the string unless the string ends with a \n, in which case it matches just before the \n.

Contributor

lloeki commented Sep 25, 2012

Well, * being greedy, this depends how the regex engine consumes \Z WRT *. According to the doc you cited it should indeed match before the last \n, but apparently it does not:

/(\n*)\Z(.*)/ === "\n\n\n"; [$1, $2] # => ["\n\n\n", ""]

Anyway you're right, and the code maps the intent much more this way.

Owner

kyrylo commented Sep 25, 2012

Could you squash your commits and rebase?

@ghost

ghost commented Sep 25, 2012

@kyrylo Any idea what is up with the Travis failures for JRuby?

@ghost ghost added a commit that referenced this pull request Sep 26, 2012

Robert Gleeson Merge pull request #717 from lloeki/fix_multiline_object_coloring
Pry <#.*> coloring falls apart when it has multiple lines
24d9e48

@ghost ghost merged commit 24d9e48 into pry:master Sep 26, 2012

1 check failed

default The Travis build failed
Details

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment