Python API: not clear how to stream the content of a session
I'm running:
Build 3.3.20190909-nightly
- iterm2 module from Monday's git
I'm trying to monitor the content of a session, like a co-process but using the new API (I'm assuming that Python API is the modern way to do this). I'm trying the following:
#!/usr/bin/env python
import iterm2
import asyncio
import datetime
from time import sleep, time
import sys
def timestamp():
return datetime.datetime.today().strftime('%Y-%m-%d at %H:%M.%S')
async def main(connection):
app = await iterm2.async_get_app(connection)
id = sys.argv[1]
print(f"mirror content from session {id}")
session = app.get_session_by_id(id)
if session is None:
raise NameError("session not found: {id}")
lines = 0
async with session.get_screen_streamer(want_contents=True) as streamer:
while True:
content = await streamer.async_get()
print(f"--< lines={content.number_of_lines} above={content.number_of_lines_above_screen} >--")
for i in range(0, content.number_of_lines):
lines += 1
line = content.line(i)
print(f"{lines:6d} {i:6d} [hard_eol={line.hard_eol}] {line.string}")
print(f"-- batch done --")
print("main done")
if len(sys.argv) < 2:
print("missing session id")
sys.exit(-1)
iterm2.run_forever(main)
print("done")
My goal is to echo back the content of another session given its session id. What I get is the number of lines that are rendered, so if my window is 40 lines high I always get the 40 lines currently displayed on screen. number_of_lines_above_screen is always 0.
A few questions:
- is there a bug that only the visible lines are returned and there is no way to see what is scrollable to (which I had assumed 'scrollback' or above_screen would mean but I think I'm mistaken) ?
- I can't think of an easy way to only show "what has changed" in terms of line to just 'tail' another session. Because I don't know which lines are the same as last call and which ones are new. How can I do it ? I wasn't able to find any example for Session.get_screen_streamer. It's made harder that the streamer gets pretty much triggered for each character entered
- bonus: I looked quickly but I don't think streamer.async_get().lines(index) returns special characters, only printable one
- I don't understand the purpose for LineContents.string_at(index), I'm not sure what a 'cell' is or what range of values is acceptable for index x (width of screen ?)