Class used to represent one command in the history list.
| 58 | |
| 59 | @dataclass(frozen=True) |
| 60 | class HistoryItem: |
| 61 | """Class used to represent one command in the history list.""" |
| 62 | |
| 63 | _listformat = " {:>4} {}" |
| 64 | _ex_listformat = " {:>4}x {}" |
| 65 | |
| 66 | # Used in JSON dictionaries |
| 67 | _statement_field = "statement" |
| 68 | |
| 69 | statement: Statement |
| 70 | |
| 71 | def __str__(self) -> str: |
| 72 | """Human-readable representation of the history item.""" |
| 73 | return self.statement.raw |
| 74 | |
| 75 | @property |
| 76 | def raw(self) -> str: |
| 77 | """The raw input from the user for this item. |
| 78 | |
| 79 | Proxy property for ``self.statement.raw`` |
| 80 | """ |
| 81 | return self.statement.raw |
| 82 | |
| 83 | @property |
| 84 | def expanded(self) -> str: |
| 85 | """Return the command as run which includes shortcuts and aliases resolved plus any changes made in hooks. |
| 86 | |
| 87 | Proxy property for ``self.statement.expanded_command_line`` |
| 88 | """ |
| 89 | return self.statement.expanded_command_line |
| 90 | |
| 91 | def pr(self, idx: int, script: bool = False, expanded: bool = False, verbose: bool = False) -> str: |
| 92 | """Represent this item in a pretty fashion suitable for printing. |
| 93 | |
| 94 | If you pass verbose=True, script and expanded will be ignored |
| 95 | |
| 96 | :param idx: The 1-based index of this item in the history list |
| 97 | :param script: True if formatting for a script (No item numbers) |
| 98 | :param expanded: True if expanded command line should be printed |
| 99 | :param verbose: True if expanded and raw should both appear when they are different |
| 100 | :return: pretty print string version of a HistoryItem |
| 101 | """ |
| 102 | if verbose: |
| 103 | raw = self.raw.rstrip() |
| 104 | expanded_command = self.expanded |
| 105 | |
| 106 | ret_str = self._listformat.format(idx, raw) |
| 107 | if raw != expanded_command: |
| 108 | ret_str += "\n" + self._ex_listformat.format(idx, expanded_command) |
| 109 | else: |
| 110 | ret_str = self.expanded if expanded else single_line_format(self.statement).rstrip() |
| 111 | |
| 112 | # Display a numbered list if not writing to a script |
| 113 | if not script: |
| 114 | ret_str = self._listformat.format(idx, ret_str) |
| 115 | |
| 116 | return ret_str |
| 117 |
no outgoing calls
searching dependent graphs…