map: fix rendering errors

This commit is contained in:
lunar-mining
2022-01-09 12:16:19 +01:00
parent 8ad7073ee9
commit 176bad6bcd
3 changed files with 89 additions and 139 deletions

View File

@@ -1,38 +1,88 @@
use crate::list::StatefulList;
pub struct App<'a> {
pub items: StatefulList<(&'a str, usize)>,
pub nodes: StatefulList<(&'a str)>,
}
impl<'a> App<'a> {
pub fn new() -> App<'a> {
App {
items: StatefulList::with_items(vec![
("node0", 1),
("node1", 2),
("node2", 1),
("node3", 3),
("node4", 1),
("node5", 4),
("node6", 1),
("node7", 3),
("node8", 1),
("node9", 6),
("node10", 1),
("node11", 3),
("node12", 1),
("node13", 2),
("node14", 1),
("node15", 1),
("node16", 4),
("node17", 1),
("node18", 5),
("node19", 4),
("node20", 1),
("node21", 2),
("node22", 1),
("node23", 3),
("node24", 1),
nodes: StatefulList::with_items(vec![
("node0"),
("node1"),
("node2"),
("node3"),
("node4"),
("node5"),
("node6"),
("node7"),
("node8"),
("node9"),
("node10"),
("node11"),
("node12"),
("node13"),
("node14"),
("node15"),
("node16"),
("node17"),
("node18"),
("node19"),
("node20"),
("node21"),
("node22"),
("node23"),
("node24"),
("node0"),
("node1"),
("node2"),
("node3"),
("node4"),
("node5"),
("node6"),
("node7"),
("node8"),
("node9"),
("node10"),
("node11"),
("node12"),
("node13"),
("node14"),
("node15"),
("node16"),
("node17"),
("node18"),
("node19"),
("node20"),
("node21"),
("node22"),
("node23"),
("node24"),
("node0"),
("node1"),
("node2"),
("node3"),
("node4"),
("node5"),
("node6"),
("node7"),
("node8"),
("node9"),
("node10"),
("node11"),
("node12"),
("node13"),
("node14"),
("node15"),
("node16"),
("node17"),
("node18"),
("node19"),
("node20"),
("node21"),
("node22"),
("node23"),
("node24"),
]),
}
}

View File

@@ -1,8 +1,4 @@
// make async task that updates info
// this display that
//use drk::Result;
use std::{
error::Error,
io,
io::Read,
time::{Duration, Instant},
@@ -10,10 +6,6 @@ use std::{
use termion::{async_stdin, event::Key, input::TermRead, raw::IntoRawMode};
use tui::{
backend::{Backend, TermionBackend},
layout::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
text::{Span, Spans, Text},
widgets::{Block, Borders, List, ListItem, ListState, Paragraph, Wrap},
Terminal,
};
@@ -33,7 +25,7 @@ fn main() -> Result<(), io::Error> {
let tick_rate = Duration::from_millis(250);
let app = App::new();
let res = run_app(&mut terminal, app, tick_rate);
terminal.clear()?;
if let Err(err) = res {
@@ -41,85 +33,6 @@ fn main() -> Result<(), io::Error> {
}
Ok(())
// Create a separate thread to poll stdin.
// This provides non-blocking input support.
//let mut asi = async_stdin();
//// Clear the terminal before first draw.
//terminal.clear()?;
//loop {
// // Lock the terminal and start a drawing session.
// terminal.draw(|frame| {
// // Create a layout into which to place our blocks.
// let chunks = Layout::default()
// .direction(Direction::Vertical)
// .constraints([Constraint::Percentage(6), Constraint::Percentage(94)].as_ref())
// .split(frame.size());
// //let size = frame.size();
// // The text lines for our text box.
// let txt = vec![Spans::from("\n Press q to quit.\n")];
// // Create a paragraph with the above text...
// let graph = Paragraph::new(txt)
// // In a block with borders and the given title...
// .block(Block::default().title("").borders(Borders::ALL))
// // With white foreground and black background...
// .style(Style::default().fg(Color::White).bg(Color::Black));
// // Render into the layout.
// frame.render_widget(graph, chunks[0]);
// // create a list
// //let mut items: Vec<ListItem> = Vec::new();
// //for num in 1..100 {
// // let new_item = ListItem::new(format!("Node {}", num));
// // items.push(new_item);
// //}
// //let list = List::new(items)
// // .block(Block::default().title("Nodes").borders(Borders::ALL))
// // .style(Style::default().fg(Color::White))
// // .highlight_style(Style::default().add_modifier(Modifier::ITALIC))
// // .highlight_symbol(">>");
// //// draw a list
// //frame.render_widget(list, chunks[1]);
// // make a paragraph
// let mut text1 = String::new();
// for num in 1..10000 {
// let text2 = format!("\n Node {}\n", num);
// text1.push_str(&text2);
// }
// let text = Spans::from(vec![Span::raw(String::from(text1))]);
// let graph = Paragraph::new(text)
// .block(Block::default().title("").borders(Borders::ALL))
// .style(Style::default().fg(Color::White).bg(Color::Black))
// .scroll((0, 10000))
// .wrap(Wrap { trim: true });
// frame.render_widget(graph, chunks[1]);
// })?;
// // Iterate over all the keys that have been pressed since the
// // last time we checked.
// for k in asi.by_ref().keys() {
// match k.unwrap() {
// // If any of them is q, quit
// Key::Char('q') => {
// // Clear the terminal before exit so as not to leave
// // a mess.
// terminal.clear()?;
// return Ok(())
// }
// Key::Char('j') => {}
// // Otherwise, throw them away.
// _ => (),
// }
// }
//}
}
fn run_app<B: Backend>(
@@ -129,22 +42,20 @@ fn run_app<B: Backend>(
) -> io::Result<()> {
let mut asi = async_stdin();
terminal.clear()?;
let mut last_tick = Instant::now();
loop {
terminal.draw(|f| ui::ui(f, &mut app))?;
for k in asi.by_ref().keys() {
match k.unwrap() {
// If any of them is q, quit
Key::Char('q') => {
// Clear the terminal before exit so as not to leave
// a mess.
terminal.clear()?;
return Ok(())
}
Key::Char('j') => app.items.next(),
Key::Char('k') => app.items.previous(),
// Otherwise, throw them away.
Key::Char('j') => app.nodes.next(),
Key::Char('k') => app.nodes.previous(),
_ => (),
}
}

View File

@@ -1,42 +1,31 @@
use crate::app::App;
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
text::{Span, Spans},
text::{Spans},
widgets::{Block, Borders, List, ListItem},
Frame,
};
pub fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(6), Constraint::Percentage(94)].as_ref())
.split(f.size());
let size = f.size();
// Iterate through all elements in the `items` app and append some debug text to it.
let items: Vec<ListItem> = app
.items
.nodes
.items
.iter()
.map(|i| {
let mut lines = vec![Spans::from(i.0)];
for _ in 0..i.1 {
lines.push(Spans::from(Span::styled(
"Just some random garbage",
Style::default().add_modifier(Modifier::ITALIC),
)));
}
let lines = vec![Spans::from(i.to_string())];
ListItem::new(lines).style(Style::default())
})
.collect();
// Create a List from all list items and highlight the currently selected one
let items = List::new(items)
.block(Block::default().borders(Borders::ALL).title("List"))
.highlight_style(Style::default().bg(Color::LightGreen).add_modifier(Modifier::BOLD))
.block(Block::default().borders(Borders::ALL).title("List of nodes"))
.highlight_style(Style::default().bg(Color::Black).add_modifier(Modifier::BOLD))
.highlight_symbol(">> ");
// Render the item list
f.render_stateful_widget(items, chunks[0], &mut app.items.state);
f.render_stateful_widget(items, size, &mut app.nodes.state);
}