|
| 1 | +"""CLI for accessing the gtk/tickit UIs implemented by this package.""" |
| 2 | +import shlex |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +from .ui_bridge import UIBridge |
| 7 | +from .. import attach |
| 8 | + |
| 9 | + |
| 10 | +@click.command(context_settings=dict(allow_extra_args=True)) |
| 11 | +@click.option('--prog') |
| 12 | +@click.option('--gui', '-g', default=False, is_flag=True) |
| 13 | +@click.option('--listen', '-l') |
| 14 | +@click.option('--connect', '-c') |
| 15 | +@click.option('--profile', |
| 16 | + default='disable', |
| 17 | + type=click.Choice(['ncalls', 'tottime', 'percall', 'cumtime', |
| 18 | + 'name', 'disable'])) |
| 19 | +@click.pass_context |
| 20 | +def main(ctx, prog, gui, listen, connect, profile): |
| 21 | + """Entry point.""" |
| 22 | + address = connect or listen |
| 23 | + |
| 24 | + if address: |
| 25 | + import re |
| 26 | + p = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:\:\d{1,5})?$') |
| 27 | + |
| 28 | + if p.match(address): |
| 29 | + args = ('tcp',) |
| 30 | + kwargs = {'address': address} |
| 31 | + else: |
| 32 | + args = ('socket',) |
| 33 | + kwargs = {'path': address} |
| 34 | + |
| 35 | + if connect: |
| 36 | + # connect to existing instance listening on address |
| 37 | + nvim = attach(*args, **kwargs) |
| 38 | + elif listen: |
| 39 | + # spawn detached instance listening on address and connect to it |
| 40 | + import os |
| 41 | + import time |
| 42 | + from subprocess import Popen |
| 43 | + os.environ['NVIM_LISTEN_ADDRESS'] = address |
| 44 | + nvim_argv = shlex.split(prog or 'nvim -T abstract_ui') + ctx.args |
| 45 | + # spawn the nvim with stdio redirected to /dev/null. |
| 46 | + dnull = open(os.devnull) |
| 47 | + p = Popen(nvim_argv, stdin=dnull, stdout=dnull, stderr=dnull) |
| 48 | + dnull.close() |
| 49 | + while p.poll() or p.returncode is None: |
| 50 | + try: |
| 51 | + nvim = attach(*args, **kwargs) |
| 52 | + break |
| 53 | + except IOError: |
| 54 | + # socket not ready yet |
| 55 | + time.sleep(0.050) |
| 56 | + else: |
| 57 | + # spawn embedded instance |
| 58 | + nvim_argv = shlex.split(prog or 'nvim --embed') + ctx.args |
| 59 | + nvim = attach('child', argv=nvim_argv) |
| 60 | + |
| 61 | + if gui: |
| 62 | + from .gtk_ui import GtkUI |
| 63 | + ui = GtkUI() |
| 64 | + else: |
| 65 | + from .tickit_ui import TickitUI |
| 66 | + ui = TickitUI() |
| 67 | + bridge = UIBridge() |
| 68 | + bridge.connect(nvim, ui, profile if profile != 'disable' else None) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + main() |
0 commit comments