pub struct History { /* private fields */ }
Expand description
This keeps track of the most recent command lines entered.
Implementations§
source§impl History
impl History
sourcepub fn new() -> History
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
sourcepub fn from_file<P: AsRef<Path>>(path: P) -> Result<History>
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.
sourcepub fn read_history_from<P: AsRef<Path>>(&mut self, path: P) -> Result<usize>
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.
sourcepub fn write_history_to<P: AsRef<Path>>(&self, path: P) -> Result<()>
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.
sourcepub fn set_limit(&mut self, limit: Option<NonZeroUsize>) -> &mut History
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.
sourcepub fn set_strip_duplicates(&mut self, strip_duplicates: bool) -> &mut History
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.
sourcepub fn set_autosave_handler(
&mut self,
autosave_handler: Option<Box<dyn Fn(&History) -> Result<()> + Send + Sync>>
) -> &mut History
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.
sourcepub fn set_autosave_interval(
&mut self,
autosave_interval: Option<NonZeroUsize>
) -> &mut History
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.
sourcepub fn add_line(&mut self, line: String) -> Result<()>
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.