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

Consistent examples in ratatui #1157

Open
kdheepak opened this issue May 31, 2024 · 2 comments
Open

Consistent examples in ratatui #1157

kdheepak opened this issue May 31, 2024 · 2 comments
Assignees
Labels
Status: In Progress Someone is actively working on this Type: Enhancement New feature or request

Comments

@kdheepak
Copy link
Collaborator

Problem

On discord, we mentioned there was an issue with examples being inconsistent. I wanted to open an issue to track it.

Some of my thoughts are below. I think we need a few different kinds of examples:

  1. Examples of features used in a widget
  2. Examples of how to build more complicated applications on top of widgets / patterns one can follow etc
  3. Examples of how to structure applications

As an example, compare examples/paragraph.rs and examples/list.rs. IMO,

  • paragraph.rs falls under the category of showing features of the Paragraph widget
  • list.rs falls into the category of showing how to build a simple TODO application on top of the List widget while showcasing the List and ListState functionality.

There's a complexity difference between these two categories.

Suggestions:

Perhaps the list.rs example works better as a tutorial than an example?

Examples that fall into category 1 should make it extremely easy for anyone to experiment with different features offered by said widget in Ratatui. To debug a problem on discord, I tried to start with the list.rs example but found it was too complicated to experiment with, and it was faster to write the following from scratch:

use crossterm::{
    event::{self, Event, KeyCode, KeyEventKind},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::CrosstermBackend, Terminal};

fn main() -> Result<(), color_eyre::eyre::Error> {
    let original_hook = std::panic::take_hook();

    std::panic::set_hook(Box::new(move |panic| {
        disable_raw_mode().unwrap();
        let mut terminal = Terminal::new(CrosstermBackend::new(std::io::stdout())).unwrap();
        execute!(terminal.backend_mut(), LeaveAlternateScreen).unwrap();
        original_hook(panic);
    }));

    let mut terminal = Terminal::new(CrosstermBackend::new(std::io::stdout()))?;
    enable_raw_mode()?;
    execute!(terminal.backend_mut(), EnterAlternateScreen)?;

    loop {
        let mut list_state = ratatui::widgets::ListState::default();

        list_state.select(Some(1));

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

            let items = ["Item 1", "Item 2", "Item 3"]
                .into_iter()
                .map(|s| ratatui::text::Text::from(s).centered());

            let list = ratatui::widgets::List::new(items)
                .highlight_symbol(" > ")
                .highlight_style(
                    ratatui::style::Style::default()
                        .fg(ratatui::style::Color::Black)
                        .bg(ratatui::style::Color::White),
                );

            frame.render_stateful_widget(list, area, &mut list_state)
        })?;

        if let Event::Key(key) = event::read()? {
            if key.kind == KeyEventKind::Press {
                match key.code {
                    KeyCode::Char('q') => break,
                    KeyCode::Char('j') => list_state.next(), // TODO implement .next()
                    KeyCode::Char('k') => list_state.previous(), // TODO implement .previous()
                    _ => (),
                }
            }
        }
    }

    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    Ok(())
}

We should solicit more input from users on what kinds of examples they find useful.

@kdheepak kdheepak added the Type: Enhancement New feature or request label May 31, 2024
joshka added a commit that referenced this issue May 31, 2024
Also cleans up the list example significantly (see also
<#1157>)

Fixes: <#1159>
@joshka
Copy link
Member

joshka commented May 31, 2024

We should solicit more input from users on what kinds of examples they find useful.

I was thinking about the same today - a good place for this might be to add a small comment box on the website - e.g. if we move the examples to the website (ratatui-org/ratatui-website#619) then that becomes a good place to solicit info. A simple thumbs up / down / with a textbox that posts to a kv store on cloudflare might be enough for this. There were also some suggestions just today on hn about feedback form approaches https://news.ycombinator.com/item?id=40524501

@joshka
Copy link
Member

joshka commented May 31, 2024

I refactored the list example in #1159 quite a bit.

I've been reading the bevy docs, and examples quite a bit recently, one thing I noted was that they have a bunch of examples on the same topic, some simple, some more advanced. I'd caution against making everything simple or converting it into a tutorial, as there's a lot of work going from example to tutorial well. I'd say a good middle ground would be webpages that have

  • here's the code
  • here's how it looks
  • here's the relevant bit that matters
  • here's a FAQ about it

joshka added a commit that referenced this issue Jun 4, 2024
joshka added a commit that referenced this issue Jun 6, 2024
@joshka joshka added Type: Enhancement Type: Enhancement New feature or request Status: In Progress Someone is actively working on this and removed Type: Enhancement New feature or request Type: zEnhancement labels Jun 19, 2024
@joshka joshka self-assigned this Jun 19, 2024
itsjunetime pushed a commit to itsjunetime/ratatui that referenced this issue Jun 23, 2024
joshka added a commit that referenced this issue Jun 24, 2024
Also cleans up the list example significantly (see also
<#1157>)

Fixes: <#1159>

BREAKING CHANGE: The `List` widget now clamps the selected index to the
bounds of the list when navigating with `first`, `last`, `previous`, and
`next`, as well as when setting the index directly with `select`.

fix: integ test
joshka added a commit that referenced this issue Jun 24, 2024
Also cleans up the list example significantly (see also
<#1157>)

Fixes: <#1159>

BREAKING CHANGE: The `List` widget now clamps the selected index to the
bounds of the list when navigating with `first`, `last`, `previous`, and
`next`, as well as when setting the index directly with `select`.
orhun pushed a commit that referenced this issue Jun 24, 2024
#1159)

Also cleans up the list example significantly (see also
<#1157>)
    
Fixes: <#1159>
    
BREAKING CHANGE: The `List` widget now clamps the selected index to the
bounds of the list when navigating with `first`, `last`, `previous`, and
`next`, as well as when setting the index directly with `select`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: In Progress Someone is actively working on this Type: Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants