Skip to content

Commit

Permalink
fix: consider wrap size
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoto7250 committed Jun 27, 2024
1 parent 4480a87 commit d343f64
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ keywords = ["database", "cli", "Rust", "tui", "terminal"]
categories = ["command-line-utilities"]

[dependencies]
ratatui = { version = "0.26.3" }
ratatui = { version = "0.26.3", features = ["unstable-rendered-line-info"] }
crossterm = "0.27.0"
anyhow = "1.0.86"
unicode-width = "0.1.13"
Expand Down
23 changes: 6 additions & 17 deletions src/components/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ impl ClipboardComponent {
self.table = Some((database, table));
}

pub fn content_height(&self) -> usize {
if let Some(s) = self.content.clone() {
s.matches('\n').count()
} else {
0
}
}

pub fn unwrap_content(&self) -> String {
self.content.clone().unwrap_or(String::from(""))
}
Expand Down Expand Up @@ -90,9 +82,11 @@ impl PropertyTrait for ClipboardComponent {
}));

// can scroll = content.height - widget.height
// we can use `line_count` when this function is stable.
// see: https://github.com/ratatui-org/ratatui/issues/293
let content_height = self.content_height();
let paragraph = Paragraph::new(self.unwrap_content())
.scroll((self.position, 0))
.wrap(Wrap { trim: false });

let content_height = paragraph.line_count(chunks[0].width);
let rect_height = (chunks[0].height - Self::MARGIN) as usize;
let diff = (content_height).saturating_sub(rect_height);
self.position = std::cmp::min(self.position, diff as u16);
Expand All @@ -104,12 +98,7 @@ impl PropertyTrait for ClipboardComponent {
);
self.scroll.draw(f, chunks[0]);

f.render_widget(
Paragraph::new(self.unwrap_content())
.scroll((self.position, 0))
.wrap(Wrap { trim: false }),
chunks[0],
);
f.render_widget(paragraph, chunks[0]);

Ok(())
}
Expand Down

0 comments on commit d343f64

Please sign in to comment.