Skip to content

Instantly share code, notes, and snippets.

@TimonLukas
Created February 26, 2020 09:30
Show Gist options
  • Save TimonLukas/e4808fc752d24620f3a26c2ab8ffe60d to your computer and use it in GitHub Desktop.
Save TimonLukas/e4808fc752d24620f3a26c2ab8ffe60d to your computer and use it in GitHub Desktop.
Sets an iterm2 terminal window up in a useful way
#!/usr/bin/env python3.7
import iterm2
import asyncio
async def run(cmd: str) -> str:
process = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await process.communicate()
return stdout.decode()
async def main(connection):
app = await iterm2.async_get_app(connection)
tab = app.current_window.current_tab
current_session = app.current_window.current_tab.current_session
sessions = tab.sessions
pwd = await current_session.async_get_variable('path')
bpython_exists = 'not found' not in (await run('where bpython'))
if len(sessions) > 1:
await asyncio.wait([
session.async_close() for session in sessions
if session.session_id != current_session.session_id
])
right = await current_session.async_split_pane(vertical=True)
bottom = await right.async_split_pane(vertical=False)
await asyncio.wait([
asyncio.wait([
current_session.async_activate(),
current_session.async_send_text('clear\n'),
]),
asyncio.gather(*[
right.async_send_text(f'cd "{pwd.strip()}"\n'),
right.async_send_text('bpython\n' if bpython_exists else 'python\n'),
right.async_send_text(b'\x0c'),
]),
asyncio.gather(*[
bottom.async_send_text(f'cd "{pwd.strip()}"\n'),
bottom.async_send_text('clear\n'),
]),
])
iterm2.run_until_complete(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment