feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
//! State like [`ListState`], [`TableState`] and [`ScrollbarState`] can be serialized and
|
|
|
|
//! deserialized through serde. This allows saving your entire state to disk when the user exits the
|
|
|
|
//! the app, and restore it again upon re-opening the app.
|
|
|
|
//! This way, they get right back to where they were, without having to re-seek to their previous
|
|
|
|
//! position, if that's applicable for the app at hand.
|
|
|
|
//!
|
|
|
|
//! **Note**: For this pattern to work easily, you need to have some toplevel struct which stores
|
|
|
|
//! _only_ state and not any draw commands.
|
|
|
|
//!
|
|
|
|
//! **Note**: For many applications, it might be beneficial to instead keep your own state and
|
|
|
|
//! instead construct the state for widgets on the fly instead, if that allows you to express you
|
|
|
|
//! the semantic meaning of your state better or only fetch part of a dataset.
|
|
|
|
|
|
|
|
// not too happy about the redundancy in these tests,
|
|
|
|
// but if that helps readability then it's ok i guess /shrug
|
|
|
|
|
2024-09-28 18:47:45 +00:00
|
|
|
use ratatui::{
|
|
|
|
backend::TestBackend,
|
|
|
|
layout::{Constraint, Direction, Layout},
|
|
|
|
text::Line,
|
|
|
|
widgets::{
|
|
|
|
Block, Borders, List, ListState, Row, Scrollbar, ScrollbarOrientation, ScrollbarState,
|
|
|
|
Table, TableState,
|
|
|
|
},
|
|
|
|
Terminal,
|
|
|
|
};
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
|
|
struct AppState {
|
2024-03-02 09:06:53 +00:00
|
|
|
list: ListState,
|
|
|
|
table: TableState,
|
|
|
|
scrollbar: ScrollbarState,
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AppState {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2024-03-02 09:06:53 +00:00
|
|
|
list: ListState::default(),
|
|
|
|
table: TableState::default(),
|
|
|
|
scrollbar: ScrollbarState::new(10),
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl AppState {
|
|
|
|
fn select(&mut self, index: usize) {
|
2024-03-02 09:06:53 +00:00
|
|
|
self.list.select(Some(index));
|
2024-10-13 11:06:29 +00:00
|
|
|
self.table.select_cell(Some((index, index)));
|
2024-03-02 09:06:53 +00:00
|
|
|
self.scrollbar = self.scrollbar.position(index);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
/// Renders the list to a `TestBackend` and asserts that the result matches the expected buffer.
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
#[track_caller]
|
2024-05-14 01:13:46 +00:00
|
|
|
fn assert_buffer<'line, Lines>(state: &mut AppState, expected: Lines)
|
|
|
|
where
|
|
|
|
Lines: IntoIterator,
|
|
|
|
Lines::Item: Into<Line<'line>>,
|
|
|
|
{
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
let backend = TestBackend::new(21, 5);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
2024-03-02 09:06:53 +00:00
|
|
|
let items = [
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
"awa", "banana", "Cats!!", "d20", "Echo", "Foxtrot", "Golf", "Hotel", "IwI",
|
|
|
|
"Juliett",
|
|
|
|
];
|
|
|
|
|
|
|
|
let layout = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
2024-03-02 09:06:53 +00:00
|
|
|
.constraints([
|
|
|
|
Constraint::Length(10),
|
|
|
|
Constraint::Length(10),
|
|
|
|
Constraint::Length(1),
|
|
|
|
])
|
2024-08-06 03:15:14 +00:00
|
|
|
.split(f.area());
|
2024-03-02 09:06:53 +00:00
|
|
|
let list = List::new(items)
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
.highlight_symbol(">>")
|
2024-05-02 10:09:48 +00:00
|
|
|
.block(Block::new().borders(Borders::RIGHT));
|
2024-03-02 09:06:53 +00:00
|
|
|
f.render_stateful_widget(list, layout[0], &mut state.list);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
|
|
|
|
let table = Table::new(
|
2024-03-02 09:06:53 +00:00
|
|
|
items.into_iter().map(|i| Row::new(vec![i])),
|
2024-01-17 02:51:25 +00:00
|
|
|
[Constraint::Length(10); 1],
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
)
|
|
|
|
.highlight_symbol(">>");
|
2024-03-02 09:06:53 +00:00
|
|
|
f.render_stateful_widget(table, layout[1], &mut state.table);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
|
|
|
|
let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight);
|
2024-03-02 09:06:53 +00:00
|
|
|
f.render_stateful_widget(scrollbar, layout[2], &mut state.scrollbar);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
terminal.backend().assert_buffer_lines(expected);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_STATE_BUFFER: [&str; 5] = [
|
|
|
|
"awa │awa ▲",
|
|
|
|
"banana │banana █",
|
|
|
|
"Cats!! │Cats!! ║",
|
|
|
|
"d20 │d20 ║",
|
|
|
|
"Echo │Echo ▼",
|
|
|
|
];
|
|
|
|
|
|
|
|
const DEFAULT_STATE_REPR: &str = r#"{
|
2024-03-02 09:06:53 +00:00
|
|
|
"list": {
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
"offset": 0,
|
|
|
|
"selected": null
|
|
|
|
},
|
2024-03-02 09:06:53 +00:00
|
|
|
"table": {
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
"offset": 0,
|
2024-10-13 11:06:29 +00:00
|
|
|
"selected": null,
|
|
|
|
"selected_column": null
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
},
|
2024-03-02 09:06:53 +00:00
|
|
|
"scrollbar": {
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
"content_length": 10,
|
|
|
|
"position": 0,
|
|
|
|
"viewport_content_length": 0
|
|
|
|
}
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_state_serialize() {
|
|
|
|
let mut state = AppState::default();
|
2024-05-14 01:13:46 +00:00
|
|
|
assert_buffer(&mut state, DEFAULT_STATE_BUFFER);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
let state = serde_json::to_string_pretty(&state).unwrap();
|
|
|
|
assert_eq!(state, DEFAULT_STATE_REPR);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_state_deserialize() {
|
|
|
|
let mut state: AppState = serde_json::from_str(DEFAULT_STATE_REPR).unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
assert_buffer(&mut state, DEFAULT_STATE_BUFFER);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SELECTED_STATE_BUFFER: [&str; 5] = [
|
|
|
|
" awa │ awa ▲",
|
|
|
|
">>banana │>>banana █",
|
|
|
|
" Cats!! │ Cats!! ║",
|
|
|
|
" d20 │ d20 ║",
|
|
|
|
" Echo │ Echo ▼",
|
|
|
|
];
|
|
|
|
const SELECTED_STATE_REPR: &str = r#"{
|
2024-03-02 09:06:53 +00:00
|
|
|
"list": {
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
"offset": 0,
|
|
|
|
"selected": 1
|
|
|
|
},
|
2024-03-02 09:06:53 +00:00
|
|
|
"table": {
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
"offset": 0,
|
2024-10-13 11:06:29 +00:00
|
|
|
"selected": 1,
|
|
|
|
"selected_column": 0
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
},
|
2024-03-02 09:06:53 +00:00
|
|
|
"scrollbar": {
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
"content_length": 10,
|
|
|
|
"position": 1,
|
|
|
|
"viewport_content_length": 0
|
|
|
|
}
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn selected_state_serialize() {
|
|
|
|
let mut state = AppState::default();
|
|
|
|
state.select(1);
|
2024-05-14 01:13:46 +00:00
|
|
|
assert_buffer(&mut state, SELECTED_STATE_BUFFER);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
let state = serde_json::to_string_pretty(&state).unwrap();
|
|
|
|
assert_eq!(state, SELECTED_STATE_REPR);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn selected_state_deserialize() {
|
|
|
|
let mut state: AppState = serde_json::from_str(SELECTED_STATE_REPR).unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
assert_buffer(&mut state, SELECTED_STATE_BUFFER);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SCROLLED_STATE_BUFFER: [&str; 5] = [
|
|
|
|
" Echo │ Echo ▲",
|
|
|
|
" Foxtrot│ Foxtrot ║",
|
|
|
|
" Golf │ Golf ║",
|
|
|
|
" Hotel │ Hotel █",
|
|
|
|
">>IwI │>>IwI ▼",
|
|
|
|
];
|
|
|
|
|
|
|
|
const SCROLLED_STATE_REPR: &str = r#"{
|
2024-03-02 09:06:53 +00:00
|
|
|
"list": {
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
"offset": 4,
|
|
|
|
"selected": 8
|
|
|
|
},
|
2024-03-02 09:06:53 +00:00
|
|
|
"table": {
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
"offset": 4,
|
2024-10-13 11:06:29 +00:00
|
|
|
"selected": 8,
|
|
|
|
"selected_column": 0
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
},
|
2024-03-02 09:06:53 +00:00
|
|
|
"scrollbar": {
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
"content_length": 10,
|
|
|
|
"position": 8,
|
|
|
|
"viewport_content_length": 0
|
|
|
|
}
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn scrolled_state_serialize() {
|
|
|
|
let mut state = AppState::default();
|
|
|
|
state.select(8);
|
2024-05-14 01:13:46 +00:00
|
|
|
assert_buffer(&mut state, SCROLLED_STATE_BUFFER);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
let state = serde_json::to_string_pretty(&state).unwrap();
|
|
|
|
assert_eq!(state, SCROLLED_STATE_REPR);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn scrolled_state_deserialize() {
|
|
|
|
let mut state: AppState = serde_json::from_str(SCROLLED_STATE_REPR).unwrap();
|
2024-05-14 01:13:46 +00:00
|
|
|
assert_buffer(&mut state, SCROLLED_STATE_BUFFER);
|
feat(serde): support TableState, ListState, and ScrollbarState (#723)
TableState, ListState, and ScrollbarState can now be serialized and deserialized
using serde.
```rust
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct AppState {
list_state: ListState,
table_state: TableState,
scrollbar_state: ScrollbarState,
}
let app_state = AppState::default();
let serialized = serde_json::to_string(app_state);
let app_state = serde_json::from_str(serialized);
```
2024-01-13 00:13:35 +00:00
|
|
|
}
|
2024-10-13 11:06:29 +00:00
|
|
|
|
|
|
|
// For backwards compatibility these fields should be enough to deserialize the state.
|
|
|
|
const OLD_TABLE_DESERIALIZE: &str = r#"{
|
|
|
|
"offset": 0,
|
|
|
|
"selected": 1
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
const NEW_TABLE_DESERIALIZE: &str = r#"{
|
|
|
|
"offset": 0,
|
|
|
|
"selected": 1,
|
|
|
|
"selected_column": null
|
|
|
|
}"#;
|
|
|
|
|
|
|
|
// This test is to check for backwards compatibility with the old states.
|
|
|
|
#[test]
|
|
|
|
fn table_state_backwards_compatibility() {
|
|
|
|
let old_state: TableState = serde_json::from_str(OLD_TABLE_DESERIALIZE).unwrap();
|
|
|
|
let new_state: TableState = serde_json::from_str(NEW_TABLE_DESERIALIZE).unwrap();
|
|
|
|
assert_eq!(old_state, new_state);
|
|
|
|
}
|