Skip to content

Commit

Permalink
chore: deps update and clippy lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Nov 17, 2023
1 parent 3f3e7c8 commit 8d9d24f
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 175 deletions.
344 changes: 195 additions & 149 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
crossterm = "0.26"
itertools = "0.11"
ratatui = "0.22"
crossterm = "0.27.0"
itertools = "0.12.0"
ratatui = "0.24.0"

[dev-dependencies]
clap = { version = "4.3.11", features = ["derive"] }
Expand Down
6 changes: 2 additions & 4 deletions examples/multi_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::Parser;
use color_eyre::Result;
use crossterm::event::{self, Event, KeyEvent};
use ratatui::{prelude::*, widgets::*};
use tui::{Frame, Tui};
use tui::Tui;
use tui_prompts::prelude::*;

#[derive(Parser)]
Expand Down Expand Up @@ -107,8 +107,6 @@ impl<'a> App<'a> {
}

fn handle_key_event(&mut self, key_event: KeyEvent) {
match (key_event.code, key_event.modifiers) {
_ => self.state.handle_key_event(key_event),
}
self.state.handle_key_event(key_event);
}
}
4 changes: 2 additions & 2 deletions examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::Parser;
use color_eyre::Result;
use crossterm::event::{self, Event, KeyEvent, KeyModifiers};
use ratatui::{prelude::*, widgets::*};
use tui::{Frame, Tui};
use tui::Tui;
use tui_prompts::prelude::*;

#[derive(Parser)]
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<'a> App<'a> {
.direction(Direction::Vertical)
.constraints(vec![Length(1), Length(1), Length(1), Length(1)])
.split(prompt_area);
return (areas[0], areas[1], areas[2], areas[3], debug_area);
(areas[0], areas[1], areas[2], areas[3], debug_area)
}

fn draw_text_prompt(&mut self, frame: &mut Frame, username_area: Rect) {
Expand Down
3 changes: 0 additions & 3 deletions examples/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ pub struct Tui {
terminal: Terminal<CrosstermBackend<Stderr>>,
}

/// A new type alias for a ratatui frame to avoid having to specify the backend type.
pub type Frame<'a> = ratatui::Frame<'a, CrosstermBackend<Stderr>>;

impl Tui {
pub fn new() -> Result<Self> {
let terminal = Self::init()?;
Expand Down
8 changes: 3 additions & 5 deletions src/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Status;
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::{prelude::*, widgets::*};
use ratatui::{prelude::*, widgets::StatefulWidget};

/// A prompt that can be drawn to a terminal.
pub trait Prompt: StatefulWidget {
Expand All @@ -11,7 +11,7 @@ pub trait Prompt: StatefulWidget {
///
/// [`StatefulWidget`]: ratatui::widgets::StatefulWidget
/// [`Frame`]: ratatui::Frame
fn draw<B: Backend>(self, frame: &mut Frame<B>, area: Rect, state: &mut Self::State);
fn draw(self, frame: &mut Frame, area: Rect, state: &mut Self::State);
}

/// The focus state of a prompt.
Expand Down Expand Up @@ -99,9 +99,7 @@ pub trait State {
(KeyCode::Delete, _) | (KeyCode::Char('d'), KeyModifiers::CONTROL) => self.delete(),
(KeyCode::Char('k'), KeyModifiers::CONTROL) => self.kill(),
(KeyCode::Char('u'), KeyModifiers::CONTROL) => self.truncate(),
(KeyCode::Char(c), KeyModifiers::NONE) | (KeyCode::Char(c), KeyModifiers::SHIFT) => {
self.push(c)
}
(KeyCode::Char(c), KeyModifiers::NONE | KeyModifiers::SHIFT) => self.push(c),
_ => {}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ pub enum Status {

impl Status {
#[must_use]
pub fn is_pending(&self) -> bool {
pub const fn is_pending(&self) -> bool {
matches!(self, Self::Pending)
}

#[must_use]
pub fn is_aborted(&self) -> bool {
pub const fn is_aborted(&self) -> bool {
matches!(self, Self::Aborted)
}

#[must_use]
pub fn is_done(&self) -> bool {
pub const fn is_done(&self) -> bool {
matches!(self, Self::Done)
}

Expand Down
21 changes: 15 additions & 6 deletions src/text_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{borrow::Cow, vec};
use crate::prelude::*;

use itertools::Itertools;
use ratatui::{prelude::*, widgets::*};
use ratatui::{
prelude::*,
widgets::{Block, Paragraph, StatefulWidget, Widget},
};

// TODO style the widget
// TODO style each element of the widget.
Expand Down Expand Up @@ -71,7 +74,7 @@ impl Prompt for TextPrompt<'_> {
///
/// This is in addition to the `Widget` trait implementation as we need the `Frame` to set the
/// cursor position.
fn draw<B: Backend>(self, frame: &mut Frame<B>, area: Rect, state: &mut Self::State) {
fn draw(self, frame: &mut Frame, area: Rect, state: &mut Self::State) {
frame.render_stateful_widget(self, area, state);
if state.is_focused() {
frame.set_cursor(state.cursor().0, state.cursor().1);
Expand Down Expand Up @@ -104,7 +107,11 @@ impl<'a> StatefulWidget for TextPrompt<'a> {
let position = (state.position() + prompt_length).min(area.area() as usize - 1);
let row = position / width;
let column = position % width;
*state.cursor_mut() = (area.x + column as u16, area.y + row as u16);
// sizes are already constrained to the u16 range
#[allow(clippy::cast_possible_truncation)]
{
*state.cursor_mut() = (area.x + column as u16, area.y + row as u16);
}
Paragraph::new(lines).render(area, buf);
}
}
Expand All @@ -114,7 +121,7 @@ impl<'a> StatefulWidget for TextPrompt<'a> {
/// This is a character based wrap, not a word based wrap.
///
/// TODO: move this into the `Line` type.
fn wrap<'a>(line: Line<'a>, width: usize) -> impl Iterator<Item = Line<'a>> + 'a {
fn wrap(line: Line, width: usize) -> impl Iterator<Item = Line> {
let mut line = line;
std::iter::from_fn(move || {
if line.width() > width {
Expand All @@ -135,7 +142,7 @@ fn wrap<'a>(line: Line<'a>, width: usize) -> impl Iterator<Item = Line<'a>> + 'a
///
/// TODO: move this into the `Line` type.
/// TODO: fix this so that it operates on multi-width characters.
fn line_split_at<'a>(line: Line<'a>, mid: usize) -> (Line<'a>, Line<'a>) {
fn line_split_at(line: Line, mid: usize) -> (Line, Line) {
let mut first = Line::default();
let mut second = Line::default();
first.alignment = line.alignment;
Expand All @@ -161,7 +168,8 @@ fn line_split_at<'a>(line: Line<'a>, mid: usize) -> (Line<'a>, Line<'a>) {
///
/// TODO: move this into the `Span` type.
/// TODO: fix this so that it operates on multi-width characters.
fn span_split_at<'a>(span: Span<'a>, mid: usize) -> (Span<'a>, Span<'a>) {
#[allow(clippy::needless_pass_by_value)]
fn span_split_at(span: Span, mid: usize) -> (Span, Span) {
let (first, second) = span.content.split_at(mid);
let first = Span {
content: Cow::Owned(first.into()),
Expand Down Expand Up @@ -379,6 +387,7 @@ mod tests {
}

#[test]
#[allow(clippy::cognitive_complexity)]
fn draw_wrapped() -> Result<(), Box<dyn std::error::Error>> {
let prompt = TextPrompt::from("prompt");
let mut state = TextState::new().with_value("hello world");
Expand Down

0 comments on commit 8d9d24f

Please sign in to comment.