Skip to content

Commit

Permalink
Update to use ratatui as tui is abandoned
Browse files Browse the repository at this point in the history
  • Loading branch information
fnordpig committed Jun 1, 2023
1 parent 77f942a commit db166be
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 54 deletions.
46 changes: 15 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ crossterm = "^0.26"
rust-embed = "^6.4"
toml = "^0.7"

[dependencies.tui]
version = "^0.19"
default-features = false
features = ["crossterm"]
[dependencies.ratatui]
version = "^0.21"

[dependencies.rand]
version = "^0.8"
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{
de::{self, IntoDeserializer},
Deserialize,
};
use tui::style::{Color, Modifier, Style};
use ratatui::style::{Color, Modifier, Style};

#[derive(Debug, Deserialize)]
#[serde(default)]
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::{
str,
};
use structopt::StructOpt;
use tui::{backend::CrosstermBackend, terminal::Terminal};
use ratatui::{backend::CrosstermBackend, terminal::Terminal};

#[derive(RustEmbed)]
#[folder = "resources/runtime"]
Expand Down Expand Up @@ -156,7 +156,7 @@ enum State {
}

impl State {
fn render_into<B: tui::backend::Backend>(
fn render_into<B: ratatui::backend::Backend>(
&self,
terminal: &mut Terminal<B>,
config: &Config,
Expand Down
32 changes: 16 additions & 16 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use results::Fraction;
use std::iter;
use tui::{
use ratatui::{
buffer::Buffer,
layout::{Constraint, Direction, Layout, Rect},
symbols::Marker,
text::{Span, Spans, Text},
text::{Span, Line, Text},
widgets::{Axis, Block, BorderType, Borders, Chart, Dataset, GraphType, Paragraph, Widget},
};

Expand Down Expand Up @@ -39,10 +39,10 @@ trait DrawInner<T> {
fn draw_inner(&self, content: T, buf: &mut Buffer);
}

impl DrawInner<&Spans<'_>> for SizedBlock<'_> {
fn draw_inner(&self, content: &Spans, buf: &mut Buffer) {
impl DrawInner<&Line<'_>> for SizedBlock<'_> {
fn draw_inner(&self, content: &Line, buf: &mut Buffer) {
let inner = self.block.inner(self.area);
buf.set_spans(inner.x, inner.y, content, inner.width);
buf.set_line(inner.x, inner.y, content, inner.width);
}
}

Expand Down Expand Up @@ -88,19 +88,19 @@ impl ThemedWidget for &Test {
// Sections
let input = SizedBlock {
block: Block::default()
.title(Spans::from(vec![Span::styled("Input", theme.title)]))
.title(Line::from(vec![Span::styled("Input", theme.title)]))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(theme.input_border),
area: chunks[0],
};
input.draw_inner(
&Spans::from(self.words[self.current_word].progress.clone()),
&Line::from(self.words[self.current_word].progress.clone()),
buf,
);
input.render(buf);

let target_lines: Vec<Spans> = {
let target_lines: Vec<Line> = {
let words = iter::empty::<Vec<Span>>()
// already typed words
.chain(self.words[..self.current_word].iter().map(|w| {
Expand Down Expand Up @@ -158,23 +158,23 @@ impl ThemedWidget for &Test {
.map(|w| vec![Span::styled(w.text.clone() + " ", theme.prompt_untyped)]),
);

let mut lines: Vec<Spans> = Vec::new();
let mut lines: Vec<Line> = Vec::new();
let mut current_line: Vec<Span> = Vec::new();
let mut current_width = 0;
for word in words {
let word_width: usize = word.iter().map(|s| s.width()).sum();

if current_width + word_width > chunks[1].width as usize - 2 {
current_line.push(Span::raw("\n"));
lines.push(Spans::from(current_line.clone()));
lines.push(Line::from(current_line.clone()));
current_line.clear();
current_width = 0;
}

current_line.extend(word);
current_width += word_width;
}
lines.push(Spans::from(current_line));
lines.push(Line::from(current_line));

lines
};
Expand Down Expand Up @@ -217,19 +217,19 @@ impl ThemedWidget for &results::Results {
// Sections
let mut overview_text = Text::styled("", theme.results_overview);
overview_text.extend([
Spans::from(format!(
Line::from(format!(
"Adjusted WPM: {:.1}",
self.timing.overall_cps * WPM_PER_CPS * f64::from(self.accuracy.overall)
)),
Spans::from(format!(
Line::from(format!(
"Accuracy: {:.1}%",
f64::from(self.accuracy.overall) * 100f64
)),
Spans::from(format!(
Line::from(format!(
"Raw WPM: {:.1}",
self.timing.overall_cps * WPM_PER_CPS
)),
Spans::from(format!("Correct Keypresses: {}", self.accuracy.overall)),
Line::from(format!("Correct Keypresses: {}", self.accuracy.overall)),
]);
let overview = Paragraph::new(overview_text).block(
Block::default()
Expand Down Expand Up @@ -264,7 +264,7 @@ impl ThemedWidget for &results::Results {
None
}
})
.map(Spans::from),
.map(Line::from),
);
let worst = Paragraph::new(worst_text).block(
Block::default()
Expand Down

0 comments on commit db166be

Please sign in to comment.