Skip to content
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

wrap to terminal width #249

Closed
wants to merge 1 commit into from
Closed

wrap to terminal width #249

wants to merge 1 commit into from

Conversation

gelisam
Copy link
Owner

@gelisam gelisam commented Aug 30, 2024

We are using the prettyprinter library, which nicely wraps and indents the output to fit the screen, but we were using the default layout options, which assume that the screen is always exactly 80 characters wide. With a slightly sinful unsafePerformIO, we can use the terminal's actual width instead.

Summary by CodeRabbit

  • New Features
    • Introduced dynamic layout configuration for pretty-printing based on terminal width, enhancing document rendering flexibility.
  • Bug Fixes
    • Improved handling of environment variables for layout options, ensuring a default of 80 columns when necessary.
  • Documentation
    • Updated import statements to reflect new dependencies for environment variable access and string parsing.

We are using the prettyprinter library, which nicely wraps and indents
the output to fit the screen, but we were using the default layout
options, which assume that the screen is always exactly 80 characters
wide. With a slightly sinful unsafePerformIO, we can use the terminal's
actual width instead.
Copy link

coderabbitai bot commented Aug 30, 2024

Walkthrough

The changes introduce a new function, customLayoutOptions, in the pretty-printing module to dynamically set the number of columns based on the COLUMNS environment variable. This function is integrated into the pretty and prettyEnv functions, replacing the static defaultLayoutOptions. The import statements are updated to include necessary modules for environment access and integer parsing.

Changes

Files Change Summary
src/Pretty.hs Added customLayoutOptions for dynamic column layout based on COLUMNS env variable; modified pretty and prettyEnv to use this function; updated import statements.

Poem

In a world where columns sway,
A rabbit hops to greet the day.
With options bright, the prints align,
Adjusting width, oh how they shine!
So let us cheer, with joyful thumps,
For pretty prints and happy jumps! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@@ -58,8 +62,21 @@ angles doc = text "⟨" <> align (group doc) <> "⟩"
vec :: Doc ann -> Doc ann
vec doc = text "[" <> align (group doc) <> "]"

customLayoutOptions :: LayoutOptions
customLayoutOptions = unsafePerformIO $ do
Copy link
Owner Author

Choose a reason for hiding this comment

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

Using unsafePerformIO is the most straightforward way to do it, but of course I expect that somebody will object and that I will have to thread a LayoutOptions variable everywhere.

Copy link
Owner Author

Choose a reason for hiding this comment

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

One concern is that the tests use pretty, so they might start failing depending on the terminal width. But I tried making my terminal very small and the tests still passed 🤷

Copy link
Owner Author

Choose a reason for hiding this comment

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

Oh, that's because it doesn't work. echo $COLUMNS prints the right value in the terminal, but lookupEnv "COLUMNS" returns Nothing!

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a99bf39 and 810bf2b.

Files selected for processing (1)
  • src/Pretty.hs (5 hunks)
Additional comments not posted (1)
src/Pretty.hs (1)

79-79: Conditional approval of changes to pretty and prettyEnv.

The modifications to use customLayoutOptions align with the PR's objectives to dynamically adjust the layout based on the terminal width. However, the approval is conditional on addressing the potential issues with unsafePerformIO as noted in the review of customLayoutOptions.

The changes are conditionally approved pending the resolution of the unsafePerformIO usage.

Also applies to: 89-89

Comment on lines +23 to +26
import System.Environment (lookupEnv)
import System.FilePath (takeFileName)
import System.IO.Unsafe (unsafePerformIO)
import Text.Read (readMaybe)
Copy link

Choose a reason for hiding this comment

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

Review the new imports for environment handling and unsafe IO operations.

The addition of System.Environment and System.IO.Unsafe is necessary for the new functionality to dynamically fetch the terminal width. However, the use of unsafePerformIO should be carefully justified as it can lead to non-deterministic behavior in a purely functional setting.

Consider alternatives to unsafePerformIO if possible, to maintain the purity of the functions.

Comment on lines +65 to +77
customLayoutOptions :: LayoutOptions
customLayoutOptions = unsafePerformIO $ do
columnsMaybeString <- lookupEnv "COLUMNS"
let columnsMaybeInt :: Maybe Int
columnsMaybeInt = do
str <- columnsMaybeString
readMaybe str
let columns :: Int
columns = fromMaybe 80 columnsMaybeInt

pure $ defaultLayoutOptions
{layoutPageWidth = AvailablePerLine columns 1.0}

Copy link

Choose a reason for hiding this comment

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

Consider refactoring customLayoutOptions to avoid unsafePerformIO.

The function uses unsafePerformIO to fetch the terminal width, which can lead to unpredictable behavior in Haskell. It's generally recommended to keep IO operations explicit in the function's type signature to maintain purity and predictability.

Consider refactoring the function to return IO LayoutOptions instead, and handle the IO operation in the calling context. This change would make the IO operations explicit and maintain the purity of the function.

customLayoutOptions :: IO LayoutOptions
customLayoutOptions = do
  columnsMaybeString <- lookupEnv "COLUMNS"
  let columnsMaybeInt = readMaybe =<< columnsMaybeString
  let columns = fromMaybe 80 columnsMaybeInt
  pure $ defaultLayoutOptions { layoutPageWidth = AvailablePerLine columns 1.0 }

@gelisam
Copy link
Owner Author

gelisam commented Aug 30, 2024

Closing because it doesn't work 🙃

@gelisam gelisam closed this Aug 30, 2024
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.

1 participant