Prompt

Provides information about the shell prompt.

class PromptState

Describes the states that a command prompt can take.

EDITING = 0

User is editing the command at the prompt

FINISHED = 3

The last entered command has finished but there hasn’t been a new prompt yet (rare).

RUNNING = 1

The last entered command is still executing, and has not finished yet.

UNKNOWN = -1

This version of iTerm2 does not report prompt state (you should upgrade)

class Prompt(proto)

Describes a command prompt.

Shell Integration must be installed for this to work properly. Do not construct this object yourself. Use async_get_last_prompt() to get an instance.

property command

Returns the command entered at the prompt.

property command_range

Gives the CoordRange of the command following the shell prompt.

property output_range

Gives the CoordRange of the output of a command following a shell prompt.

property prompt_range

Gives the CoordRange of a shell prompt.

property working_directory

Returns the working directory at the time the prompt was printed.

class PromptMonitor(connection: iterm2.connection.Connection, session_id: str, modes: Optional[List[iterm2.prompt.PromptMonitor.Mode]] = None)

An asyncio context manager to watch for changes to the prompt.

This requires shell integration or prompt-detecting triggers to be installed for prompt detection.

Note: Older versions of the runtime do not support modes other than PROMPT. Attempting to use a mode other than PROMPT when connected to a too-old version of iTerm2 will result in an exception telling the user to upgrade.

Parameters
  • connection – The Connection to use.

  • session_id – The string session ID to monitor.

Example:

async with iterm2.PromptMonitor(
    connection, my_session.session_id) as mon:
        while True:
            await mon.async_get()
            DoSomething()
async async_get(include_id: bool = False) → Union[Tuple[iterm2.prompt.PromptMonitor.Mode, Any], Tuple[iterm2.prompt.PromptMonitor.Mode, Any, Optional[str]]]

Blocks until a new shell prompt is received.

Note: Older versions of the runtime that do not support modes other than PROMPT always return None.

Parameters

include_id (bool) – If True, return a triple where the last value is the prompt’s unique ID if available or None otherwise.

Returns

A tuple of (PROMPT,None), (COMMAND_START,Str), or (COMMAND_END,Int) where the Str gives the command being run and the Int gives the exit status of the command.

class Mode

The mode for a prompt monitor.

COMMAND_END = 3
COMMAND_START = 2
PROMPT = 1
async async_get_last_prompt(connection: iterm2.connection.Connection, session_id: str) → Union[None, iterm2.prompt.Prompt]

Fetches info about the last prompt in a session.

Parameters
  • connection (Connection) – The connection to iTerm2.

  • session_id (str) – The session ID for which to fetch the most recent prompt.

Returns

The prompt if one exists, or else None.

Throws

RPCException if something goes wrong.


Indices and tables