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