Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

add detach and config file support #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 98 additions & 3 deletions neovim_gui/cli.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,117 @@
"""CLI for accessing the gtk/tickit UIs implemented by this package."""
import os
import resource
import sys
import shlex

import click
import yaml

from .ui_bridge import UIBridge
from neovim import attach
from neovim.compat import IS_PYTHON3


CONFIG_FILES = (
'.pynvim.yaml',
'~/.pynvim.yaml',
'~/.config/pynvim/config.yaml'
)


def load_config(config_file):
"""Load config values from yaml."""

if config_file:
with open(config_file) as f:
return yaml.load(f)

else:
for config_file in CONFIG_FILES:
try:
with open(os.path.expanduser(config_file)) as f:
return yaml.load(f)

except IOError:
pass

return {}


# http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
def detach_proc(workdir='.', umask=0):
"""Detach a process from the controlling terminal and run it in the
background as a daemon.
"""

# Default maximum for the number of available file descriptors.
MAXFD = 1024

# The standard I/O file descriptors are redirected to /dev/null by default.
if (hasattr(os, "devnull")):
REDIRECT_TO = os.devnull
else:
REDIRECT_TO = "/dev/null"

try:
pid = os.fork()
except OSError, e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not valid python3, please use OSError as e

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this except have any purpose at all? Can't we just propagate the OSError (no point in losing information by reraising as a less specific Exception)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After digging around for best practices how to fork (seems a science) I found this script at http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/

I could live without reraising another general Exception.

raise Exception, "%s [%d]" % (e.strerror, e.errno)

if (pid == 0):
os.setsid()

try:
pid = os.fork()

except OSError, e:
raise Exception, "%s [%d]" % (e.strerror, e.errno)

if (pid == 0):
os.chdir(workdir)
os.umask(umask)
else:
os._exit(0)
else:
os._exit(0)

maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if (maxfd == resource.RLIM_INFINITY):
maxfd = MAXFD

# Iterate through and close all file descriptors.
for fd in range(0, maxfd):
try:
os.close(fd)
except OSError:
pass

os.open(REDIRECT_TO, os.O_RDWR)

os.dup2(0, 1)
os.dup2(0, 2)

return(0)


@click.command(context_settings=dict(allow_extra_args=True))
@click.option('--prog')
@click.option('--notify', '-n', default=False, is_flag=True)
@click.option('--listen', '-l')
@click.option('--connect', '-c')
@click.option('--font', '-f', default=('Monospace', 13), nargs=2)
@click.option('--profile',
default='disable',
type=click.Choice(['ncalls', 'tottime', 'percall', 'cumtime',
'name', 'disable']))
@click.option('config_file', '--config', type=click.Path(exists=True))
@click.option('--detach/--no-detach', default=True, is_flag=True)
@click.pass_context
def main(ctx, prog, notify, listen, connect, font, profile):
def main(ctx, prog, notify, listen, connect, profile, config_file, detach):
"""Entry point."""

if detach:
exit_code = detach_proc()

address = connect or listen

if address:
Expand Down Expand Up @@ -61,10 +152,14 @@ def main(ctx, prog, notify, listen, connect, font, profile):
nvim = attach('child', argv=nvim_argv)

from .gtk_ui import GtkUI
ui = GtkUI(font)
config = load_config(config_file)
ui = GtkUI(config)
bridge = UIBridge()
bridge.connect(nvim, ui, profile if profile != 'disable' else None, notify)

if detach:
sys.exit(exit_code)


if __name__ == '__main__':
main()
13 changes: 8 additions & 5 deletions neovim_gui/gtk_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ class GtkUI(object):

"""Gtk+ UI class."""

def __init__(self, font):
def __init__(self, config):
"""Initialize the UI instance."""
self._redraw_arg = None
self._foreground = -1
self._background = -1
self._font_name = font[0]
self._font_size = font[1]
self._foreground = config.get('foreground', -1)
self._background = config.get('background', -1)
self._window_background = config.get('window_background', '#aaaaaa')
self._font_size = config.get('font_size', 13)
self._font_name = config.get('font_name', 'Monospace')
self._screen = None
self._attrs = None
self._busy = False
Expand Down Expand Up @@ -115,6 +116,8 @@ def start(self, bridge):
window.connect('scroll-event', self._gtk_scroll)
window.connect('focus-in-event', self._gtk_focus_in)
window.connect('focus-out-event', self._gtk_focus_out)
window.modify_bg(Gtk.StateType.NORMAL,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what difference does this make? The entire window is painted with the neovim background color so this color would never be seen

Copy link
Contributor Author

@diefans diefans Apr 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use tiling awesome wm and I always get a grey border on the right side of the window without this setting:
pynvim_border

Gdk.color_parse(self._window_background))
window.show_all()
im_context = Gtk.IMMulticontext()
im_context.set_client_window(drawing_area.get_window())
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
install_requires = [
'neovim>=0.1.3',
'click>=3.0',
'pygobject'
'pygobject',
'pyyaml'
]
ext_modules = None

Expand Down