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

Method get_line

cmd2/pt_utils.py:244–305  ·  view source on GitHub ↗

Return the tokens for the given line number.

(lineno: int)

Source from the content-addressed store, hash-verified

242 tokens.append(("", match_text))
243
244 def get_line(lineno: int) -> list[tuple[str, str]]:
245 """Return the tokens for the given line number."""
246 line = document.lines[lineno]
247 tokens: list[tuple[str, str]] = []
248
249 # No syntax highlighting if styles are disallowed
250 if ru.ALLOW_STYLE == ru.AllowStyle.NEVER:
251 tokens.append(("", line))
252 return tokens
253
254 # Only attempt to match a command on the first line
255 if lineno == 0:
256 # Use cmd2's command pattern to find the first word (the command)
257 match = self.cmd_app.statement_parser._command_pattern.search(line)
258 if match:
259 # Group 1 is the command, Group 2 is the character(s) that terminated the command match
260 command = match.group(1)
261 cmd_start = match.start(1)
262 cmd_end = match.end(1)
263
264 # Add any leading whitespace
265 if cmd_start > 0:
266 tokens.append(("", line[:cmd_start]))
267
268 if command:
269 # Determine the style for the command
270 shortcut_found = False
271 for shortcut, _ in self.cmd_app.statement_parser.shortcuts:
272 if command.startswith(shortcut):
273 # Add the shortcut with the command style
274 tokens.append((self.command_color, shortcut))
275
276 # If there's more in the command word, it's an argument
277 if len(command) > len(shortcut):
278 tokens.append((self.argument_color, command[len(shortcut) :]))
279
280 shortcut_found = True
281 break
282
283 if not shortcut_found:
284 style = ""
285 if command in self.cmd_app.get_all_commands():
286 style = self.command_color
287 elif command in self.cmd_app.aliases:
288 style = self.alias_color
289 elif command in self.cmd_app.macros:
290 style = self.macro_color
291
292 # Add the command with the determined style
293 tokens.append((style, command))
294
295 # Add the rest of the line as arguments
296 if cmd_end < len(line):
297 highlight_args(line[cmd_end:], tokens)
298 else:
299 # No command match found on the first line
300 tokens.append(("", line))
301 else:

Callers

nothing calls this directly

Calls 2

appendMethod · 0.80
get_all_commandsMethod · 0.45

Tested by

no test coverage detected