Paragraph, Block, and Padding#6
Conversation
There was a problem hiding this comment.
Pull Request Overview
Adds foundational layout and component types for rendering styled text blocks with padding in terminal UIs.
- Introduces
Paddingtype withIsZeroandApplyto adjust drawing areas. - Implements
Paragraphcomponent to wrap, truncate, and style text. - Implements
Blockcomponent that applies background fill, padding, and inherited styles/links. - Provides an example showcasing the new types in an interactive terminal app.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| layout/padding.go | New Padding struct with zero-check and area apply |
| component/paragraph/paragraph.go | New Paragraph.Draw logic for wrapping and truncation |
| component/block/block.go | New Block.Draw logic for background, padding, style inheritance |
| examples/paragraph/main.go | Sample application demonstrating Paragraph and Block usage |
Comments suppressed due to low confidence (1)
component/paragraph/paragraph.go:24
- The new
Drawmethod handles wrapping, truncation, and newline logic; consider adding unit tests covering each scenario (normal wrap, explicit newline, truncation with tail) to ensure edge cases are handled correctly.
func (p Paragraph) Draw(scr uv.Screen, area uv.Rectangle) {
|
|
||
| pos := uv.Pos(x, y) | ||
| if pos.In(area) { | ||
| if p.Truncate && y >= area.Max.Y-1 && tailWidth > 0 && x+w > area.Max.X-tailWidth { |
There was a problem hiding this comment.
After drawing the truncation tail, the loop continues processing original text graphemes, potentially rendering characters beyond the tail. Consider breaking out of the loop or returning after appending the tail to prevent overdraw.
| b.Component.Draw(scr, area) | ||
|
|
||
| // Apply missing style and link to the component. | ||
| for y := area.Min.Y; y < area.Max.Y; y++ { |
There was a problem hiding this comment.
[nitpick] This nested loop applies style and link to each cell individually, which could be costly for large areas. Consider merging style inheritance into the main draw pass or tracking regions that actually need updates to reduce iterations.
| } | ||
|
|
||
| // Apply applies the padding to the given area and returns a new area. | ||
| func (p Padding) Apply(area uv.Rectangle) uv.Rectangle { |
There was a problem hiding this comment.
If padding exceeds the dimensions of the provided area, area.Min may become greater than area.Max, leading to invalid rectangles. Consider clamping values or returning a zero-area rectangle to avoid unintended behavior.
This adds a new `paragraph` component that allows rendering paragraphs of text with optional truncation and tail. The component can handle line wrapping and supports styles and links.
This commit introduces a new `Block` component that allows for creating blocks that wrap other components with optional padding.
This adds new
Paragraph,BlockandPaddingtypes. AParagraphis a blob of text that can be wrapped or truncated. The text can have style attributes.A
Blockcan wrap any component. The wrapped component inherits any style attributes defined in theBlock.