Skip to main content

History

Struct History 

Source
pub struct History {
    pub limit_size: Option<usize>,
    /* private fields */
}
Available on crate feature texteditor only.
Expand description

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, and moving through the history in both forward and backward directions. Additionally, it can limit the number of entries stored in the history to a specified maximum size.

Fields§

§limit_size: Option<usize>

Optional limit on the number of entries in the history. If set, the history will not exceed this number of entries, and older entries will be removed to make room for new ones.

Implementations§

Source§

impl History

Source

pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>

Saves the current history items to a file in reverse order (newest first), respecting the optional limit on the number of entries if provided.

§Arguments
  • path - The path to the file where the history should be saved.
§Returns

Returns Ok(()) if the history was successfully saved, or an io::Error otherwise.

Source

pub fn load_from_file<P: AsRef<Path>>( path: P, limit_size: Option<usize>, ) -> Result<Self>

Loads history items from a file into a new History instance.

This function reads the specified file line by line, adding each non-empty line to the history. It respects the optional limit on the number of entries if provided. An empty string is always added at the end of the history to represent a new input line. After loading, the cursor is moved to the end of the history.

§Arguments
  • path - The path to the file from which the history should be loaded.
  • limit_size - An optional limit on the number of entries in the history.
§Returns

Returns Ok(History) with the loaded history if successful, or an io::Error otherwise.

Source

pub fn insert<T: AsRef<str>>(&mut self, item: T)

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.

§Arguments
  • item - The item to be inserted into the history.
§Examples
  • Initial state: items = [""]
  • After inserting “abc”: items = ["abc", ""]
  • After inserting “xyz”: items = ["abc", "xyz", ""]
Source

pub fn get(&self) -> String

Retrieves the current item from the history based on the current position. Returns an empty string if the position is out of bounds.

Source

pub fn backward(&mut self) -> bool

Moves the current position backward in the history, if possible. Returns true if the position was successfully moved backward, false otherwise.

Source

pub fn forward(&mut self) -> bool

Moves the current position forward in the history, if possible. Returns true if the position was successfully moved forward, false otherwise.

Source

pub fn move_to_tail(&mut self)

Moves the current position to the tail (end) of the history buffer.

Trait Implementations§

Source§

impl Clone for History

Source§

fn clone(&self) -> History

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for History

Source§

fn default() -> Self

Creates a new History instance with a single empty string in the buffer and initializes the position at 0.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.