cmd2.history
cmd2.history
History management classes
HistoryItem
dataclass
Class used to represent one command in the history list
expanded
property
Return the command as run which includes shortcuts and aliases resolved plus any changes made in hooks
Proxy property for self.statement.expanded_command_line
pr
Represent this item in a pretty fashion suitable for printing.
If you pass verbose=True, script and expanded will be ignored
:param idx: The 1-based index of this item in the history list :param script: True if formatting for a script (No item numbers) :param expanded: True if expanded command line should be printed :param verbose: True if expanded and raw should both appear when they are different :return: pretty print string version of a HistoryItem
| PARAMETER | DESCRIPTION |
|---|---|
idx
|
TYPE:
|
script
|
TYPE:
|
expanded
|
TYPE:
|
verbose
|
TYPE:
|
Source code in cmd2/history.py
to_dict
Utility method to convert this HistoryItem into a dictionary for use in persistent JSON history files
from_dict
staticmethod
Utility method to restore a HistoryItem from a dictionary
:param source_dict: source data dictionary (generated using to_dict()) :return: HistoryItem object :raises KeyError: if source_dict is missing required elements
| PARAMETER | DESCRIPTION |
|---|---|
source_dict
|
TYPE:
|
Source code in cmd2/history.py
History
Bases: List[HistoryItem]
A list of HistoryItem objects with additional methods for searching and managing the list.
cmd2.Cmd instantiates this class into the cmd2.Cmd.history
attribute, and adds commands to it as a user enters them.
See History for information about the built-in command which allows users to view, search, run, and save previously entered commands.
Developers interested in accessing previously entered commands can use this class to gain access to the historical record.
| PARAMETER | DESCRIPTION |
|---|---|
seq
|
TYPE:
|
Source code in cmd2/history.py
spanpattern
class-attribute
instance-attribute
spanpattern = compile(
"^\\s*(?P<start>-?[1-9]\\d*)?(?P<separator>:|(\\.{2,}))(?P<end>-?[1-9]\\d*)?\\s*$"
)
start_session
append
append(new: HistoryItem) -> None
append(new: Statement) -> None
Append a new statement to the end of the History list.
:param new: Statement object which will be composed into a HistoryItem and added to the end of the list
| PARAMETER | DESCRIPTION |
|---|---|
new
|
TYPE:
|
Source code in cmd2/history.py
clear
get
Get item from the History list using 1-based indexing.
:param index: optional item to get :return: a single cmd2.history.HistoryItem
| PARAMETER | DESCRIPTION |
|---|---|
index
|
TYPE:
|
Source code in cmd2/history.py
span
Return a slice of the History list
:param span: string containing an index or a slice :param include_persisted: if True, then retrieve full results including from persisted history :return: a dictionary of history items keyed by their 1-based index in ascending order, or an empty dictionary if no results were found
This method can accommodate input in any of these forms:
a..b or a:b
a.. or a:
..a or :a
-a.. or -a:
..-a or :-a
Different from native python indexing and slicing of arrays, this method uses 1-based array numbering. Users who are not programmers can't grok zero based numbering. Programmers can sometimes grok zero based numbering. Which reminds me, there are only two hard problems in programming:
- naming
- cache invalidation
- off by one errors
| PARAMETER | DESCRIPTION |
|---|---|
span
|
TYPE:
|
include_persisted
|
TYPE:
|
Source code in cmd2/history.py
str_search
Find history items which contain a given string
:param search: the string to search for :param include_persisted: if True, then search full history including persisted history :return: a dictionary of history items keyed by their 1-based index in ascending order, or an empty dictionary if the string was not found
| PARAMETER | DESCRIPTION |
|---|---|
search
|
TYPE:
|
include_persisted
|
TYPE:
|
Source code in cmd2/history.py
regex_search
Find history items which match a given regular expression
:param regex: the regular expression to search for. :param include_persisted: if True, then search full history including persisted history :return: a dictionary of history items keyed by their 1-based index in ascending order, or an empty dictionary if the regex was not matched
| PARAMETER | DESCRIPTION |
|---|---|
regex
|
TYPE:
|
include_persisted
|
TYPE:
|
Source code in cmd2/history.py
truncate
Truncate the length of the history, dropping the oldest items if necessary
:param max_length: the maximum length of the history, if negative, all history items will be deleted :return: nothing
| PARAMETER | DESCRIPTION |
|---|---|
max_length
|
TYPE:
|
Source code in cmd2/history.py
to_json
Utility method to convert this History into a JSON string for use in persistent history files
Source code in cmd2/history.py
from_json
staticmethod
Utility method to restore History from a JSON string
:param history_json: history data as JSON string (generated using to_json()) :return: History object :raises json.JSONDecodeError: if passed invalid JSON string :raises KeyError: if JSON is missing required elements :raises ValueError: if history version in JSON isn't supported
| PARAMETER | DESCRIPTION |
|---|---|
history_json
|
TYPE:
|
Source code in cmd2/history.py
single_line_format
Format a command line to display on a single line.
Spaces and newlines in quotes are preserved so those strings will span multiple lines.
:param statement: Statement being formatted. :return: formatted command line
| PARAMETER | DESCRIPTION |
|---|---|
statement
|
TYPE:
|