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 iTerm capability to ssh-manager #2084

Merged
merged 6 commits into from
Jun 28, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions extensions/ssh-manager/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ssh-manager Changelog

## [Add iTerm Support] - 2022-06-28
This version adds the option to add ssh connections in iTerm.
Checkout the preferences and select iTerm in the dropdown to get going.

## [Initial Version] - 2022-04-05
23 changes: 22 additions & 1 deletion extensions/ssh-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@
"icon": "command-icon.png",
"author": "dimagrossman",
"license": "MIT",
"preferences": [
{
"name": "terminal",
"type": "dropdown",
"required": false,
"title": "Decide which Terminal Application you want to use",
"description": "If iTerm is available, you can use iTerm instead of Terminal.",
"data": [{
"title": "Terminal",
"value": "Terminal"
},
{
"title": "iTerm",
"value": "iTerm"
}
],
"default": "Terminal"
}

],
"commands": [
{
"name": "create",
Expand Down Expand Up @@ -41,6 +61,7 @@
"scripts": {
"build": "ray build -e dist",
"dev": "ray develop",
"lint": "ray lint"
"lint": "ray lint",
"fix-lint": "ray lint --fix"
}
}
84 changes: 81 additions & 3 deletions extensions/ssh-manager/src/show.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { List, ActionPanel, showHUD } from "@raycast/api";
import { List, ActionPanel, showHUD, getPreferenceValues } from "@raycast/api";
import { useEffect, useState } from "react";
import { runAppleScript } from "run-applescript";
import { ISSHConnection } from "./types";
import { getConnections, saveConnections } from "./storage.api";

interface Preferences {
terminal: string;
}
const preferences = getPreferenceValues<Preferences>();
export const terminal = preferences["terminal"];

async function runTerminal(item: ISSHConnection) {
let identity = "";
if (item.sshKey) {
identity = `-i ${item.sshKey} `;
}
const command = `ssh ${identity} ${item.user}@${item.address}`;

const script = `
const scriptTerminal = `
tell application "Terminal"
do script ""
activate
Expand All @@ -25,8 +31,80 @@ async function runTerminal(item: ISSHConnection) {
if result is not {} then perform action "AXRaise" of item 1 of result
end tell
`;
const scriptIterm = `
-- Set this property to true to open in a new window instead of a new tab
property open_in_new_window : false

on new_window()
tell application "iTerm" to create window with default profile
end new_window

on new_tab()
tell application "iTerm" to tell the first window to create tab with default profile
end new_tab

on call_forward()
tell application "iTerm" to activate
end call_forward

on is_running()
application "iTerm" is running
end is_running

on is_processing()
tell application "iTerm" to tell the first window to tell current session to get is processing
end is_processing

on has_windows()
if not is_running() then return false
if windows of application "iTerm" is {} then return false
true
end has_windows

on send_text(custom_text)
tell application "iTerm" to tell the first window to tell current session to write text custom_text
end send_text

-- Main
if has_windows() then
-- Open the command in the current session unless it has a running command, e.g., ssh or top
if is_processing() then
if open_in_new_window then
new_window()
else
new_tab()
end if
end if
else
-- If iTerm is not running and we tell it to create a new window, we get two
-- One from opening the application, and the other from the command
if is_running() then
new_window()
else
call_forward()
end if
end if

-- Make sure a window exists before we continue, or the write may fail
repeat until has_windows()
delay 0.01
end repeat

send_text("${command}")
call_forward()
`;

if (terminal == "iTerm") {
try {
await runAppleScript(scriptIterm);
} catch (error) {
await runAppleScript(scriptTerminal);
console.log(error);
}
} else {
await runAppleScript(scriptTerminal);
}

await runAppleScript(script);
await showHUD("Success ✅");
}

Expand Down