Sublime Forum

Build & Execute in already-open Terminus Tab

#1

I’m fairly new to using Sublime Text, and was very fortunate to come across @OdatNurd’s content. With his YouTube video, I was able to set up a Java build environment that works very nicely, and is a lot less resource-intensive than my IDE. Thank you so much for putting your content out there!

I’m using Origami to split the window and bring up Terminus beside my source. The code is compiled and then executed using a single command (I may split this up with variants later). The “problem” I’m encountering is that when testing, I may execute my code several times – and each time I press Command + B, it spawns a new Terminus window.

Here’s what I’d like to have happen:

  • If no Terminus tab is open, open it and execute my shell_cmd
  • If a terminus tab is open, clear the open shell and execute the shell_cmd within the already-open tab.

I poked around on the Terminus readme, and I see that they have the concept of a “tag”. I’m thinking that I should be able to call the tag, and if it exists, do what I described… but I don’t know how to do that.

Here’s what I have so far:
{
“target”: “terminus_open”,
“cancel”: “terminus_cancel_build”,

“auto_close”: false,
“focus”: true,
“timeit”: false,
“title”: “Java Execution”,
“post_window_hooks”: [
[“carry_file_to_pane”, {“direction”: “right”}]
],
“working_dir”: “$file_path”,
“shell_cmd”: “/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/javac “$file” && /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/java “$file_base_name””,
“file_regex” : “^[ ]File "(…?)”, line ([0-9]*)",
“selector”: “source.java”,
“tag”: “Java Exeucutor”
}

1 Like

#2

Something to note is that when you give Terminus something to execute, once that task is finished running, the connection between it and Terminus is broken. So, unless you run an interactive shell (say cmd.exe or /bin/bash) you can’t execute something else in the same Tab once the first thing is done.

That said, I wrote a simple plugin to make this possible for myself a couple of weeks ago. Combined with a tweak to the build system, the same effect is achieved.

First, the plugin (see this video if you’re not sure how to use one):

import sublime
import sublime_plugin


class CloseTerminusViewCommand(sublime_plugin.WindowCommand):
    def run(self, title):
        current = self.window.active_view()

        for view in self.window.views():
            if view.name() == title:
                view.run_command('terminus_close')

        self.window.focus_view(current)

This implements a new close_terminus_view command which looks through all of the tabs in the current window for any that have a specific title, and then it closes them (it assumes such found tabs are Terminus views).

With that in place, you need to make sure that there’s a title set in your Terminus build to name the tab, and then you need to add a pre_window_hooks to get Terminus to execute the command before it creates the new Tab and starts things running.

For completeness, my sample build looks like the following, but in your case you’d just need to put the pre_window_hooks in place in your build and adjust the title as appropriate:

{
    "target": "terminus_open",

    "shell_cmd": "yarn ts-node \"src/$file_name\"",
    "working_dir": "$file_path",
    "selector": "source.ts",

    "title": "Async TS",
    "auto_close": false,

    "pre_window_hooks": [
        ["close_terminus_view", {"title": "Async TS"}]
    ],

    "post_window_hooks": [
        ["carry_file_to_pane", {"direction": "right"}]
    ]
}

Now every time you run a build, Terminus will first run close_terminus_view to find and close the existing tab (if any), then it will create a new one and shift it to the right. The effect is that the build on the right is always “current”, though it does require that the focus be in the file on the left when you build.

As a note on your build, you don’t need the cancel key if you’re using terminus_open; you can cancel the build by closing the tab if you need to.

4 Likes

Thank you to Jon Skinner and OdatNurd
#3

Thank you so much @OdatNurd!

This is very helpful, and does exactly what I wanted it to do. I really appreciate you taking the time out to share your solution!

1 Like

#4

Can you please explain to me what should I write within the “package.json”?
I’m novice in this

Thank you

screenshot:
2021-04-30_175621

0 Likes