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

fix: consider wrap size #83

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading