tui-rs
tui-rs copied to clipboard
state: adds offset access and manipulation
Description
Adds offset access and manipulation. Fixes #495.
Testing guidelines
I have a few structs that keep the table state. I use this trait ScrollableTable
for each of these to attach actions like scroll up/down, etc. I've added this fn recenter
to test offset manipulation. ListState
should be pretty much analogously.
Note that I haven't added tests per se, because I think that these kinds of accessor/mutator functions are quite obvious.
use tui::layout::Rect;
use tui::widgets::TableState;
pub trait ScrollableTable {
fn size(&self) -> Rect;
fn state(&self) -> &TableState;
fn state_mut(&mut self) -> &mut TableState;
fn recenter(&mut self) {
if let Some(selected) = self.state().selected() {
let height = self.size().height as usize;
let offset = self.state_mut().offset_mut();
*offset = selected.saturating_sub(height / 2);
}
}
}
Checklist
- [x] I have read the contributing guidelines.
- [ ] I have added relevant tests.
- [x] I have documented all new additions.
cargo make does not seem to be compatible with rustc 1.56.1:
error: failed to compile `cargo-make v0.35.8`, intermediate artifacts can be found at `/tmp/cargo-install52QPLl`
Caused by:
package `os_str_bytes v6.2.0` cannot be built because it requires rustc 1.57.0 or newer, while the currently active rustc version is 1.56.1
Error: Process completed with exit code 101.