ratatui-org / ratatui

Rust library that's all about cooking up terminal user interfaces (TUIs) 👨‍🍳🐀
https://ratatui.rs
MIT License
8.82k stars 263 forks source link

TableState resets offset to 0 #1179

Closed Cretezy closed 2 weeks ago

Cretezy commented 2 weeks ago

Description

TableState is calculating wrong row bounds, causing offset to be reset to 0. This means that offset is broken on TableState.

To Reproduce

use crossterm::{
    event::{self, KeyCode, KeyEventKind},
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
    ExecutableCommand,
};
use ratatui::{
    layout::Constraint,
    prelude::{CrosstermBackend, Stylize, Terminal},
    widgets::{Paragraph, Row, Table, TableState},
};
use std::io::{stdout, Result};

fn main() -> Result<()> {
    stdout().execute(EnterAlternateScreen)?;
    enable_raw_mode()?;
    let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
    terminal.clear()?;

    let mut table_state = TableState::default();

    loop {
        terminal.draw(|frame| {
            let area = frame.size();

            let mut rows = vec![];
            for i in 0..100 {
                rows.push(Row::new([i.to_string(), table_state.offset().to_string()]))
            }

            let table = Table::new(rows, [Constraint::Fill(1), Constraint::Fill(1)]);

            frame.render_stateful_widget(table, area, &mut table_state);
        })?;

        if let event::Event::Key(key) = event::read()? {
            if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('j') {
                *table_state.offset_mut() += 1;
            }
            if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
                break;
            }
        }
    }

    stdout().execute(LeaveAlternateScreen)?;
    disable_raw_mode()?;
    Ok(())
}

Expected behavior

When pressing j, the second column (which is table_state.offset()) should increment to 1, then 2, then 3, etc.

Currently, it goes to 1, then gets reset to 0 on the next render.

Environment

joshka commented 2 weeks ago

@Cretezy can you please check that https://github.com/ratatui-org/ratatui/pull/1187 solves your issue (See https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html#the-patch-section for info on overriding deps).

Cretezy commented 2 weeks ago

@joshka Apologies for the delay (was away this weekend). The fix does work! Thank you very much

joshka commented 2 weeks ago

@joshka Apologies for the delay (was away this weekend). The fix does work! Thank you very much

Thanks for testing the fix. If you need it in a released crate, It will be available in the next 0.27.0-alpha release (these automatically come out on Saturdays), and a full release in the few of weeks.

Cretezy commented 2 weeks ago

Thanks. I am going to wait for the next full release.

This is the workaround I did in the meantime: https://github.com/Cretezy/lazyjj/commit/fcf9e104103d4656ae0cd2b961c9c85e7923bd90#diff-e49e07cbead444edb2dcd3b3dedd55ebd006f01f72af1cd16ef3f9b71ad857f2R33