Repository navigation
Fix pp of an Array of messages - #71
Merged
Merged
Conversation
Message#pretty_print writes straight to q.output so IRB's
ColorPrinter can't escape our ANSI codes. Inside an Array, though, PP
is still buffering the "[" and the separators between elements, so
`pp chat.messages` printed every message first and every comma last:
`[{...},{...}{...} , ]`. Flushing first puts the buffered text out
ahead of each message. Items#pretty_print gets the same fix.
New specs pin the exact plain-text layout of Chat#inspect and of an
Array of messages. Lessons show this output verbatim, and until now
the specs only checked that inspect returned a String.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pp chat.messages, and IRB's echo ofchat.messages, garbled the separators:AI::Message#pretty_printwrites directly toq.outputso that IRB'sColorPrintercan't re-colorize (and escape) amazing_print's ANSI codes. Inside an Array, PP is still holding[and the,separators in its buffer, so each message's direct write lands ahead of them.q.flushfirst emits what PP has buffered, so the output becomes:This matches
p chat.messages.AI::Items#pretty_printhad the same direct write and gets the same fix.Specs
Until now the formatting specs only checked that
inspectreturns a String. This adds exact-layout specs for:Chat#inspect: a new chat, and a proxy-mode chat with messages (the output the AI::Chat Examples lesson shows verbatim).ppof an Array of messages, and of an Array ofItems.With the
lib/change reverted, exactly the two Array specs fail. The output is identical on Ruby 3.3.11 and 4.0.1: amazing_print 2.0 printsrole:style keys on both.Testing
rspec spec/unit: 101 examples, 0 failures on Ruby 3.3.11 and 4.0.1.a.messageswith--nocolorizeand with colors forced on: separators correct, ANSI codes kept and not escaped.standardrbclean.Follow-up worth considering, not in this PR: in
Chat#inspect, the closing]of@messagesprints at column 0 instead of under@messages. The new spec pins that layout as it is today, so fixing it later means updating the spec.