Skip to content

Commit

Permalink
refactor lines for docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed Feb 17, 2024
1 parent 366e75f commit 33c1bc8
Show file tree
Hide file tree
Showing 27 changed files with 260 additions and 125 deletions.
3 changes: 2 additions & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pub mod text;
pub mod text_editor;
pub mod tree;

/// Defines a `Len` trait for obtaining the length of a collection and checking if it is empty.
/// Defines a `Len` trait for obtaining the length of a collection
/// and checking if it is empty.
pub trait Len {
/// Returns the length of the collection.
fn len(&self) -> usize;
Expand Down
13 changes: 9 additions & 4 deletions src/core/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ use crate::core::listbox::Listbox;
mod render;
pub use render::Renderer;

/// A `Checkbox` struct that encapsulates a listbox for item selection and a set of picked (selected) indices.
/// It allows for multiple selections, toggling the selection state of items, and navigating through the items.
/// A `Checkbox` struct that encapsulates a listbox
/// for item selection and a set of picked (selected) indices.
/// It allows for multiple selections,
/// toggling the selection state of items,
/// and navigating through the items.
#[derive(Clone)]
pub struct Checkbox {
listbox: Listbox,
picked: HashSet<usize>,
}

impl<T: fmt::Display> FromIterator<T> for Checkbox {
/// Creates a `Checkbox` from an iterator of items that implement the `Display` trait.
/// Each item is added to the listbox, and the set of picked indices is initialized as empty.
/// Creates a `Checkbox` from an iterator of items
/// that implement the `Display` trait.
/// Each item is added to the listbox,
/// and the set of picked indices is initialized as empty.
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Self {
listbox: Listbox::from_iter(iter),
Expand Down
9 changes: 6 additions & 3 deletions src/core/checkbox/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ use crate::{

use super::Checkbox;

/// Represents a renderer for the `Checkbox` component, capable of visualizing checkboxes in a pane.
/// It supports custom symbols for the cursor and checkmark, styles for active and inactive items,
/// and a configurable number of lines for rendering. It also handles key events for navigation and toggling checkboxes.
/// Represents a renderer for the `Checkbox` component,
/// capable of visualizing checkboxes in a pane.
/// It supports custom symbols for the cursor and checkmark,
/// styles for active and inactive items,
/// and a configurable number of lines for rendering.
/// It also handles key events for navigation and toggling checkboxes.
#[derive(Clone)]
pub struct Renderer {
/// The `Checkbox` component to be rendered.
Expand Down
6 changes: 4 additions & 2 deletions src/core/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::core::Len;

/// A generic cursor structure for navigating and manipulating collections.
/// It maintains a position within the collection and provides methods to move forward, backward,
/// to the head, and to the tail of the collection. It requires the collection to implement the `Len` trait.
/// It maintains a position within the collection
/// and provides methods to move forward, backward,
/// to the head, and to the tail of the collection.
/// It requires the collection to implement the `Len` trait.
#[derive(Clone)]
pub struct Cursor<C> {
contents: C,
Expand Down
18 changes: 12 additions & 6 deletions src/core/listbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ use crate::core::cursor::Cursor;
mod render;
pub use render::Renderer;

/// A `Listbox` struct that encapsulates a list of strings, allowing for navigation and manipulation
/// through a cursor. It supports basic operations such as moving the cursor forward and backward,
/// retrieving the current item, and initializing from an iterator of displayable items.
/// A `Listbox` struct that encapsulates a list of strings,
/// allowing for navigation and manipulation through a cursor.
/// It supports basic operations
/// such as moving the cursor forward and backward,
/// retrieving the current item,
/// and initializing from an iterator of displayable items.
#[derive(Clone)]
pub struct Listbox(Cursor<Vec<String>>);

impl<T: fmt::Display> FromIterator<T> for Listbox {
/// Creates a `Listbox` from an iterator of items that implement the `Display` trait.
/// Each item is converted to a `String` and collected into a `Vec<String>`.
/// Creates a `Listbox` from an iterator of items
/// that implement the `Display` trait.
/// Each item is converted to a `String`
/// and collected into a `Vec<String>`.
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Self(Cursor::new(
iter.into_iter().map(|e| format!("{}", e)).collect(),
Expand All @@ -33,7 +38,8 @@ impl Listbox {
}

/// Retrieves the item at the current cursor position as a `String`.
/// If the cursor is at a position without an item, returns an empty `String`.
/// If the cursor is at a position without an item,
/// returns an empty `String`.
pub fn get(&self) -> String {
self.items()
.get(self.position())
Expand Down
6 changes: 4 additions & 2 deletions src/core/listbox/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ use crate::{

use super::Listbox;

/// Represents a renderer for the `Listbox` component, capable of visualizing a list of items in a pane.
/// It supports a custom symbol for the selected line, styles for active and inactive items,
/// Represents a renderer for the `Listbox` component,
/// capable of visualizing a list of items in a pane.
/// It supports a custom symbol for the selected line,
/// styles for active and inactive items,
/// and a configurable number of lines for rendering.
#[derive(Clone)]
pub struct Renderer {
Expand Down
3 changes: 2 additions & 1 deletion src/core/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub use suggest::Suggest;
mod mode;
pub use mode::Mode;

/// A text editor that supports basic editing operations such as insert, delete, and overwrite.
/// A text editor that supports basic editing operations
/// such as insert, delete, and overwrite.
/// It utilizes a cursor to navigate and manipulate the text.
#[derive(Clone)]
pub struct TextEditor(Cursor<String>);
Expand Down
18 changes: 12 additions & 6 deletions src/core/text_editor/history.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// Manages the history of user inputs for a text editor.
/// This structure allows for the storage, retrieval, and navigation through past inputs.
/// It supports adding new entries, checking for the existence of specific entries,
/// This structure allows for the storage,
/// retrieval, and navigation through past inputs.
/// It supports adding new entries,
/// checking for the existence of specific entries,
/// and moving through the history in both forward and backward directions.
#[derive(Clone)]
pub struct History {
Expand All @@ -24,9 +26,12 @@ impl Default for History {
impl History {
/// Inserts a new item into the history.
///
/// If the item does not already exist in the buffer, it is inserted just before the last item.
/// This method ensures there is always an empty string at the end of the buffer to represent
/// a new input line. After insertion, the current position is moved to the end of the buffer.
/// If the item does not already exist in the buffer,
/// it is inserted just before the last item.
/// This method ensures there is always an empty string
/// at the end of the buffer to represent
/// a new input line. After insertion,
/// the current position is moved to the end of the buffer.
///
/// # Arguments
///
Expand All @@ -50,7 +55,8 @@ impl History {
}
}

/// Retrieves the current item from the history based on the current position.
/// Retrieves the current item from the history
/// based on the current position.
/// Returns an empty string if the position is out of bounds.
pub fn get(&self) -> String {
self.buf
Expand Down
12 changes: 8 additions & 4 deletions src/core/text_editor/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ use crate::{

use super::{History, Mode, Suggest, TextEditor};

/// Represents a renderer for the `TextEditor` component, capable of visualizing text input in a pane.
/// It supports a variety of features including history navigation, input suggestions, input masking,
/// customizable prompt strings, and styles for different parts of the input. It also handles different
/// edit modes such as insert and overwrite, and can be configured to render a specific number of lines.
/// Represents a renderer for the `TextEditor` component,
/// capable of visualizing text input in a pane.
/// It supports a variety of features including history navigation,
/// input suggestions, input masking,
/// customizable prompt strings,
/// and styles for different parts of the input. It also handles different
/// edit modes such as insert and overwrite,
/// and can be configured to render a specific number of lines.
#[derive(Clone)]
pub struct Renderer {
/// The `TextEditor` component to be rendered.
Expand Down
6 changes: 4 additions & 2 deletions src/core/tree/node.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/// A `Node` struct that represents a single node in a tree structure.
/// It contains data as a `String`, a list of child nodes, and a visibility flag for its children.
/// It contains data as a `String`,
/// a list of child nodes, and a visibility flag for its children.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Node {
data: String,
children: Vec<Node>,
children_visible: bool,
}

/// A `NodeWithDepth` struct that represents a node along with its depth information.
/// A `NodeWithDepth` struct that represents a node
/// along with its depth information.
/// It is used for flattening a tree structure into a linear representation.
#[derive(Clone, Debug, PartialEq)]
pub struct NodeWithDepth {
Expand Down
9 changes: 6 additions & 3 deletions src/core/tree/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ use crate::{
tree::{NodeWithDepth, Tree},
};

/// Represents a renderer for a tree structure, capable of visualizing the tree in a pane.
/// It supports custom symbols for folded and unfolded items, styles for active and inactive items,
/// and a configurable number of lines for rendering. It also handles key events for navigation and folding/unfolding.
/// Represents a renderer for a tree structure,
/// capable of visualizing the tree in a pane.
/// It supports custom symbols for folded and unfolded items,
/// styles for active and inactive items,
/// and a configurable number of lines for rendering.
/// It also handles key events for navigation and folding/unfolding.
#[derive(Clone)]
pub struct Renderer {
pub tree: Tree,
Expand Down
33 changes: 22 additions & 11 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ use crate::{
error::{Error, Result},
};

/// Provides functionality for executing terminal commands and managing terminal state.
/// Provides functionality for executing terminal commands
/// and managing terminal state.
///
/// The `Engine` struct encapsulates methods for interacting with the terminal, such as moving the cursor,
/// clearing the screen, writing text, and more. It leverages the `crossterm` crate to provide cross-platform
/// The `Engine` struct encapsulates methods for interacting
/// with the terminal, such as moving the cursor,
/// clearing the screen, writing text, and more.
/// It leverages the `crossterm` crate to provide cross-platform
/// terminal operations.
#[derive(Clone)]
pub struct Engine<W: Write> {
Expand Down Expand Up @@ -54,16 +57,19 @@ impl<W: Write> Engine<W> {
///
/// # Returns
///
/// Returns a `Result` indicating the success of the operation, or an `Error` if it fails.
/// Returns a `Result` indicating the success of the operation,
/// or an `Error` if it fails.
pub fn clear_from_cursor_down(&mut self) -> Result {
execute!(self.out, Clear(ClearType::FromCursorDown)).map_err(Error::from)
}

/// Clears the entire terminal screen and moves the cursor to the top-left position.
/// Clears the entire terminal screen
/// and moves the cursor to the top-left position.
///
/// # Returns
///
/// Returns a `Result` indicating the success of the operation, or an `Error` if it fails.
/// Returns a `Result` indicating the success of the operation,
/// or an `Error` if it fails.
pub fn clear(&mut self) -> Result {
execute!(self.out, Clear(ClearType::All), MoveTo(0, 0)).map_err(Error::from)
}
Expand All @@ -76,7 +82,8 @@ impl<W: Write> Engine<W> {
///
/// # Returns
///
/// Returns a `Result` indicating the success of the operation, or an `Error` if it fails.
/// Returns a `Result` indicating the success of the operation,
/// or an `Error` if it fails.
pub fn write<D: fmt::Display>(&mut self, string: D) -> Result {
execute!(self.out, Print(string)).map_err(Error::from)
}
Expand All @@ -89,7 +96,8 @@ impl<W: Write> Engine<W> {
///
/// # Returns
///
/// Returns a `Result` indicating the success of the operation, or an `Error` if it fails.
/// Returns a `Result` indicating the success of the operation,
/// or an `Error` if it fails.
pub fn move_to(&mut self, pos: (u16, u16)) -> Result {
execute!(self.out, MoveTo(pos.0, pos.1)).map_err(Error::from)
}
Expand All @@ -98,7 +106,8 @@ impl<W: Write> Engine<W> {
///
/// # Returns
///
/// Returns a `Result` containing `true` if the cursor is at the bottom, otherwise `false`.
/// Returns a `Result` containing `true`
/// if the cursor is at the bottom, otherwise `false`.
pub fn is_bottom(&self) -> Result<bool> {
Ok(cursor::position()?.1 + 1 == terminal::size()?.1)
}
Expand All @@ -111,7 +120,8 @@ impl<W: Write> Engine<W> {
///
/// # Returns
///
/// Returns a `Result` indicating the success of the operation, or an `Error` if it fails.
/// Returns a `Result` indicating the success of the operation,
/// or an `Error` if it fails.
pub fn scroll_up(&mut self, times: u16) -> Result {
execute!(self.out, ScrollUp(times)).map_err(Error::from)
}
Expand All @@ -120,7 +130,8 @@ impl<W: Write> Engine<W> {
///
/// # Returns
///
/// Returns a `Result` indicating the success of the operation, or an `Error` if it fails.
/// Returns a `Result` indicating the success of the operation,
/// or an `Error` if it fails.
pub fn move_to_next_line(&mut self) -> Result {
execute!(self.out, cursor::MoveToNextLine(1)).map_err(Error::from)
}
Expand Down
30 changes: 21 additions & 9 deletions src/grapheme.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
//! Manages characters and their display widths within a terminal interface.
//!
//! This module provides structures and functions for handling graphemes (characters and their associated display widths)
//! in terminal applications. It is designed to accurately manage cursor positions and text rendering, especially when dealing
//! with wide characters such as emojis or special symbols that occupy more than one column in terminal displays.
//! This module provides structures
//! and functions for handling graphemes
//! (characters and their associated display widths)
//! in terminal applications.
//! It is designed to accurately manage cursor positions
//! and text rendering, especially when dealing
//! with wide characters such as emojis
//! or special symbols that occupy more than one column in terminal displays.
//!
//! # Structures
//!
//! - `Grapheme`: Represents a single character, its display width, and optional styling.
//! - `Graphemes`: A collection of `Grapheme` instances, supporting operations like total width calculation and styling.
//! - `Grapheme`: Represents a single character,
//! its display width, and optional styling.
//! - `Graphemes`: A collection of `Grapheme` instances,
//! supporting operations like total width calculation and styling.
//!
//! # Utility Functions
//!
//! - `matrixify`: Splits a collection of graphemes into lines that fit within a specified width, useful for text wrapping.
//! - `trim`: Trims a collection of graphemes to fit within a specified width, discarding any excess graphemes.
//! - `matrixify`: Splits a collection of graphemes into lines
//! that fit within a specified width, useful for text wrapping.
//! - `trim`: Trims a collection of graphemes
//! to fit within a specified width, discarding any excess graphemes.
//!
//! # Usage
//!
//! This module is intended for use in terminal applications where accurate text rendering and cursor movement are crucial.
//! It leverages the `unicode_width` crate to calculate the display width of characters, ensuring compatibility with a wide
//! This module is intended for use in terminal applications
//! where accurate text rendering and cursor movement are crucial.
//! It leverages the `unicode_width` crate
//! to calculate the display width of characters,
//! ensuring compatibility with a wide
//! range of Unicode characters.
use std::{
fmt::{self, Debug},
Expand Down
Loading

0 comments on commit 33c1bc8

Please sign in to comment.