Skip to content

Commit

Permalink
feat: add move_{up,down,left,right} fns to state (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka authored Nov 28, 2024
1 parent c51ac3c commit 58371fa
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 48 deletions.
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@
//! - [tui-popup](https://crates.io/crates/tui-popup)
//! - [tui-prompts](https://crates.io/crates/tui-prompts)
//! - [tui-scrollview](https://crates.io/crates/tui-scrollview)
//!
#![doc = document_features::document_features!()]

#[cfg(feature = "big-text")]
#[doc(inline)]
pub use tui_big_text as big_text;

#[cfg(feature = "popup")]
#[doc(inline)]
pub use tui_popup as popup;

#[cfg(feature = "prompts")]
#[doc(inline)]
pub use tui_prompts as prompts;

#[cfg(feature = "scrollview")]
#[doc(inline)]
pub use tui_scrollview as scrollview;
3 changes: 2 additions & 1 deletion tui-big-text/src/big_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,10 @@ fn render_glyph(glyph: [u8; 8], area: Rect, buf: &mut Buffer, pixel_size: &Pixel

#[cfg(test)]
mod tests {
use super::*;
use ratatui_core::style::Stylize;

use super::*;

#[test]
fn build() {
let lines = vec![Line::from(vec!["Hello".red(), "World".blue()])];
Expand Down
12 changes: 4 additions & 8 deletions tui-box-text/examples/box_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::iter::zip;

use color_eyre::eyre::Ok;
use ratatui::{
crossterm::event::{self, Event, KeyCode, KeyEventKind},
crossterm::event::{self, Event, KeyCode},
layout::{Constraint, Layout, Rect},
text::Line,
DefaultTerminal, Frame,
Expand All @@ -19,13 +19,9 @@ fn main() -> color_eyre::Result<()> {

fn run(mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
loop {
terminal.draw(|frame| draw(frame))?;
match event::read()? {
Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
KeyCode::Esc => break Ok(()),
_ => {}
},
_ => {}
terminal.draw(draw)?;
if matches!(event::read()?, Event::Key(key) if key.code == KeyCode::Esc) {
break Ok(());
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions tui-popup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ fn render_stateful_popup(frame: &mut Frame, popup_state: &mut PopupState) {
frame.render_stateful_widget_ref(popup, frame.size(), popup_state);
}

fn move_up(popup_state: &mut PopupState) {
popup_state.move_by(0, -1);
fn handle_key(event: KeyEvent, &mut state) {
match event.code {
KeyCode::Up => state.move_up(1),
KeyCode::Down => state.move_down(1),
KeyCode::Left => state.move_left(1),
KeyCode::Right => state.move_right(1),
}
}
```

Expand Down
9 changes: 4 additions & 5 deletions tui-popup/examples/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ fn handle_key_event(event: KeyEvent, popup: &mut PopupState, exit: &mut bool) {
match event.code {
KeyCode::Char('q') | KeyCode::Esc => *exit = true,
KeyCode::Char('r') => *popup = PopupState::default(),
// TODO: move handling to PopupState (e.g. move_up, move_down, etc. or move(Move:Up))
KeyCode::Char('j') | KeyCode::Down => popup.move_by(0, 1),
KeyCode::Char('k') | KeyCode::Up => popup.move_by(0, -1),
KeyCode::Char('h') | KeyCode::Left => popup.move_by(-1, 0),
KeyCode::Char('l') | KeyCode::Right => popup.move_by(1, 0),
KeyCode::Char('j') | KeyCode::Down => popup.move_down(1),
KeyCode::Char('k') | KeyCode::Up => popup.move_up(1),
KeyCode::Char('h') | KeyCode::Left => popup.move_left(1),
KeyCode::Char('l') | KeyCode::Right => popup.move_right(1),
_ => {}
}
}
6 changes: 3 additions & 3 deletions tui-popup/src/known_size_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<W> KnownSize for &KnownSizeWrapper<W> {

impl<W> KnownSizeWrapper<W> {
/// Create a new `KnownSizeWrapper` with the given widget and size.
pub fn new(inner: W, width: usize, height: usize) -> Self {
pub const fn new(inner: W, width: usize, height: usize) -> Self {
Self {
inner,
width,
Expand All @@ -62,8 +62,8 @@ mod tests {
struct TestWidget;

impl WidgetRef for TestWidget {
fn render_ref(&self, _area: Rect, _buf: &mut Buffer) {
"Hello".render_ref(_area, _buf);
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
"Hello".render_ref(area, buf);
}
}

Expand Down
6 changes: 3 additions & 3 deletions tui-popup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
//! # Example
//!
//! ```rust
//! use ratatui::prelude::*;
//! use ratatui::{Frame, style::{Style, Stylize}};
//! use tui_popup::Popup;
//!
//! fn render_popup(frame: &mut Frame) {
//! let popup = Popup::new("Press any key to exit")
//! .title("tui-popup demo")
//! .style(Style::new().white().on_blue());
//! frame.render_widget(&popup, frame.size());
//! .style(Style::new().white().on_blue());
//! frame.render_widget(&popup, frame.area());
//! }
//! ```
//!
Expand Down
2 changes: 1 addition & 1 deletion tui-popup/src/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ mod tests {
title: Line::default(),
style: Style::default(),
}
)
);
}

#[test]
Expand Down
19 changes: 17 additions & 2 deletions tui-popup/src/popup_state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use derive_getters::Getters;
use ratatui::prelude::Rect;

#[cfg(feature = "crossterm")]
use ratatui::crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
use ratatui::prelude::Rect;

#[derive(Clone, Debug, Default, Getters)]
pub struct PopupState {
Expand All @@ -23,6 +22,22 @@ pub enum DragState {
}

impl PopupState {
pub fn move_up(&mut self, amount: u16) {
self.move_by(0, -i32::from(amount));
}

pub fn move_down(&mut self, amount: u16) {
self.move_by(0, i32::from(amount));
}

pub fn move_left(&mut self, amount: u16) {
self.move_by(-i32::from(amount), 0);
}

pub fn move_right(&mut self, amount: u16) {
self.move_by(i32::from(amount), 0);
}

/// Move the popup by the given amount.
pub fn move_by(&mut self, x: i32, y: i32) {
if let Some(area) = self.area {
Expand Down
9 changes: 1 addition & 8 deletions tui-prompts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ mod text_state;

pub use prompt::*;
pub use status::*;

pub use text_prompt::*;
pub use text_state::*;

pub mod prelude {
pub use crate::FocusState;
pub use crate::Prompt;
pub use crate::State;
pub use crate::Status;
pub use crate::TextPrompt;
pub use crate::TextRenderStyle;
pub use crate::TextState;
pub use crate::{FocusState, Prompt, State, Status, TextPrompt, TextRenderStyle, TextState};
}
10 changes: 6 additions & 4 deletions tui-prompts/src/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::iter::once;

use crate::Status;
use itertools::chain;
use ratatui::{
crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
prelude::*,
widgets::StatefulWidget,
};

use crate::Status;

/// A prompt that can be drawn to a terminal.
pub trait Prompt: StatefulWidget {
/// Draws the prompt widget.
Expand Down Expand Up @@ -184,9 +185,10 @@ pub trait State {
if self.position() == self.len() {
self.value_mut().push(c);
} else {
// We cannot use String::insert() as it operates on bytes, which can lead to incorrect modifications with
// multibyte characters. Instead, we handle text manipulation at the character level using Rust's char type
// for Unicode correctness. Check docs of String::insert() and String::chars() for futher info.
// We cannot use String::insert() as it operates on bytes, which can lead to incorrect
// modifications with multibyte characters. Instead, we handle text
// manipulation at the character level using Rust's char type for Unicode
// correctness. Check docs of String::insert() and String::chars() for futher info.
*self.value_mut() = chain![
self.value().chars().take(self.position()),
once(c),
Expand Down
8 changes: 4 additions & 4 deletions tui-prompts/src/text_prompt.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{borrow::Cow, vec};

use crate::prelude::*;

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

use crate::prelude::*;

// TODO style the widget
// TODO style each element of the widget.
// TODO handle multi-line input.
Expand Down Expand Up @@ -196,12 +196,12 @@ where

#[cfg(test)]
mod tests {
use crate::Status;
use ratatui::{backend::TestBackend, widgets::Borders};
use ratatui_macros::line;
use rstest::{fixture, rstest};

use super::*;
use ratatui::{backend::TestBackend, widgets::Borders};
use crate::Status;

#[test]
fn new() {
Expand Down
6 changes: 3 additions & 3 deletions tui-scrollview/src/scroll_view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use ratatui_core::{
buffer::Buffer,
layout::Rect,
layout::Size,
layout::{Rect, Size},
widgets::{StatefulWidget, Widget},
};
use ratatui_widgets::scrollbar::{Scrollbar, ScrollbarOrientation, ScrollbarState};
Expand Down Expand Up @@ -345,10 +344,11 @@ impl ScrollView {

#[cfg(test)]
mod tests {
use super::*;
use ratatui_core::text::Span;
use rstest::{fixture, rstest};

use super::*;

/// Initialize a buffer and a scroll view with a buffer size of 10x10
///
/// The buffer will be filled with characters from A to Z in a 10x10 grid
Expand Down

0 comments on commit 58371fa

Please sign in to comment.