mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-11-08 09:09:17 +01:00
Done navigation
This commit is contained in:
parent
729385362c
commit
372290a796
83
src/list.rs
83
src/list.rs
@ -1,34 +1,22 @@
|
|||||||
use std::{io, time::Duration};
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use crossterm::{
|
use crossterm::{
|
||||||
event::{self, KeyCode, KeyEventKind},
|
event::{self, Event, KeyCode, KeyEventKind},
|
||||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||||
ExecutableCommand,
|
ExecutableCommand,
|
||||||
};
|
};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
backend::CrosstermBackend,
|
backend::CrosstermBackend,
|
||||||
layout::Constraint,
|
layout::Constraint,
|
||||||
style::{Modifier, Style, Stylize},
|
style::{Style, Stylize},
|
||||||
text::Span,
|
text::Span,
|
||||||
widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
|
widgets::{HighlightSpacing, Row, Table, TableState},
|
||||||
Terminal,
|
Terminal,
|
||||||
};
|
};
|
||||||
|
use std::io;
|
||||||
|
|
||||||
use crate::{exercise::Exercise, state::State};
|
use crate::{exercise::Exercise, state::State};
|
||||||
|
|
||||||
// 40 FPS.
|
fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
|
||||||
const UPDATE_INTERVAL: Duration = Duration::from_millis(25);
|
|
||||||
|
|
||||||
pub fn list(state: &State, exercises: &[Exercise]) -> Result<()> {
|
|
||||||
let mut stdout = io::stdout().lock();
|
|
||||||
|
|
||||||
stdout.execute(EnterAlternateScreen)?;
|
|
||||||
enable_raw_mode()?;
|
|
||||||
|
|
||||||
let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?;
|
|
||||||
terminal.clear()?;
|
|
||||||
|
|
||||||
let header = Row::new(["State", "Name", "Path"]);
|
let header = Row::new(["State", "Name", "Path"]);
|
||||||
|
|
||||||
let max_name_len = exercises
|
let max_name_len = exercises
|
||||||
@ -60,28 +48,69 @@ pub fn list(state: &State, exercises: &[Exercise]) -> Result<()> {
|
|||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let table = Table::new(rows, widths)
|
Table::new(rows, widths)
|
||||||
.header(header)
|
.header(header)
|
||||||
.column_spacing(2)
|
.column_spacing(2)
|
||||||
.highlight_spacing(HighlightSpacing::Always)
|
.highlight_spacing(HighlightSpacing::Always)
|
||||||
.highlight_style(Style::new().add_modifier(Modifier::REVERSED))
|
.highlight_style(Style::new().bg(ratatui::style::Color::Rgb(50, 50, 50)))
|
||||||
.highlight_symbol("🦀");
|
.highlight_symbol("🦀")
|
||||||
|
}
|
||||||
|
|
||||||
let mut table_state = TableState::default().with_selected(Some(0));
|
pub fn list(state: &State, exercises: &[Exercise]) -> Result<()> {
|
||||||
|
let mut stdout = io::stdout().lock();
|
||||||
|
|
||||||
loop {
|
stdout.execute(EnterAlternateScreen)?;
|
||||||
|
enable_raw_mode()?;
|
||||||
|
|
||||||
|
let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?;
|
||||||
|
terminal.clear()?;
|
||||||
|
|
||||||
|
let table = table(state, exercises);
|
||||||
|
|
||||||
|
let last_ind = exercises.len() - 1;
|
||||||
|
let mut selected = 0;
|
||||||
|
let mut table_state = TableState::default().with_selected(Some(selected));
|
||||||
|
|
||||||
|
'outer: loop {
|
||||||
terminal.draw(|frame| {
|
terminal.draw(|frame| {
|
||||||
let area = frame.size();
|
let area = frame.size();
|
||||||
|
|
||||||
frame.render_stateful_widget(&table, area, &mut table_state);
|
frame.render_stateful_widget(&table, area, &mut table_state);
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
if event::poll(UPDATE_INTERVAL)? {
|
let key = loop {
|
||||||
if let event::Event::Key(key) = event::read()? {
|
match event::read()? {
|
||||||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
|
Event::Key(key) => break key,
|
||||||
break;
|
// Redraw
|
||||||
}
|
Event::Resize(_, _) => continue 'outer,
|
||||||
|
// Ignore
|
||||||
|
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => (),
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if key.kind != KeyEventKind::Press {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
match key.code {
|
||||||
|
KeyCode::Char('q') => break,
|
||||||
|
KeyCode::Down | KeyCode::Char('j') => {
|
||||||
|
selected = selected.saturating_add(1).min(last_ind);
|
||||||
|
table_state.select(Some(selected));
|
||||||
|
}
|
||||||
|
KeyCode::Up | KeyCode::Char('k') => {
|
||||||
|
selected = selected.saturating_sub(1).max(0);
|
||||||
|
table_state.select(Some(selected));
|
||||||
|
}
|
||||||
|
KeyCode::Home | KeyCode::Char('g') => {
|
||||||
|
selected = 0;
|
||||||
|
table_state.select(Some(selected));
|
||||||
|
}
|
||||||
|
KeyCode::End | KeyCode::Char('G') => {
|
||||||
|
selected = last_ind;
|
||||||
|
table_state.select(Some(selected));
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user