MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / HistoryItem

Class HistoryItem

cmd2/history.py:60–131  ·  view source on GitHub ↗

Class used to represent one command in the history list.

Source from the content-addressed store, hash-verified

58
59@dataclass(frozen=True)
60class 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

Callers 6

histFunction · 0.90
persisted_histFunction · 0.90
histitemFunction · 0.90
from_dictMethod · 0.85
appendMethod · 0.85

Calls

no outgoing calls

Tested by 4

histFunction · 0.72
persisted_histFunction · 0.72
histitemFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…