Skip to content

[printer] Add write method to print without a newline #587

New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

juharris
Copy link

@juharris juharris commented Mar 21, 2025

Helps when writing in a frame.

Examples:
image

It's helpful because existing methods to write don't work well in frames because the beginning bar is missing. This still isn't great, but it's much better:
image

Bump to version 2.4.0.

Bump Ruby version to 3.3.1.

@juharris juharris marked this pull request as ready for review March 21, 2025 18:31
@juharris juharris requested a review from a team as a code owner March 21, 2025 18:31
Copy link
Contributor

@joshheinrichs-shopify joshheinrichs-shopify left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like the idea of supporting this sort of thing, but this example doesnt seem to work very well

#!/usr/bin/env ruby
# typed: true

require('cli/ui')

CLI::UI.frame_style = :bracket
CLI::UI::StdoutRouter.enable
CLI::UI::Frame.open('Frame 2') do
  30.times do
    CLI::UI.write('test')
  end
end
CLI::UI::Frame.open('Frame 3') do
  CLI::UI.write('test' * 20)
end
CLI::UI::Frame.open('Frame 4') do
  CLI::UI.write('test' * 30)
  CLI::UI.write("\n")
end

image

i guess i'd expect closing out the frame to inject a newline? not sure.. we've kind of avoided this up until now because of these sorts of problems. i haven't looked at how easy that would be to do. you could consider doing this manually via CLI::UI.raw e.g.

CLI::UI::Frame.open('Frame 1') do
  CLI::UI::Frame.open('Frame 2') do
    CLI::UI.raw do
      $stdout.write(CLI::UI::Frame.prefix)
      $stdout.write('test' * 30)
      $stdout.write("\n")
    end
  end
end

image

and then it's the responsibility of the caller to ensure there's a trailing newline. if we provided something like CLI::UI.write ourselves i think it's our responsibility to ensure frames work sanely. maybe it'd make sense to have some api that automatically adds the newline at the end of the block? e.g.

CLI::UI.write_line do |io|
  io.write('test' * 30)
end

the ruby + gem version bumps i think are unrelated to this change and should be split out if this pr continues

@juharris
Copy link
Author

Thanks for the tips @joshheinrichs-shopify
CLI::UI.raw do ... $stdout.write(CLI::UI::Frame.prefix) doesn't work for us because we're processing events from a stream when chatting with Sidekick. If we get a text event, then we wanna just write to the current frame. If we get one of many other events, then we might start a new frame. We don't know when the text will stop, so we want to just be event-driven and not worry about tracking the current state and if we need a newline or not.

I'll PM you more details since this repo is public.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants