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

feat: Add is_at_start and is_at_end methods #1024

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ description to help the reviewer (follow the provided template). Make sure to hi
which may need additional attention or you are uncertain about. Any idea with a large scale impact
on the crate or its users should ideally be discussed in a "Feature Request" issue beforehand.

### Keep PRs small, intentional and focused
### Keep PRs small, intentional, and focused

Try to do one pull request per change. The time taken to review a PR grows exponential with the size
of the change. Small focused PRs will generally be much more faster to review. PRs that include both
Expand Down Expand Up @@ -171,7 +171,7 @@ time to update. However, if a deprecation is blocking for us to implement a new

### Use of unsafe for optimization purposes

We don't currently use any unsafe code in Ratatui, and would like to keep it that way. However there
We don't currently use any unsafe code in Ratatui, and would like to keep it that way. However, there
may be specific cases that this becomes necessary in order to avoid slowness. Please see [this
discussion](https://github.com/ratatui-org/ratatui/discussions/66) for more about the decision.

Expand All @@ -193,10 +193,10 @@ This project was forked from [`tui-rs`](https://github.com/fdehau/tui-rs/) in Fe
[blessing of the original author](https://github.com/fdehau/tui-rs/issues/654), Florian Dehau
([@fdehau](https://github.com/fdehau)).

The original repository contains all the issues, PRs and discussion that were raised originally, and
The original repository contains all the issues, PRs, and discussion that were raised originally, and
it is useful to refer to when contributing code, documentation, or issues with Ratatui.

We imported all the PRs from the original repository and implemented many of the smaller ones and
We imported all the PRs from the original repository, implemented many of the smaller ones, and
made notes on the leftovers. These are marked as draft PRs and labelled as [imported from
tui](https://github.com/ratatui-org/ratatui/pulls?q=is%3Apr+is%3Aopen+label%3A%22imported+from+tui%22).
We have documented the current state of those PRs, and anyone is welcome to pick them up and
Expand Down
51 changes: 48 additions & 3 deletions src/widgets/scrollbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,16 @@
self.position = 0;
}

/// Sets the scroll position to the end of the scrollable content.
pub fn last(&mut self) {
self.position = self.content_length.saturating_sub(1);
/// Returns true if the scroll position is at (or above) the start of the scrollable content.
#[must_use = "returns whether the scroll position is at the start"]
pub const fn is_at_start(&self) -> bool {
self.position == 0
}

/// Returns true if the scroll position is at (or below) the end of the scrollable content.
#[must_use = "returns whether the scroll position is at the end"]
pub const fn is_at_end(&self) -> bool {
self.position + self.viewport_content_length >= self.content_length.saturating_sub(1)

Check warning on line 468 in src/widgets/scrollbar.rs

View check run for this annotation

Codecov / codecov/patch

src/widgets/scrollbar.rs#L467-L468

Added lines #L467 - L468 were not covered by tests
}

/// Changes the scroll position based on the provided [`ScrollDirection`].
Expand Down Expand Up @@ -1081,4 +1088,42 @@
scrollbar_no_arrows.render(buffer.area, &mut buffer, &mut state);
assert_eq!(buffer, Buffer::with_lines(vec![expected]), "{description}");
}

#[rstest]
#[case(0, 0, true, "position_0")]
#[case(0, 1, true, "position_1")]
#[case(0, 2, true, "position_2")]
#[case(1, 0, false, "position_3")]
#[case(1, 1, false, "position_4")]
#[case(2, 2, false, "position_5")]
fn test_is_at_start(
#[case] position: usize,
#[case] content_length: usize,
#[case] expected: bool,
#[case] description: &str,
) {
let state = ScrollbarState::new(content_length).position(position);
assert_eq!(state.is_at_start(), expected, "{description}");
}

// FIXME: these tests don't work
// #[rstest]
// #[case(0, 0, true, "position_0")]
// #[case(1, 0, true, "position_1")]
// #[case(2, 0, true, "position_2")]
// #[case(0, 1, false, "position_3")]
// #[case(0, 2, false, "position_4")]
// #[case(1, 1, true, "position_5")]
// #[case(1, 2, false, "position_6")]
// #[case(2, 2, true, "position_7")]
// #[case(3, 2, true, "position_8")]
// fn test_is_at_end(
// #[case] position: usize,
// #[case] content_length: usize,
// #[case] expected: bool,
// #[case] description: &str,
// ) {
// let state = ScrollbarState::new(content_length).position(position);
// assert_eq!(state.is_at_end(), expected, "{description}");
// }
}
Loading