Prompt

Provides information about the shell prompt.

class PromptState(value)

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: Union[None, str]

Returns the command entered at the prompt.

property command_range: iterm2.util.CoordRange

Gives the CoordRange of the command following the shell prompt.

property output_range: iterm2.util.CoordRange

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

property prompt_range: iterm2.util.CoordRange

Gives the CoordRange of a shell prompt.

property working_directory: Union[None, str]

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,Optional[Prompt]), (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. Older versions of iTerm2 will not provide a Prompt object when the first value is PROMPT.

class Mode(value)

The mode for a prompt monitor.

COMMAND_END = 3

Notify when a command finishes execution

COMMAND_START = 2

Notify when a command begins execution

PROMPT = 1

Notify when prompt detected

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