#!/usr/bin/env python3 """Run nvim inside gnome-terminal""" import sys import os import subprocess import dbus from time import time APP_ID = 'org.neovim' CLASS = 'neovim' NAME = 'Neovim' def find_terminal_server(): candidates = list(filter(os.path.exists, \ ('/usr/lib/gnome-terminal/gnome-terminal-server', # arch et al '/usr/libexec/gnome-terminal-server'))) # fedora 22 if len(candidates) > 0: return candidates[0] else: print("nvim-wrapper needs gnome-terminal-server, but it wasn't found.") sys.exit() SERVER_CMD = [ find_terminal_server(), '--app-id', APP_ID, '--class', CLASS, '--name', NAME, ] TERM_CMD = [ 'gnome-terminal', '--app-id', APP_ID, '--class', CLASS, '--name', NAME, '--hide-menubar', '-x', 'nvim', ] def main(): """Run nvim inside gnome-terminal""" session_bus = dbus.SessionBus() # launch the terminal server with a custom app-id # and window class (so the .desktop file gets associated) if not session_bus.name_has_owner(APP_ID): subprocess.Popen(SERVER_CMD) # wait until the name is registered, or 2 seconds pass (when launching from # cold cache it might take more time) timeout = time() + 2 while not session_bus.name_has_owner(APP_ID) and time() <= timeout: pass # launch nvim in a gnome-terminal instance if session_bus.name_has_owner(APP_ID): env = os.environ.copy() env['NVIM_TUI_ENABLE_CURSOR_SHAPE'] = '1' env['NVIM_TUI_ENABLE_TRUE_COLOR'] = '1' with open(os.devnull, 'wb') as fnull: subprocess.Popen(TERM_CMD + sys.argv[1:], stdout=fnull, stderr=fnull, env=env) if __name__ == '__main__': main()