Skip to content

Commit

Permalink
Make SSH Quick Open work
Browse files Browse the repository at this point in the history
  • Loading branch information
sedwards2009 committed Jun 30, 2024
1 parent 6c280a1 commit 2cd7ba0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 22 deletions.
5 changes: 2 additions & 3 deletions extensions/SSHQuickOpen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"eslint": "8.53.0",
"eslint-config-extraterm": "1.0.0",
"eslint-plugin-unicorn": "42.0.0",
"fs-extra": "^5.0.0",
"lodash-es": "4.17.21",
"extraterm-uuid": "1.0.0",
"qt-construct": "0.1.0",
"shx": "^0.3.2",
"typescript": "5.2.2"
Expand All @@ -36,7 +35,7 @@
"command": "ssh-quick-open:open",
"title": "SSH Quick Open",
"category": "terminal",
"icon": "fa-search"
"icon": "fa-plug"
}
]
}
Expand Down
54 changes: 51 additions & 3 deletions extensions/SSHQuickOpen/src/SSHQuickOpenExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
*
* This source code is licensed under the MIT license which is detailed in the LICENSE.txt file.
*/
import { ExtensionContext, Logger } from '@extraterm/extraterm-extension-api';
import { ExtensionContext, Logger, SessionConfiguration } from '@extraterm/extraterm-extension-api';
import { createUuid } from "extraterm-uuid";


let log: Logger = null;
let context: ExtensionContext = null;
Expand All @@ -15,9 +17,55 @@ export function activate(_context: ExtensionContext): any {
context.commands.registerCommand("ssh-quick-open:open", quickOpenCommand);
}

// Note: This is mostlyduplicated in SSHSessionEditorExtension.ts.
interface SSHSessionConfiguration extends SessionConfiguration {
host?: string;
port?: number;
username?: string;
authenticationMethod?: number; // AuthenticationMethod;
}

async function quickOpenCommand(): Promise<void> {
const selected = await context.activeTerminal.tab.showTextInput({
message: "Enter a command",
const sshConnectionString = await context.activeTerminal.tab.showTextInput({
message: "Enter a SSH connection string:",
value: "",
});
if (sshConnectionString == null) {
return;
}

const sshSessionConfiguration = parseConnectionString(sshConnectionString);
context.commands.executeCommand("extraterm:window.newTerminal", {sessionConfiguration: sshSessionConfiguration});
}

function parseConnectionString(sshConnectionString: string): SSHSessionConfiguration {
let username: string = null;

const parts = sshConnectionString.split("@");
let hostnamePort = sshConnectionString;
if (parts.length === 2) {
username = parts[0];
hostnamePort = parts[1];
}

const hostPortParts = hostnamePort.split(":");
let host: string = hostnamePort;
let port = 22;
if (hostPortParts.length === 2) {
host = hostPortParts[0];
const parsedPort = parseInt(hostPortParts[1], 10);
if (! isNaN(parsedPort)) {
port = parsedPort;
}
}

return {
uuid: createUuid(),
name: sshConnectionString,
type: "ssh",
authenticationMethod: 0,
host,
port,
username,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface SSHSessionConfiguration extends SessionConfiguration {
host?: string;
port?: number;
username?: string;
authenicationMethod?: AuthenticationMethod;
authenticationMethod?: AuthenticationMethod;
keyFilePath?: string;
verboseLogging?: boolean;
}
Expand Down Expand Up @@ -58,7 +58,7 @@ class SSHBackend implements SessionBackend {

const privateKeyFilenames: string[] = [];
let tryPasswordAuth = false;
switch (sessionConfig.authenicationMethod) {
switch (sessionConfig.authenticationMethod) {
case AuthenticationMethod.DEFAULT_KEYS_PASSWORD:
const homeDir = os.homedir();
privateKeyFilenames.push(path.join(homeDir, ".ssh", "id_rsa"));
Expand Down Expand Up @@ -98,8 +98,8 @@ class SSHBackend implements SessionBackend {
}

#createAgentSocketPath(sessionConfig: SSHSessionConfiguration): string {
const needAgent = sessionConfig.authenicationMethod === AuthenticationMethod.DEFAULT_KEYS_PASSWORD ||
sessionConfig.authenicationMethod === AuthenticationMethod.SSH_AGENT_ONLY;
const needAgent = sessionConfig.authenticationMethod === AuthenticationMethod.DEFAULT_KEYS_PASSWORD ||
sessionConfig.authenticationMethod === AuthenticationMethod.SSH_AGENT_ONLY;
if (! needAgent) {
return undefined;
}
Expand Down
10 changes: 5 additions & 5 deletions extensions/SSHSessionEditor/src/SSHSessionEditorExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface SSHSessionConfiguration extends SessionConfiguration {
host?: string;
port?: number;
username?: string;
authenicationMethod?: AuthenticationMethod;
authenticationMethod?: AuthenticationMethod;
keyFilePath?: string;
verboseLogging?: boolean;
}
Expand Down Expand Up @@ -108,10 +108,10 @@ class EditorUi {

"Authentication:",
ComboBox({
currentIndex: this.#config.authenicationMethod ?? AuthenticationMethod.DEFAULT_KEYS_PASSWORD,
currentIndex: this.#config.authenticationMethod ?? AuthenticationMethod.DEFAULT_KEYS_PASSWORD,
items: AUTHENTICATION_METHOD_LABELS,
onCurrentIndexChanged: (index: number): void => {
this.#config.authenicationMethod = index;
this.#config.authenticationMethod = index;
sessionEditorBase.setSessionConfiguration(this.#config);
this.#updateKeyFileLabel();
}
Expand Down Expand Up @@ -139,7 +139,7 @@ class EditorUi {
onClicked: (): void => {
this.#handleSelectKeyFile();
},
enabled: this.#config.authenicationMethod === AuthenticationMethod.KEY_FILE_ONLY,
enabled: this.#config.authenticationMethod === AuthenticationMethod.KEY_FILE_ONLY,
}),
stretch: 0,
}
Expand All @@ -163,7 +163,7 @@ class EditorUi {

#updateKeyFileLabel(): void {
this.#selectedKeyFileLineEdit.setText(this.#config.keyFilePath ?? "");
const enabled = this.#config.authenicationMethod === AuthenticationMethod.KEY_FILE_ONLY;
const enabled = this.#config.authenticationMethod === AuthenticationMethod.KEY_FILE_ONLY;
this.#selectedKeyFileLineEdit.setEnabled(enabled);
this.#selectKeyFileButton.setEnabled(enabled);
}
Expand Down
27 changes: 21 additions & 6 deletions main/src/extension/TextInputPopOver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import * as ExtensionApi from "@extraterm/extraterm-extension-api";
import { Logger, log, getLogger } from "extraterm-logging";
import { doLater } from "extraterm-timeoutqt";
import { Label } from "qt-construct";
import { Label, LineEdit } from "qt-construct";
import { Window } from "../Window.js";
import { UiStyle } from "../ui/UiStyle.js";
import { WindowPopOver } from "../ui/WindowPopOver.js";
import { Direction, QLabel, QPushButton, QRect, TextFormat } from "@nodegui/nodegui";
import { Direction, Key, QKeyEvent, QLabel, QLineEdit, QPushButton, QRect, TextFormat } from "@nodegui/nodegui";
import { BoxLayout, Widget } from "qt-construct";


Expand All @@ -23,9 +23,9 @@ export class TextInputPopOver {
private _log: Logger = null;
#uiStyle: UiStyle = null;
#messageLabel: QLabel = null;
#lineEdit: QLineEdit = null;
#windowPopOver: WindowPopOver = null;
#containingRect: QRect = null;
#optionButtons: QPushButton[] = [];

#resolveFunc: (value: string | undefined) => void = null;

Expand All @@ -47,6 +47,11 @@ export class TextInputPopOver {
wordWrap: true,
openExternalLinks: true
}),
this.#lineEdit = LineEdit({
onKeyPress: (nativeEvent) => {
this.#handleKeyPress(new QKeyEvent(nativeEvent));
},
}),
]
})
})
Expand All @@ -58,6 +63,16 @@ export class TextInputPopOver {
this.#sendResult(undefined);
}

#handleKeyPress(event: QKeyEvent): void {
const key = event.key();
if (key === Key.Key_Enter || key === Key.Key_Return) {
event.accept();
this.#lineEdit.setEventProcessed(true);
this.#sendResult(this.#lineEdit.text());
return;
}
}

#sendResult(value: string | undefined) :void {
this.hide();
doLater( () => {
Expand Down Expand Up @@ -87,6 +102,8 @@ export class TextInputPopOver {
this.#messageLabel.setTextFormat(TextFormat.RichText);
}

this.#lineEdit.setText(options.value ?? "");

if (options.aroundRect != null) {
const screenGeometry = window.getWidget().windowHandle().screen().geometry();
const maxHeight = Math.floor(screenGeometry.height() - options.aroundRect.height() / 2);
Expand All @@ -101,9 +118,7 @@ export class TextInputPopOver {
});

this.#windowPopOver.show();
if (this.#optionButtons.length !== 0){
this.#optionButtons[0].setFocus();
}
this.#lineEdit.setFocus();

return new Promise<string | undefined>((resolve, reject) => {
this.#resolveFunc = resolve;
Expand Down
4 changes: 3 additions & 1 deletion main/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ class Main {
}

async commandNewTerminal(state: CommonExtensionWindowState, args: {sessionUuid?: string, sessionName?: string,
workingDirectory?: string}): Promise<void> {
workingDirectory?: string, sessionConfiguration?: SessionConfiguration}): Promise<void> {

let sessionConfiguration: SessionConfiguration = this.#configDatabase.getSessionConfig()[0];
if (args.sessionUuid != null) {
Expand All @@ -546,6 +546,8 @@ class Main {
if (sessionConfiguration == null) {
throw new Error(`Unable to find session with name ${args.sessionName}`);
}
} else if (args.sessionConfiguration != null) {
sessionConfiguration = args.sessionConfiguration;
}

const activeTab = state.activeTab;
Expand Down

0 comments on commit 2cd7ba0

Please sign in to comment.