LesBeCool
LesBeCool

Reputation: 1

Sublime Text 3 - Add a pane at the bottom to my current layout

I wanted to make a simple plugin for ST3 that adds a pane at the bottom of my screen with a simple key board shortcut. I am aware of Origami, and TBH, would use it for a task like this, but I am doing this more out of getting a taste of making ST3 plugins.

NOTE: I do not want to change the layout. Whatever is there, add a pane at the bottom to it.

So for example, If my current screen tabs are arranged like this:

----------
| tab 1  |
|        |
----------
| tab 2  |
|        |
----------

Then I want to add a tab 3 so it is positioned like this:

----------
| tab 1  |
|        |
----------
| tab 2  |
----------
| tab 3  |
----------

Some more examples:

-----------------        ------------------
| tab 1 | tab 2 |        | tab 1  | tab 2 |
|       |       |        |        |       |
|       |       |  --- > ------------------ 
|       |       |        |      tab 3     |
-----------------        ------------------

-----------------        ------------------
| tab 1 | tab 2 |        | tab 1  | tab 2 |
|       |       |        |        |       |
-----------------  --- > ------------------
| tab 3 | tab 4 |        | tab 3  | tab 4 |
|       |       |        ------------------
-----------------        |    tab 5       |
                         ------------------

Here is the code I have so for:

import sublime
import sublime_plugin
import subprocess

class CreateBottomTabCommand(sublime_plugin.WindowCommand):
    def run(self):
        layout = self.window.get_layout()
        newLayout = calculate_new_layout_with_extra_pannel_at_bottom() # How do I do this?
        self.window.run_command("set_layout", newLayout)

Upvotes: 0

Views: 205

Answers (1)

user10408316
user10408316

Reputation:

Calling window.get_layout() returns an object like this:

{
    "cells": [[0,0,1,1],[1,0,2,2]],
    "cols": [0.0, 0.5, 1],
    "rows", [0.0, 1.0]
}

The cells attribute has a list of lists, where each inner list is the co-ordinate of that cell.

cols contains the relative Y position from (0 - 1) of each place to split the screen vertically at

rows is similar to cols but for horizontal breaks instead

To keep this brief I will reference you to the docs for more info.

So now, what we want to do, is insert your extra row in the middle of the last row and 1, (bottom of screen):

terminalPos = round(1 - ((1 - layout["rows"][-2]) / 2), 1)

We also want to give this it's cooridnates:

terminalCoords = [0, len(layout["rows"]) - 1 , len(layout["cols"]) - 1, len(layout["rows"])]

Then you can put these in the correct position the object, and presto! You're done!

Here is the full code:

import sublime
import sublime_plugin
import subprocess

class CreateTerminalCommand(sublime_plugin.WindowCommand):
    def run(self):
        layout = self.window.get_layout()

        terminalPos = round(1 - ((1 - layout["rows"][-2]) / 2), 1)
        terminalCoords = [0, len(layout["rows"]) - 1 , len(layout["cols"]) - 1, len(layout["rows"])]

        newLayout = layout
        newLayout["rows"].insert(-1, terminalPos)
        newLayout["cells"].append(terminalCoords)

        self.window.run_command("set_layout", newLayout)

Upvotes: 1

Related Questions