Skip to content

Fix --wrap-width truncating code blocks + pin Rust toolchain#57

Merged
jvanderberg merged 5 commits into
mainfrom
fix/wrap-width-code-block-clipping
May 14, 2026
Merged

Fix --wrap-width truncating code blocks + pin Rust toolchain#57
jvanderberg merged 5 commits into
mainfrom
fix/wrap-width-code-block-clipping

Conversation

@jvanderberg
Copy link
Copy Markdown
Owner

Summary

Closes #55. Three related changes plus a CI hygiene fix.

1. Don't let --wrap-width truncate code blocks (src/document/parser.rs, src/app/model.rs, src/document/mod.rs, src/app/event_loop.rs)

wrap_width was used both to word-wrap prose AND to clamp the maximum width of non-wrapping elements like code blocks. Setting --wrap-width 80 on a wider terminal silently truncated code lines >76 chars, which broke copy/paste of those lines.

Split the parser's two width inputs:

  • wrap_width — controls prose word-wrap (unchanged behavior)
  • max_width — upper bound for elements that don't wrap (code blocks, tables); equals the available terminal area regardless of --wrap-width

New public API: Document::parse_with_widths, parse_with_all_options_widths, prepare_document_from_bytes_with_widths. Old API preserved (forwards width as both).

2. Copy the raw code block source, not the rendered line (src/app/model.rs, src/document/types.rs)

When a line is still wider than the terminal itself, it has to clip visually. The previous selection/copy path read from the rendered text, so the trailing characters were silently dropped on paste — the original complaint in #55. Selection now looks up Document::code_block_raw_line for any code body line and copies that verbatim.

3. Maximize visible content when truncation is unavoidable (src/document/parser.rs)

Even after fix #1, lines longer than the terminal need to clip. The old clamp at max_width - 4 left the frame at max_width + 3, so the terminal lopped off the right , the space before it, and one padding space — visible result had two cols of trailing whitespace before the (missing) border. Clip to max_width - 2 instead so visible content extends to the screen edge. The missing right remains the truncation indicator.

4. Pin the Rust toolchain (rust-toolchain.toml, .github/workflows/*.yml)

Discovered while running pre-push checks for this PR: CI used dtolnay/rust-toolchain@stable, so any new stable release that added clippy lints could break CI without anyone changing code (this happened to PR #56). Pin to 1.95.0 across both ci.yml and release.yml and add rust-toolchain.toml so local rustup matches. Bonus: fixed 9 clippy lints from Rust 1.95 in pre-existing code (sort_by_key, collapsible_match, useless_conversion, unnecessary_trailing_comma).

Test plan

  • cargo fmt --check
  • cargo clippy -- -D warnings (Rust 1.95.0)
  • cargo test — 626 tests pass, 5 new (select_poll_ms x3 + parse_with_widths x3 + wrap_width_does_not_truncate_code_blocks + selected_text_returns_raw_code_line_even_when_rendering_truncates)
  • Manual verification with the yt-dlp curl … --import case from --wrap-width truncates code blocks without an indication #55: code block renders at full width on a wide terminal, paste contains the trailing t
  • Manual verification with an over-terminal-width line: content reaches the screen edge, right is the missing indicator, paste still returns the original source

wrap_width and the document's maximum element width were conflated in the
parser — a single value used both to wrap prose and to clamp non-wrapping
elements like code blocks. Setting --wrap-width 80 on a wider terminal
silently truncated code-block lines that exceeded 76 columns, which broke
copy/paste of those lines (the rendered content is what gets copied).

Split the two:

- wrap_width: controls prose word-wrapping (unchanged behavior)
- max_width: upper bound for elements that don't word-wrap; equals the
  available terminal area regardless of --wrap-width

Code blocks now use max_width as their clamp, so they extend to the full
terminal width even when --wrap-width is narrower. They still truncate
when a line exceeds the terminal itself, matching no-flag behavior.

Closes #55.
The first fix in this branch moved code-block truncation from
wrap_width-4 to terminal_width-4, but a line still narrower than the
terminal but wider than wrap_width was the easy case. When a code line
is wider than the terminal itself, it still has to truncate visually —
and the previous copy path read from the rendered text, so the trailing
characters were silently dropped on paste (the original complaint in #55).

Selection now looks up the parsed source via Document::code_block_raw_line
for any code body line and copies that verbatim. Border lines remain
filtered out, and non-code lines keep their existing behavior.
When a code line is wider than the terminal allows, content was clipped
to max_width - 4, leaving the frame at max_width + 3. The terminal then
clipped the right `│`, the space before it, and one of three padding
spaces — leaving two cols of dead whitespace before the visually missing
border.

Clip content to max_width - 2 instead so visible characters extend to
the screen edge. The right border and trailing padding still overflow
and get clipped; the missing right `│` remains the indicator that the
line was truncated.
CI was using `dtolnay/rust-toolchain@stable`, so every new stable release
that added clippy lints could fail CI without anyone changing code. Pin
to 1.95.0 in both ci.yml and release.yml, and add rust-toolchain.toml so
local rustup picks up the same version automatically.

To bump in the future, update all three values together.
Pinning the toolchain surfaces new lints that were never run against the
existing code:

- input.rs: collapse nested `if` guards into match-arm guards
  (collapsible_match)
- model.rs: replace `sort_by` with `sort_by_key` (unnecessary_sort_by)
- types.rs: drop redundant `.into_iter()` in `.zip()` (useless_conversion)
- watcher/mod.rs: remove a trailing comma in `format!` args
  (unnecessary_trailing_comma)
@jvanderberg jvanderberg merged commit 923fd4e into main May 14, 2026
3 checks passed
@jvanderberg jvanderberg deleted the fix/wrap-width-code-block-clipping branch May 14, 2026 01:52
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.

--wrap-width truncates code blocks without an indication

1 participant