Sublime Forum

Open ssh terminal command in sidebar

#1

I’m trying to create a right-click context menu for the sidebar that that allows me to open terminal with SSH command with variables from .sftp-config files.

First, context menu should be visible only on .sftp-config files, i cant find any command for realize that in https://www.sublimetext.com/docs/menus.html
that is what i have:
File: ~/Library/Application Support/Sublime Text/Packages/User/Side Bar.sublime-menu
[
{
“caption”: “Open SSH”,
“command”: “open_ssh”, “args”: {“files”: []}
},
]

I put file ~/Library/Application Support/Sublime Text/Packages/User/OpenSsh.py
with code
import sublime
import sublime_plugin

class OpenSshCommand(sublime_plugin.WindowCommand):
	def run(self, files):

	# Make command visible only when there is a single file
	def is_visible(self, files):
		return len(files) == 1

How can I make the command parse the .sftp-config file and open the terminal with variables
ssh $user@$host -p $port

.sftp-config example:
{

“host”: “123.123.123.123”,
“user”: “username”,
“port”: “22”,

}

Is it possible?

0 Likes

#2

Is the .sftp-config file a JSON file ? Asking because the relevant part of your request lies in the ability to parse these files. If it’s JSON files, then it’s pretty simple. If not, we’d have to see.

0 Likes

#3

yes, it’s simple json file

0 Likes

#4

Okay, before I get to what I came up for this, lets just break down some of the issues you have ran into.

I don’t think there is any command per say for that, but the Command classes do provide 2 special methods is_visible & is_enabled for this. is_visible simply removes the menu item if the method returns False & is_enabled disables the menu item (the menu item is still visible, just disabled) if the method returns False. You can choose whatever suits you.

Regarding your code itself, it’s incomplete. You have the menu file, menu entry, plugin file location etc. all correct. We just now need to flesh out the command itself.

Here is what I came up with.

import os
import json
import sublime
import sublime_plugin

class OpenSshCommand(sublime_plugin.WindowCommand):

    def run(self, files) -> None:
        window = self.window

        # We read the config file.
        with open(files[0], "r") as sftp_config:
            sftp_config_data = json.loads(sftp_config.read())

        # We first close all Terminus views.
        window.run_command("terminus_close_all")

        # Create a new Terminus view.
        window.run_command("terminus_open")

        # We can't run terminus_send_string directly, since a Terminus view may
        # not have been created, so schedule it to happen after some delay.
        sublime.set_timeout(
            lambda: self.send_string(window, sftp_config_data), 100
        )

    def is_enabled(self, files):
        if os.path.basename(files[0]) == ".sftp-config":
            return True
        return False

    def send_string(self, window, sftp_config_data):
        self.window.run_command("terminus_send_string", {
            "string": "ssh ${}@${} -p ${}".format(
                sftp_config_data["user"],
                sftp_config_data["host"],
                sftp_config_data["port"]
            )
        })

Note that it requires for you to install the Terminus package because I feel that it is an easier way to handle this in a cross platform manner.

1 Like