From 65bccaba1999c4b130f3aba8f6033642454bc607 Mon Sep 17 00:00:00 2001 From: Emanuele Ruffaldi Date: Fri, 4 Nov 2016 08:50:55 +0100 Subject: [PATCH 1/6] added support for profiles - except when opening new window --- itermocil.py | 81 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/itermocil.py b/itermocil.py index 971a186..1284ae8 100644 --- a/itermocil.py +++ b/itermocil.py @@ -60,12 +60,23 @@ def __init__(self, teamocil_file, here=False, cwd=None): if 'pre' in self.parsed_config: self.applescript.append('do shell script "' + self.parsed_config['pre'] + ';"') + # TODO: profile stuff: profile cannot be set AFTER loading so it does not work for reusing windows + # and for the first pane of first window + firstprofile = "default" + try: + fw = self.parsed_config["windows"][0] + firstprofile = fw.get("profile",firstprofile) + if "panes" in fw and len(fw["panes"]) > 0 and type(fw["panes"][0]) == dict: + firstprofile = fw["panes"][0].get("profile",firstprofile) + except: + pass # If we need to open a new window, then add necessary commands # to script. if not self.here: if self.new_iterm: + ps = "default profile" if firstprofile == "default" else "profile \"%s\"" % firstprofile self.applescript.append('tell current window') - self.applescript.append('create tab with default profile') + self.applescript.append('create tab with %s' % ps) self.applescript.append('end tell') # self.applescript.append('create window with default profile') else: @@ -145,7 +156,7 @@ def script(self): return parsed_script - def arrange_panes(self, num_panes, layout="tiled"): + def arrange_panes(self, num_panes, profiles=[], layout="tiled"): """ Create a set of Applescript instructions to generate the desired layout of panes. Attempt to match teamocil layout behaviour as closely as is possible. @@ -154,12 +165,12 @@ def arrange_panes(self, num_panes, layout="tiled"): generating a version for old iTerm. """ - def create_pane(parent, child, split="vertical"): - + def create_pane(parent, child, split="vertical",profile="same"): + ps = "default profile" if profile == "default" else "profile \"%s\"" % profile return (''' tell pane_{pp} - set pane_{cp} to (split {o}ly with same profile) + set pane_{cp} to (split {o}ly with {ps}) end tell - '''.format(pp=parent, cp=child, o=split)) + '''.format(pp=parent, cp=child, o=split,ps=ps)) # Link a variable to the current window. self.applescript.append("set pane_1 to (current session of current window)") @@ -177,55 +188,55 @@ def create_pane(parent, child, split="vertical"): if layout == 'even-horizontal': for p in range(2, num_panes+1): - self.applescript.append(create_pane(p-1, p, "vertical")) + self.applescript.append(create_pane(p-1, p, "vertical",profile=profiles[p-1])) # 'even-vertical' layouts just split horizontally down the screen elif layout == 'even-vertical': for p in range(2, num_panes+1): - self.applescript.append(create_pane(p-1, p, "horizontal")) + self.applescript.append(create_pane(p-1, p, "horizontal",profile=profiles[p-1])) # 'main-vertical' layouts have one left pane that is full height, # and then split the remaining panes horizontally down the right elif layout == 'main-vertical': - self.applescript.append(create_pane(1, 2, "vertical")) + self.applescript.append(create_pane(1, 2, "vertical",profile=profiles[1])) for p in range(3, num_panes+1): - self.applescript.append(create_pane(p-1, p, "horizontal")) + self.applescript.append(create_pane(p-1, p, "horizontal",profile=profiles[p-1])) # 'main-vertical-flipped' layouts have one right pane that is full height, # and then split the remaining panes horizontally down the left elif layout == 'main-vertical-flipped': - self.applescript.append(create_pane(1, num_panes, "vertical")) + self.applescript.append(create_pane(1, num_panes, "vertical",profile=profiles[1])) for p in range(2, num_panes): - self.applescript.append(create_pane(p-1, p, "horizontal")) + self.applescript.append(create_pane(p-1, p, "horizontal",profile=profiles[p-1])) # 'main-horizontal' layouts have one left pane that is full height, # and then split the remaining panes horizontally down the right elif layout == 'main-horizontal': - self.applescript.append(create_pane(1, 2, "horizontal")) + self.applescript.append(create_pane(1, 2, "horizontal",profile=profiles[1])) for p in range(3, num_panes+1): - self.applescript.append(create_pane(p-1, p, "vertical")) + self.applescript.append(create_pane(p-1, p, "vertical",profile=profiles[p-1])) # 'double-main-horizontal' layouts have two left panes that are full height, # and then split the remaining panes horizontally down the right elif layout == 'double-main-horizontal': - self.applescript.append(create_pane(1, num_panes-1, "horizontal")) - self.applescript.append(create_pane(num_panes-1, num_panes, "vertical")) + self.applescript.append(create_pane(1, num_panes-1, "horizontal",profile=profiles[1])) + self.applescript.append(create_pane(num_panes-1, num_panes, "vertical",profile=profiles[-1])) for p in range(2, num_panes-1): - self.applescript.append(create_pane(p-1, p, "vertical")) + self.applescript.append(create_pane(p-1, p, "vertical",profile=profiles[p-1])) # 'double-main-vertical' layouts have two bottom panes that spllit the width # and then split the remaining panes vertically across the top elif layout == 'double-main-vertical': - self.applescript.append(create_pane(1, 2, "vertical")) - self.applescript.append(create_pane(2, 3, "vertical")) + self.applescript.append(create_pane(1, 2, "vertical",profile=profiles[1])) + self.applescript.append(create_pane(2, 3, "vertical",profile=profiles[2])) for p in range(4, num_panes+1): - self.applescript.append(create_pane(p-1, p, "horizontal")) + self.applescript.append(create_pane(p-1, p, "horizontal",profile=profiles[p-1])) # 'tiled' layouts create 2 columns and then however many rows as # needed. If there are odd number of panes then the bottom pane @@ -234,16 +245,19 @@ def create_pane(parent, child, split="vertical"): vertical_splits = int(ceil((num_panes / 2.0))) - 1 second_columns = num_panes / 2 + k = 1 for p in range(0, vertical_splits): pp = (p * 2) + 1 cp = pp + 2 - self.applescript.append(create_pane(pp, cp, "horizontal")) + self.applescript.append(create_pane(pp, cp, "horizontal",profile=profiles[k])) + k += 1 for p in range(0, second_columns): pp = (p * 2) + 1 cp = pp + 1 - self.applescript.append(create_pane(pp, cp, "vertical")) + self.applescript.append(create_pane(pp, cp, "vertical",profile=profiles[k])) + k += 1 # '3_columns' layouts create 3 columns and then however many rows as # needed. If there are odd number of panes then the bottom pane @@ -258,7 +272,7 @@ def create_pane(parent, child, split="vertical"): pp = (p * 3) + 1 cp = pp + 3 i += 1 - self.applescript.append(create_pane(pp, cp, "horizontal")) + self.applescript.append(create_pane(pp, cp, "horizontal",profile=profiles[i-1])) for p in range(0, vertical_splits+1): pp = (p * 3) + 1 @@ -268,7 +282,7 @@ def create_pane(parent, child, split="vertical"): qp = pp + q cp = pp + 1 + q i += 1 - self.applescript.append(create_pane(qp, cp, "vertical")) + self.applescript.append(create_pane(qp, cp, "vertical",profiles[i-1])) # Raise an exception if we don't recognise the layout setting. else: @@ -508,10 +522,25 @@ def process_file(self): sys.exit(1) for num, window in enumerate(self.parsed_config['windows']): + # Extract layout format, if given. + if "profile" in window: + profile = window['profile'] + else: + profile = "default" + + if 'panes' in window and len(window["panes"]) > 0 and type(window["panes"][0]) == dict: + firstprofile = window["panes"][0].get("profile",profile) + else: + firstprofile = profile + + paneprofiles = [p.get("profile",profile) if type(p) == dict else profile for p in window.get("panes",[])] + print "profiles first,default,panes:",firstprofile,profile,paneprofiles + if num > 0: + ps = "default profile" if firstprofile == "default" else "profile \"%s\"" % firstprofile if self.new_iterm: self.applescript.append('tell current window') - self.applescript.append('create tab with default profile') + self.applescript.append('create tab with %s' % ps) self.applescript.append('end tell') # self.applescript.append('create window with default profile') else: @@ -546,7 +575,7 @@ def process_file(self): if 'panes' in window: if self.new_iterm: - self.arrange_panes(len(window['panes']), layout) + self.arrange_panes(len(window['panes']),paneprofiles, layout) else: self.arrange_panes_old_iterm(len(window['panes']), layout) From 5c0b9b30924763a302bd4e55ae2b684f7622dd2e Mon Sep 17 00:00:00 2001 From: Emanuele Ruffaldi Date: Fri, 4 Nov 2016 08:53:48 +0100 Subject: [PATCH 2/6] documentation --- itermocil.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/itermocil.py b/itermocil.py index 1284ae8..5b9af16 100644 --- a/itermocil.py +++ b/itermocil.py @@ -165,8 +165,8 @@ def arrange_panes(self, num_panes, profiles=[], layout="tiled"): generating a version for old iTerm. """ - def create_pane(parent, child, split="vertical",profile="same"): - ps = "default profile" if profile == "default" else "profile \"%s\"" % profile + def create_pane(parent, child, split="vertical",profile="default"): + ps = "same profile" if profile == "default" else "profile \"%s\"" % profile return (''' tell pane_{pp} set pane_{cp} to (split {o}ly with {ps}) end tell @@ -245,19 +245,19 @@ def create_pane(parent, child, split="vertical",profile="same"): vertical_splits = int(ceil((num_panes / 2.0))) - 1 second_columns = num_panes / 2 - k = 1 + i = 1 for p in range(0, vertical_splits): pp = (p * 2) + 1 cp = pp + 2 self.applescript.append(create_pane(pp, cp, "horizontal",profile=profiles[k])) - k += 1 + i += 1 for p in range(0, second_columns): pp = (p * 2) + 1 cp = pp + 1 self.applescript.append(create_pane(pp, cp, "vertical",profile=profiles[k])) - k += 1 + i += 1 # '3_columns' layouts create 3 columns and then however many rows as # needed. If there are odd number of panes then the bottom pane @@ -271,8 +271,8 @@ def create_pane(parent, child, split="vertical",profile="same"): for p in range(0, vertical_splits): pp = (p * 3) + 1 cp = pp + 3 + self.applescript.append(create_pane(pp, cp, "horizontal",profile=profiles[i])) i += 1 - self.applescript.append(create_pane(pp, cp, "horizontal",profile=profiles[i-1])) for p in range(0, vertical_splits+1): pp = (p * 3) + 1 @@ -281,8 +281,8 @@ def create_pane(parent, child, split="vertical",profile="same"): break qp = pp + q cp = pp + 1 + q + self.applescript.append(create_pane(qp, cp, "vertical",profiles[i])) i += 1 - self.applescript.append(create_pane(qp, cp, "vertical",profiles[i-1])) # Raise an exception if we don't recognise the layout setting. else: @@ -522,21 +522,25 @@ def process_file(self): sys.exit(1) for num, window in enumerate(self.parsed_config['windows']): - # Extract layout format, if given. + + # Extract profile if "profile" in window: profile = window['profile'] else: profile = "default" + # Extract profile of first pane => profile of first tab command if 'panes' in window and len(window["panes"]) > 0 and type(window["panes"][0]) == dict: firstprofile = window["panes"][0].get("profile",profile) else: firstprofile = profile + # Define all profiles of all panes by inheritance or specification paneprofiles = [p.get("profile",profile) if type(p) == dict else profile for p in window.get("panes",[])] - print "profiles first,default,panes:",firstprofile,profile,paneprofiles + #print "profiles first,default,panes:",firstprofile,profile,paneprofiles if num > 0: + # first TAB has been created before this function, so apply window profile ps = "default profile" if firstprofile == "default" else "profile \"%s\"" % firstprofile if self.new_iterm: self.applescript.append('tell current window') From 7132cdd8f3ab9c89f784a5f23b2283c3b6897107 Mon Sep 17 00:00:00 2001 From: Emanuele Ruffaldi Date: Fri, 4 Nov 2016 08:55:31 +0100 Subject: [PATCH 3/6] minor on panes profile, using default instead of same --- itermocil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/itermocil.py b/itermocil.py index 5b9af16..0acf981 100644 --- a/itermocil.py +++ b/itermocil.py @@ -166,7 +166,7 @@ def arrange_panes(self, num_panes, profiles=[], layout="tiled"): """ def create_pane(parent, child, split="vertical",profile="default"): - ps = "same profile" if profile == "default" else "profile \"%s\"" % profile + ps = "default profile" if profile == "default" else "profile \"%s\"" % profile return (''' tell pane_{pp} set pane_{cp} to (split {o}ly with {ps}) end tell From 69475b51d97e01f7a495e3e41bf022e99f63f4e1 Mon Sep 17 00:00:00 2001 From: Emanuele Ruffaldi Date: Fri, 4 Nov 2016 08:55:54 +0100 Subject: [PATCH 4/6] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8da362f..eb51472 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ iTermocil is compatible with all of teamocil's flags, and they all work in the s |------------|---------------------------- | `name` | All iTerm panes in this window will be given this name. | `root` | The path where all panes in the window will be started +| `profile` | The profile for all the panes of the window (default is the standard one) (only if new window) | `layout` | The layout format that iTermocil will use (see below) | `panes` | An `Array` of panes | `command` | A command to run in the current window. Ignored if `panes` is present @@ -87,6 +88,7 @@ A pane can either be a `String` or a `Hash`. If it’s a `String`, Teamocil will |------------|---------------------------- | `commands` | An `Array` of commands that will be ran when the pane is created | `focus` | If set to `true`, the pane will be selected after the layout has been executed +| `profile` | The profile of the specific panel otherwise the one of parent window ## Examples From de39575712cc02557b77033a65f17d16f5491477 Mon Sep 17 00:00:00 2001 From: Emanuele Ruffaldi Date: Fri, 4 Nov 2016 09:26:01 +0100 Subject: [PATCH 5/6] added support for classic behavior --- itermocil.py | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/itermocil.py b/itermocil.py index 0acf981..ac7b5e2 100644 --- a/itermocil.py +++ b/itermocil.py @@ -10,7 +10,7 @@ from math import ceil -__version__ = '0.2.1' +__version__ = '0.2.2' class Itermocil(object): @@ -166,7 +166,13 @@ def arrange_panes(self, num_panes, profiles=[], layout="tiled"): """ def create_pane(parent, child, split="vertical",profile="default"): - ps = "default profile" if profile == "default" else "profile \"%s\"" % profile + # handle special case + if profile == "": + ps = "same profile" + elif profile == "default": + ps = "default profile" + else: + ps = "profile \"%s\"" % profile return (''' tell pane_{pp} set pane_{cp} to (split {o}ly with {ps}) end tell @@ -525,21 +531,36 @@ def process_file(self): # Extract profile if "profile" in window: - profile = window['profile'] + window_profile = window['profile'] + window_profile_spec = True else: - profile = "default" + window_profile = "default" + window_profile_spec = False - # Extract profile of first pane => profile of first tab command - if 'panes' in window and len(window["panes"]) > 0 and type(window["panes"][0]) == dict: - firstprofile = window["panes"][0].get("profile",profile) + # Default Behavior: no pane specifies profile and no profile in window => same for panes + # New Behavior: window specifies, or pane specifies => default or specified for panes + if window_profile_spec: + panes_have_profile = True else: - firstprofile = profile + panes_have_profile = False + for p in window.get("panes",[]): + if type(p) == dict and "profile" in p: + panes_have_profile = True + break - # Define all profiles of all panes by inheritance or specification - paneprofiles = [p.get("profile",profile) if type(p) == dict else profile for p in window.get("panes",[])] - #print "profiles first,default,panes:",firstprofile,profile,paneprofiles + if panes_have_profile: + paneprofiles = [p.get("profile",window_profile) if type(p) == dict else window_profile for p in window.get("panes",[])] + else: + # empty means same + paneprofiles = ["" for p in window.get("panes",[])] if num > 0: + # Extract profile of first pane => profile of first tab command + if 'panes' in window and len(window["panes"]) > 0 and type(window["panes"][0]) == dict: + firstprofile = window["panes"][0].get("profile",window_profile) + else: + firstprofile = window_profile + # first TAB has been created before this function, so apply window profile ps = "default profile" if firstprofile == "default" else "profile \"%s\"" % firstprofile if self.new_iterm: From 95094e668b08472a53f0230b89686bc2c84facf6 Mon Sep 17 00:00:00 2001 From: Emanuele Ruffaldi Date: Fri, 4 Nov 2016 09:26:58 +0100 Subject: [PATCH 6/6] removed TODO notice --- itermocil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/itermocil.py b/itermocil.py index ac7b5e2..b3b87c6 100644 --- a/itermocil.py +++ b/itermocil.py @@ -60,7 +60,7 @@ def __init__(self, teamocil_file, here=False, cwd=None): if 'pre' in self.parsed_config: self.applescript.append('do shell script "' + self.parsed_config['pre'] + ';"') - # TODO: profile stuff: profile cannot be set AFTER loading so it does not work for reusing windows + # cannot be set AFTER loading so it does not work for reusing windows # and for the first pane of first window firstprofile = "default" try: