#!/usr/bin/env python3.11
"""
       Name: w3m-splits
     AUTHOR: https://github.com/jessebot
DESCRIPTION: quick script to run w3m with images from neomutt in iterm2
             you'll still need something like sixel and w3m locally
    LICENSE: GNU AFFERO GENERAL PUBLIC LICENSE Version 3
"""

import iterm2
import argparse

# parse args
parser = argparse.ArgumentParser()
parser.add_argument("html_file",
                    help="html file or URL to view in w3m in an iTerm2 split")
parser.add_argument("--profile", "-p", type=str, default="Minimal - bash",
                    help="iTerm2 profile you'd like to use in the new split.")
args = parser.parse_args()


async def main(connection):
    """
    Create split pane to the right, make the right pane active, and send a
    command to the newly created split
    """

    app = await iterm2.async_get_app(connection)

    # Create split pane to the right and make the right one active
    left_pane = app.current_terminal_window.current_tab.current_session
    right_pane = await left_pane.async_split_pane(vertical=True,
                                                  profile=args.profile)

    # send the command to the newly created split
    w3m = ("w3m -sixel -o auto_image=TRUE -o display_image=1 -T text/html"
           f" -o confirm_qq=false -F {args.html_file} && exit \n")
    await right_pane.async_send_text(w3m)


# run the iterm2 stuff
iterm2.run_until_complete(main)
