Struct liso::History

source ·
pub struct History { /* private fields */ }
Expand description

This keeps track of the most recent command lines entered.

Implementations§

source§

impl History

source

pub fn new() -> History

Create a new, empty History with default options.

The defaults are subject to change, but as of this version, they are:

  • History limit: 100 lines
  • Strip duplicates: yes
  • No autosave handler
  • Autosave only on drop
source

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<History>

Create a new History by reading the given file, with default options.

The defaults are subject to change, but as of this version, they are:

  • History limit: 100 lines.
  • Strip duplicates: yes
  • Autosave to given file, carefully avoiding common pitfalls
  • Autosave only on drop

If the file doesn’t exist yet, the returned history will start out empty. If it can’t be opened for some other reason, will return an error.

source

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

Attempts to read history from the given file. Does not change any settings. Overwrites all current history. Returns the number of lines read.

source

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

Attempts to write history to the given file. Doesn’t have any special logic for removing the file on write error, or backing up the original file, or et cetera. If you didn’t create your History using from_file, that’s up to you to build using this function.

source

pub fn set_limit(&mut self, limit: Option<NonZeroUsize>) -> &mut History

Sets the maximum number of lines that will be saved in the history. If more lines than this are added, the oldest lines will be removed. This is a linear time operation, so don’t set this to an absurdly large value!

This will not take effect until the next time a new history item is added.

source

pub fn set_strip_duplicates(&mut self, strip_duplicates: bool) -> &mut History

If true (default), whenever a new line is added to the history, any existing copies of that line in the history will be deleted.

Existing duplicates will still not be removed unless new copies of those exact lines are added.

source

pub fn set_autosave_handler( &mut self, autosave_handler: Option<Box<dyn Fn(&History) -> Result<()> + Send + Sync>> ) -> &mut History

Sets an autosave handler, to be called when the History is about to be dropped, or on the autosave interval if configured. The old autosave handler, if any, is dropped.

source

pub fn set_autosave_interval( &mut self, autosave_interval: Option<NonZeroUsize> ) -> &mut History

Sets an autosave interval. With interval Some(N), an autosave will be attempted after every N lines are added to the history. With interval None, autosaving will only be performed when the History is dropped.

source

pub fn add_line(&mut self, line: String) -> Result<()>

Add a new line to the end of the history.

If duplicates are being stripped, will strip identical copies of this line from the history. If there is a limit on the number of lines in the history, this may remove the oldest history elements. If there is an autosave handler and an autosave interval, this may autosave the history.

Returns an error if autosaving failed.

source

pub fn get_lines(&self) -> &[String]

Returns all the lines currently in the history.

Trait Implementations§

source§

impl Default for History

source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.