Life Cycle

Provides hooks for session life-cycle events.

class SessionTerminationMonitor(connection: iterm2.connection.Connection)

Watches for session termination.

A session is said to terminate when its command (typically login) has exited. If the user closes a window, tab, or split pane they can still undo closing it for some amount of time. Session termination will be delayed until it is no longer undoable.

Parameters

connection – The Connection to use.

Example:

async with iterm2.SessionTerminationMonitor(connection) as mon:
    while True:
        session_id = await mon.async_get()
        print("Session {} closed".format(session_id))
async async_get()str

Returns the session_id of a just-terminated session.

class LayoutChangeMonitor(connection: iterm2.connection.Connection)

Watches for changes to the composition of sessions, tabs, and windows.

Parameters

connection – The Connection to use.

async async_get()

Blocks until the layout changes.

Will block until any of the following occurs:

  • A session moves from one tab to another (including moving into its own window).

  • The relative position of sessions within a tab changes.

  • A tab moves from one window to another.

  • The order of tabs within a window changes.

  • A session is buried or disintered.

  • A session or window is resized.

Use App to examine the updated application state.

Example:

async with iterm2.LayoutChangeMonitor(connection) as mon:
    while True:
        await mon.async_get()
        print("layout changed")
class NewSessionMonitor(connection: iterm2.connection.Connection)

Watches for the creation of new sessions.

Parameters

connection – The Connection to use.

See also

Example:

  async with iterm2.NewSessionMonitor(connection) as mon:
      while True:
          session_id = await mon.async_get()
          print("Session ID {} created".format(session_id))

.. seealso::
    * Example ":ref:`autoalert`"
async async_get()str

Returns the new session ID.

class EachSessionOnceMonitor(app: iterm2.app.App)

This is a convenient way to do something to all sessions exactly once, including those created in the future.

You can use it as a context manager to get the session_id of each session, or you can use the static method async_foreach_session_create_task to have a task created for each session.

Parameters
  • connection – The Connection to use.

  • app – An instance of App.

async static async_foreach_session_create_task(app, task)

Create a task for each session. Cancels the task when the session terminates.

Includes sessions in existence now and those created in the future.

Parameters
  • app – An instance of App.

  • task – A coro taking a single argument of session ID.

Returns

A future.

Example:

app = await iterm2.async_get_app(connection)
# Print a message to stdout when there's a new prompt in any
# session
async def my_task(session_id):
    async with iterm2.PromptMonitor(connection, session_id) as mon:
        await mon.async_get()
        print("Prompt detected")

await (iterm2.EachSessionOnceMonitor.
    async_foreach_session_create_task(app, my_task))
async async_get()str

Returns the session ID.


Indices and tables