Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new vsplit hsplit cmd dbus #390

Merged
merged 4 commits into from Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 14 additions & 13 deletions remotinator
Expand Up @@ -35,17 +35,18 @@ from terminatorlib.translation import _
APP_NAME='remotinator'

COMMANDS={
# Command uuid req. Description
'new_window': [False, _('Open a new window')],
'new_tab': [True, _('Open a new tab')],
'hsplit': [True, _('Split the current terminal horizontally')],
'vsplit': [True, _('Split the current terminal vertically')],
'get_terminals': [False, _('Get a list of all terminals')],
'get_window': [True, _('Get the UUID of a parent window')],
'get_window_title': [True, _('Get the title of a parent window')],
'get_tab': [True, _('Get the UUID of a parent tab')],
'get_tab_title': [True, _('Get the title of a parent tab')],
'switch_profile': [True, _('Switch current terminal profile')],
# Command uuid req. Description
'new_window': [False, _('Open a new window')],
'new_tab': [True, _('Open a new tab')],
'hsplit': [True, _('Split the current terminal horizontally')],
'vsplit': [True, _('Split the current terminal vertically')],
'get_terminals': [False, _('Get a list of all terminals')],
'get_focused_terminal': [False, _('Get the uuid of the current focused terminal')],
'get_window': [True, _('Get the UUID of a parent window')],
'get_window_title': [True, _('Get the title of a parent window')],
'get_tab': [True, _('Get the UUID of a parent tab')],
'get_tab_title': [True, _('Get the title of a parent tab')],
'switch_profile': [True, _('Switch current terminal profile')],
}

if __name__ == '__main__':
Expand Down Expand Up @@ -88,9 +89,9 @@ if __name__ == '__main__':
if uuid_required:
uuid = options.get('uuid', os.environ.get('TERMINATOR_UUID'))
if uuid:
func(uuid, options)
print(str(func(uuid, options)))
else:
err("$TERMINATOR_UUID is not set, or passed as an option.")
sys.exit(1)
else:
func(options)
print(str(func(options)))
85 changes: 84 additions & 1 deletion terminatorlib/ipc.py
Expand Up @@ -13,6 +13,10 @@
from .config import Config
from .factory import Factory
from .util import dbg, err, enumerate_descendants
from .terminal import Terminal
from .container import Container
from gi.repository import Gtk as gtk
from gi.repository import GObject as gobject

CONFIG = Config()
if not CONFIG['dbus']:
Expand Down Expand Up @@ -120,6 +124,63 @@ def vsplit(self, uuid=None):
"""Split a terminal vertically, by UUID"""
return self.new_terminal(uuid, 'vsplit')

def get_terminal_container(self, terminal, container=None):
terminator = Terminator()
if not container:
for window in terminator.windows:
owner = self.get_terminal_container(terminal, window)
if owner: return owner
else:
for child in container.get_children():
if isinstance(child, Terminal) and child == terminal:
return container
if isinstance(child, Container):
owner = self.get_terminal_container(terminal, child)
if owner: return owner

@dbus.service.method(BUS_NAME)
def vsplit_cmd(self, uuid=None, title=None, cmd=None):
"""Split a terminal vertically, by UUID and immediately runs the specified command in the new terminal"""
return self.new_terminal_cmd(uuid=uuid, title=title, cmd=cmd, split_vert=False)

@dbus.service.method(BUS_NAME)
def hsplit_cmd(self, uuid=None, title=None, cmd=None):
"""Split a terminal horizontally, by UUID and immediately runs the specified command in the new terminal"""
return self.new_terminal_cmd(uuid=uuid, title=title, cmd=cmd, split_vert=True)

def new_terminal_cmd(self, uuid=None, title=None, cmd=None, split_vert=False):
"""Split a terminal by UUID and immediately runs the specified command in the new terminal"""
if not uuid:
return "ERROR: No UUID specified"

terminal = self.terminator.find_terminal_by_uuid(uuid)

terminals_before = set(self.get_terminals())
if not terminal:
return "ERROR: Terminal with supplied UUID not found"

# get current working dir out of target terminal
cwd = terminal.get_cwd()

# get current container
container = self.get_terminal_container(terminal)
maker = Factory()
sibling = maker.make('Terminal')
sibling.set_cwd(cwd)
if title: sibling.titlebar.set_custom_string(title)
sibling.spawn_child(init_command=cmd)

# split and run command in new terminal
container.split_axis(terminal, split_vert, cwd, sibling)

terminals_after = set(self.get_terminals())
# Detect the new terminal UUID
new_terminal_set = list(terminals_after - terminals_before)
if len(new_terminal_set) != 1:
return "ERROR: Cannot determine the UUID of the added terminal"
else:
return new_terminal_set[0]

def new_terminal(self, uuid, type):
"""Split a terminal horizontally or vertically, by UUID"""
dbg('dbus method called: %s' % type)
Expand Down Expand Up @@ -150,6 +211,13 @@ def get_terminals(self):
"""Return a list of all the terminals"""
return [x.uuid.urn for x in self.terminator.terminals]

@dbus.service.method(BUS_NAME)
def get_focused_terminal(self):
"""Returns the uuid of the currently focused terminal"""
if self.terminator.last_focused_term:
return self.terminator.last_focused_term.uuid.urn
return None

@dbus.service.method(BUS_NAME)
def get_window(self, uuid=None):
"""Return the UUID of the parent window of a given terminal"""
Expand Down Expand Up @@ -217,7 +285,7 @@ def _exec(*args, **argd):
"Remotinator can't connect to terminator. " +
"May be terminator is not running.")

func(proxy, *args, **argd)
return func(proxy, *args, **argd)
return _exec

@with_proxy
Expand Down Expand Up @@ -254,11 +322,26 @@ def vsplit(session, uuid, options):
"""Call the dbus method to vertically split a terminal"""
print(session.vsplit(uuid))

@with_proxy
def vsplit_cmd(session, uuid, title, cmd, options):
"""Call the dbus method to vertically split a terminal and run the specified command in the new terminal"""
session.vsplit_cmd(uuid, title, cmd)

@with_proxy
def hsplit_cmd(session, uuid, title, cmd, options):
"""Call the dbus method to horizontally split a terminal and run the specified command in the new terminal"""
session.hsplit_cmd(uuid, title, cmd)

@with_proxy
def get_terminals(session, options):
"""Call the dbus method to return a list of all terminals"""
print('\n'.join(session.get_terminals()))

@with_proxy
def get_focused_terminal(session, options):
"""Call the dbus method to return the currently focused terminal"""
return session.get_focused_terminal()

@with_proxy
def get_window(session, uuid, options):
"""Call the dbus method to return the toplevel tab for a terminal"""
Expand Down
4 changes: 2 additions & 2 deletions terminatorlib/terminal.py
Expand Up @@ -1420,10 +1420,10 @@ def held_open(self, widget=None, respawn=False, debugserver=False):
self.is_held_open = True
self.titlebar.update()

def spawn_child(self, widget=None, respawn=False, debugserver=False):
def spawn_child(self, init_command=None, widget=None, respawn=False, debugserver=False):
args = []
shell = None
command = None
command = init_command

if self.terminator.doing_layout:
dbg('still laying out, refusing to spawn a child')
Expand Down