Styling
This commit is contained in:
parent
ccaf8c1f0b
commit
908fe2f166
4 changed files with 52 additions and 19 deletions
29
src/fps_counter.rs
Normal file
29
src/fps_counter.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
pub struct FpsCounter {
|
||||||
|
frame_count: u64,
|
||||||
|
last_tick: std::time::Instant,
|
||||||
|
pub fps: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FpsCounter {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
frame_count: 0,
|
||||||
|
last_tick: std::time::Instant::now(),
|
||||||
|
fps: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn tick(&mut self) {
|
||||||
|
self.frame_count += 1;
|
||||||
|
if self.last_tick.elapsed().as_secs() >= 1 {
|
||||||
|
self.fps = self.frame_count;
|
||||||
|
self.frame_count = 0;
|
||||||
|
self.last_tick = std::time::Instant::now();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for FpsCounter {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ use ratatui::{
|
||||||
DefaultTerminal, Frame,
|
DefaultTerminal, Frame,
|
||||||
};
|
};
|
||||||
/// Create a vertical bar chart from the temperatures data.
|
/// Create a vertical bar chart from the temperatures data.
|
||||||
pub fn vertical_barchart(temperatures: &[u32]) -> BarChart {
|
pub fn vertical_barchart(temperatures: &[u32], terminal_width: u16) -> BarChart {
|
||||||
let bars: Vec<Bar> = temperatures
|
let bars: Vec<Bar> = temperatures
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
|
@ -15,7 +15,7 @@ pub fn vertical_barchart(temperatures: &[u32]) -> BarChart {
|
||||||
.collect();
|
.collect();
|
||||||
BarChart::default()
|
BarChart::default()
|
||||||
.data(BarGroup::default().bars(&bars))
|
.data(BarGroup::default().bars(&bars))
|
||||||
.bar_width(1)
|
.bar_width(terminal_width / temperatures.len() as u16)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn vertical_bar(hour: usize, temperature: &u32) -> Bar {
|
pub fn vertical_bar(hour: usize, temperature: &u32) -> Bar {
|
||||||
|
|
|
@ -13,7 +13,7 @@ use tui::Tui;
|
||||||
|
|
||||||
mod graphics;
|
mod graphics;
|
||||||
mod drawableVec;
|
mod drawableVec;
|
||||||
|
mod fps_counter;
|
||||||
mod algorithm;
|
mod algorithm;
|
||||||
mod tui;
|
mod tui;
|
||||||
|
|
||||||
|
|
36
src/tui.rs
36
src/tui.rs
|
@ -1,13 +1,10 @@
|
||||||
use std::time::Duration;
|
use std::{fmt::format, time::Duration};
|
||||||
|
|
||||||
use color_eyre::{Result, eyre::Ok};
|
use color_eyre::{Result, eyre::Ok};
|
||||||
use crossterm::event::{Event, KeyCode, KeyEventKind, MouseEventKind};
|
use crossterm::event::{Event, KeyCode, KeyEventKind, MouseEventKind};
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
Terminal,
|
layout::{Constraint, Layout, Margin}, prelude::CrosstermBackend, style::Stylize, widgets::Block, Terminal
|
||||||
layout::{Constraint, Layout},
|
|
||||||
prelude::CrosstermBackend,
|
|
||||||
style::Stylize,
|
|
||||||
};
|
};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
sync::mpsc::{self, UnboundedReceiver, UnboundedSender},
|
sync::mpsc::{self, UnboundedReceiver, UnboundedSender},
|
||||||
|
@ -32,9 +29,11 @@ pub struct Tui {
|
||||||
sorting_tx: UnboundedSender<SortingMessage>,
|
sorting_tx: UnboundedSender<SortingMessage>,
|
||||||
should_exit: bool,
|
should_exit: bool,
|
||||||
vec: Vec<u32>,
|
vec: Vec<u32>,
|
||||||
|
fps_counter: FpsCounter,
|
||||||
}
|
}
|
||||||
use crate::{
|
use crate::{
|
||||||
drawableVec::{self, DrawableVec, SortingMessage},
|
drawableVec::{self, DrawableVec, SortingMessage},
|
||||||
|
fps_counter::FpsCounter,
|
||||||
graphics::vertical_barchart,
|
graphics::vertical_barchart,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -50,7 +49,7 @@ impl Tui {
|
||||||
let (event_tx, event_rx): (UnboundedSender<Message>, UnboundedReceiver<Message>) =
|
let (event_tx, event_rx): (UnboundedSender<Message>, UnboundedReceiver<Message>) =
|
||||||
mpsc::unbounded_channel();
|
mpsc::unbounded_channel();
|
||||||
let (sorting_tx, sorting_rx) = mpsc::unbounded_channel();
|
let (sorting_tx, sorting_rx) = mpsc::unbounded_channel();
|
||||||
let mut vec = (Vec::from_iter(1..100));
|
let mut vec = (Vec::from_iter(1..50));
|
||||||
vec.shuffle(&mut rand::rng());
|
vec.shuffle(&mut rand::rng());
|
||||||
let drawableVec = DrawableVec::new(event_tx.clone(), sorting_rx, vec.clone());
|
let drawableVec = DrawableVec::new(event_tx.clone(), sorting_rx, vec.clone());
|
||||||
|
|
||||||
|
@ -58,13 +57,14 @@ impl Tui {
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
terminal,
|
terminal,
|
||||||
tick_rate: 20.,
|
tick_rate: 30.,
|
||||||
frame_rate: 120.,
|
frame_rate: 60.,
|
||||||
event_rx,
|
event_rx,
|
||||||
event_tx,
|
event_tx,
|
||||||
sorting_tx,
|
sorting_tx,
|
||||||
should_exit: false,
|
should_exit: false,
|
||||||
vec: vec,
|
vec: vec,
|
||||||
|
fps_counter: FpsCounter::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,6 +111,7 @@ impl Tui {
|
||||||
Message::Tick => Ok(UpdateCommand::None),
|
Message::Tick => Ok(UpdateCommand::None),
|
||||||
Message::Render => {
|
Message::Render => {
|
||||||
self.view()?;
|
self.view()?;
|
||||||
|
self.fps_counter.tick();
|
||||||
Ok(UpdateCommand::None)
|
Ok(UpdateCommand::None)
|
||||||
}
|
}
|
||||||
Message::StartSimulation => todo!(),
|
Message::StartSimulation => todo!(),
|
||||||
|
@ -127,15 +128,18 @@ impl Tui {
|
||||||
|
|
||||||
fn view(&mut self) -> Result<()> {
|
fn view(&mut self) -> Result<()> {
|
||||||
self.terminal.draw(|frame| {
|
self.terminal.draw(|frame| {
|
||||||
let [title, vertical, horizontal] = Layout::vertical([
|
let [title, vertical] =
|
||||||
Constraint::Length(1),
|
Layout::vertical([Constraint::Length(1), Constraint::Fill(1)])
|
||||||
ratatui::layout::Constraint::Fill(1),
|
.spacing(1)
|
||||||
Constraint::Fill(1),
|
.areas(frame.area());
|
||||||
])
|
|
||||||
.spacing(1)
|
|
||||||
.areas(frame.area());
|
|
||||||
frame.render_widget("Sorting!".bold().into_centered_line(), title);
|
frame.render_widget("Sorting!".bold().into_centered_line(), title);
|
||||||
frame.render_widget(vertical_barchart(&self.vec), horizontal);
|
frame.render_widget(
|
||||||
|
vertical_barchart(&self.vec, vertical.inner(Margin::new(10, 0)).width).block(
|
||||||
|
Block::bordered()
|
||||||
|
.title(format!(" Esc to Quit. FPS: {} ", self.fps_counter.fps)),
|
||||||
|
),
|
||||||
|
vertical,
|
||||||
|
);
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue