Skip to content

Commit

Permalink
Add terminal profile resolver
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Nikitenko <[email protected]>
  • Loading branch information
RomanNikitenko committed Jun 29, 2023
1 parent b3530c3 commit 4839988
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions code/extensions/che-terminal/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import * as vscode from 'vscode';
import { MachineExecClient, TerminalSession } from './machine-exec-client';
import { resolveTerminalProfile } from './profile-resolver';

let _channel: vscode.OutputChannel;
export function getOutputChannel(): vscode.OutputChannel {
Expand Down Expand Up @@ -57,6 +58,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<Api> {
return new MachineExecPTY(machineExecClient, containerName, command, workdir);
}
};

resolveTerminalProfile(getOutputChannel());

return api;
}

Expand Down
49 changes: 49 additions & 0 deletions code/extensions/che-terminal/src/profile-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**********************************************************************
* Copyright (c) 2023 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

/* eslint-disable header/header */

import * as vscode from 'vscode';
import * as cp from 'child_process';

const TERMINAL_SECTION_ID = 'terminal';
const LINUX_DEFAULT_PROFILE_ID = 'integrated.defaultProfile.linux';
const WARNING_MESSAGE = 'Default terminal profile is not configured, you can try sh profile as default or select another profile in settings';
const SH_AS_DEFAULT = 'Use sh as default profile';
const OPEN_SETTINGS = 'Open Settings';

export async function resolveTerminalProfile(outputChannel: vscode.OutputChannel): Promise<void> {
const shellOutput = cp.execSync('echo ${SHELL}') || '';
const shellValue = shellOutput.toString();
outputChannel.appendLine(`echo $SHELL: ${shellValue}`);

const shellEnvVar = process.env.SHELL;
outputChannel.appendLine(`SHELL env variable: ${shellEnvVar}`);

const terminalConfig = vscode.workspace.getConfiguration(TERMINAL_SECTION_ID);
const defaultProfile = terminalConfig.get<string>(LINUX_DEFAULT_PROFILE_ID);
outputChannel.appendLine(`Default terminal profile: ${defaultProfile}`);

if ((!shellValue || shellValue.includes('nologin')) && !defaultProfile) {
outputChannel.appendLine('Default terminal profile is not configured');
return suggestProfileResolving(terminalConfig, outputChannel);
}
}

async function suggestProfileResolving(terminalConfig: vscode.WorkspaceConfiguration, outputChannel: vscode.OutputChannel): Promise<void> {
const selected = await vscode.window.showWarningMessage(WARNING_MESSAGE, OPEN_SETTINGS, SH_AS_DEFAULT);
outputChannel.appendLine(`${selected} was selected for resolving default terminal profile`);

if (selected === OPEN_SETTINGS) {
vscode.commands.executeCommand('workbench.action.openSettings2', { query: `${TERMINAL_SECTION_ID}.${LINUX_DEFAULT_PROFILE_ID}` });
} else if (selected === SH_AS_DEFAULT) {
terminalConfig.update(LINUX_DEFAULT_PROFILE_ID, 'sh', false);
}
}

0 comments on commit 4839988

Please sign in to comment.