pub struct History {
pub limit_size: Option<usize>,
/* private fields */
}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
impl History
Sourcepub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>
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.
Sourcepub fn load_from_file<P: AsRef<Path>>(
path: P,
limit_size: Option<usize>,
) -> Result<Self>
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.
Sourcepub fn insert<T: AsRef<str>>(&mut self, item: T)
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", ""]
Sourcepub fn get(&self) -> String
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.
Sourcepub fn backward(&mut self) -> bool
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.
Sourcepub fn forward(&mut self) -> bool
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.
Sourcepub fn move_to_tail(&mut self)
pub fn move_to_tail(&mut self)
Moves the current position to the tail (end) of the history buffer.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for History
impl RefUnwindSafe for History
impl Send for History
impl Sync for History
impl Unpin for History
impl UnsafeUnpin for History
impl UnwindSafe for History
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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