A list of [HistoryItem][cmd2.history.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](../features/history.md) for in
| 132 | |
| 133 | |
| 134 | class History(list[HistoryItem]): |
| 135 | """A list of [HistoryItem][cmd2.history.HistoryItem] objects with additional methods for searching and managing the list. |
| 136 | |
| 137 | [cmd2.Cmd][] instantiates this class into the `cmd2.Cmd.history` |
| 138 | attribute, and adds commands to it as a user enters them. |
| 139 | |
| 140 | See [History](../features/history.md) for information about the built-in command |
| 141 | which allows users to view, search, run, and save previously entered commands. |
| 142 | |
| 143 | Developers interested in accessing previously entered commands can use this |
| 144 | class to gain access to the historical record. |
| 145 | """ |
| 146 | |
| 147 | # Used in JSON dictionaries |
| 148 | _history_version = "4.0.0" |
| 149 | _history_version_field = "history_version" |
| 150 | _history_items_field = "history_items" |
| 151 | |
| 152 | def __init__(self, seq: Iterable[HistoryItem] = ()) -> None: |
| 153 | """Initialize History instances.""" |
| 154 | super().__init__(seq) |
| 155 | self.session_start_index = 0 |
| 156 | |
| 157 | def start_session(self) -> None: |
| 158 | """Start a new session, thereby setting the next index as the first index in the new session.""" |
| 159 | self.session_start_index = len(self) |
| 160 | |
| 161 | def _zero_based_index(self, onebased: int | str) -> int: |
| 162 | """Convert a one-based index to a zero-based index.""" |
| 163 | result = int(onebased) |
| 164 | if result > 0: |
| 165 | result -= 1 |
| 166 | return result |
| 167 | |
| 168 | @overload |
| 169 | def append(self, new: HistoryItem) -> None: ... # pragma: no cover |
| 170 | |
| 171 | @overload |
| 172 | def append(self, new: Statement) -> None: ... # pragma: no cover |
| 173 | |
| 174 | def append(self, new: Statement | HistoryItem) -> None: |
| 175 | """Append a new statement to the end of the History list. |
| 176 | |
| 177 | :param new: Statement object which will be composed into a HistoryItem |
| 178 | and added to the end of the list |
| 179 | """ |
| 180 | history_item = HistoryItem(new) if isinstance(new, Statement) else new |
| 181 | super().append(history_item) |
| 182 | |
| 183 | def clear(self) -> None: |
| 184 | """Remove all items from the History list.""" |
| 185 | super().clear() |
| 186 | self.start_session() |
| 187 | |
| 188 | def get(self, index: int) -> HistoryItem: |
| 189 | """Get item from the History list using 1-based indexing. |
| 190 | |
| 191 | :param index: optional item to get |
no outgoing calls
searching dependent graphs…