Repository: rust-lang/rustlings
File: src/list.rs (Rust)

Code before:
                        }
                    }
                }

                match key.code {
                    KeyCode::Char('q') => {
                        return Ok(());
                    }
                    KeyCode::Down | KeyCode::Char('j') => list_state.select_next(),
                    KeyCode::Up | KeyCode::Char('k') => list_state.select_previous(),
                    KeyCode::Home | KeyCode::Char('g') => list_state.select_first(),
                    KeyCode::End | KeyCode::Char('G') => list_state.select_last(),
                    KeyCode::Char('d') => {
                        if list_state.filter() == Filter::Done {
                            list_state.set_filter(Filter::None);
                            list_state.message.push_str("Disabled filter DONE");
                        } else {
                            list_state.set_filter(Filter::Done);
                            list_state.message.push_str(
                                "Enabled filter DONE │ Press d again to disable the filter",
                            );
                        }
                    }
                    KeyCode::Char('p') => {
                        let message = if list_state.filter() == Filter::Pending {
                            list_state.set_filter(Filter::None);
                            "Disabled filter PENDING"
                        } else {
                            list_state.set_filter(Filter::Pending);
                            "Enabled filter PENDING │ Press p again to disable the filter"
                        };

                        list_state.message.push_str(message);
                    }
                    KeyCode::Char('r') => list_state.reset_selected()?,
                    KeyCode::Char('c') => {
                        if list_state.selected_to_current_exercise()? {
                            return Ok(());
                        }
                    }
                    KeyCode::Char('s') | KeyCode::Char('/') => {
                        list_state.message.push_str("search:|");
                        is_searching = true;
                    }
                    // Redraw to remove the message.
                    KeyCode::Esc => (),
                    _ => continue,
                }
            }
            Event::Mouse(event) => match event.kind {
                MouseEventKind::ScrollDown => list_state.select_next(),

Review comment (suggestion):
This can be replaced with `apply_search_query`. It should return early when `search_query` is empty.

Code after:
                        } else {
                            list_state.set_filter(Filter::Done);
                            list_state.message.push_str(
                                "Enabled filter DONE │ Press d again to disable the filter",
                            );
                        }
                    }
                    KeyCode::Char('p') => {
                        let message = if list_state.filter() == Filter::Pending {
                            list_state.set_filter(Filter::None);
                            "Disabled filter PENDING"
                        } else {
                            list_state.set_filter(Filter::Pending);
                            "Enabled filter PENDING │ Press p again to disable the filter"
                        };

                        list_state.message.push_str(message);
                    }
                    KeyCode::Char('r') => list_state.reset_selected()?,
                    KeyCode::Char('c') => {
                        if list_state.selected_to_current_exercise()? {
                            return Ok(());
                        }
                    }
                    KeyCode::Char('s' | '/') => {
                        list_state.message.push_str("search:|");
                        is_searching = true;
                    }
                    // Redraw to remove the message.
                    KeyCode::Esc => (),
                    _ => continue,
                }
            }
            Event::Mouse(event) => match event.kind {
                MouseEventKind::ScrollDown => list_state.select_next(),
                MouseEventKind::ScrollUp => list_state.select_previous(),
                _ => continue,
            },
            Event::Resize(width, height) => list_state.set_term_size(width, height),
            // Ignore
            Event::FocusGained | Event::FocusLost => continue,
        }

        list_state.draw(stdout)?;
    }
}

pub fn list(app_state: &mut AppState) -> Result<()> {
    let mut stdout = io::stdout().lock();
    stdout
        .queue(EnterAlternateScreen)?