Skip to content

Commit

Permalink
fix: enable save sesion feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenngoclongdev authored Oct 10, 2024
1 parent bfc2366 commit 3369073
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 53 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ Get it from [Visual Studio Marketplace](https://marketplace.visualstudio.com/ite

# Features

- Effortlessly generate configuration templates.
- Automatically restore your last terminal session on startup.
- Easily select which terminal session to activate.
- Seamlessly remove unwanted terminal sessions.
- Explore a variety of built-in themes for terminal icons and colors.
- Import commands from files like package.json, pipenv, makefile, grunt, gradle, gulp, ant, and more.
- ~~Save the current terminal session~~ (this feature is currently disabled due to limitations in the VSCode API. We are unable to detect certain features such as icons, colors, and split terminals. We may re-enable this feature if the VSCode API provides full support for these features.)
- Easy Configuration: Quickly create templates to simplify your terminal setup.
- Restore Last Session Automatically: Start your day smoothly by bringing back your last terminal session when you open the app.
- Select Your Terminal Session: Choose which terminal session to open with just a few clicks, giving you control over your work.
- Remove Unwanted Sessions: Easily delete any terminal sessions you no longer need, keeping your workspace tidy.
- Explore Vibrant Themes: Personalize your terminal with a variety of colorful themes for icons and colors that brighten your workspace.
- ✨ Simple Session Management: Use Terminal Keeper Activity to manage your terminal sessions effortlessly.
- ✨ Import Commands with Ease: Bring in commands from files like package.json, pipenv, makefile, grunt, gradle, gulp, ant, and more quickly.
-~~Save Your Terminal Session~~: Automatically detect icons, colors, and split terminals to create a new session. (Note: Due to current limitations in the VSCode API, retrieving the last command from the current terminal isn't supported yet. Please fill it in manually. In the feature if api support we can supported.)

## Using the extension

Expand Down
101 changes: 55 additions & 46 deletions src/commands/saveAsync.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
import { TerminalApi } from '@vscode-utility/terminal-browserify';
import { QuickPickItem, window } from 'vscode';
import { Configuration } from '../configuration/configuration';
import { SessionItem } from '../configuration/interface';
import { SessionConfiguration, SessionItem } from '../configuration/interface';
import { constants } from '../utils/constants';
import { getSessionQuickPickItems, showErrorMessageWithDetail, showInputBox, showTextDocument } from '../utils/utils';
import { TerminalApi } from '@vscode-utility/terminal-browserify';
import { getSessionQuickPickItems, showErrorMessageWithDetail, showTextDocument } from '../utils/utils';

const chooseSessionName = async (config: SessionConfiguration): Promise<string | undefined> => {
// Get current session
if (!config.sessions) {
config.sessions = { default: [] };
}

// Show choose session name box
const sessionsWithDescription: QuickPickItem[] = getSessionQuickPickItems(config.sessions);
sessionsWithDescription.forEach((sessionItem) => {
sessionItem.detail = `Overwrites scripts to session ${sessionItem.label}`;
});
const addNewSession: QuickPickItem = {
label: 'Add new session...',
detail: 'Create new session, and save scripts to it.',
alwaysShow: true
};
const quickPickItem = await window.showQuickPick([addNewSession].concat(sessionsWithDescription), {
title: 'Select the session you want to override or add new session',
placeHolder: 'Session name...',
ignoreFocusOut: true
});
if (!quickPickItem) {
return undefined;
}

// Show input box if select add new
let sessionName = quickPickItem.label;
if (sessionName === addNewSession.label) {
const sessionNameInput = await window.showInputBox({
title: 'Please enter the session name.',
placeHolder: 'e.g. build, migrate, start, deploy',
ignoreFocusOut: true,
validateInput: (value: string) => {
if (!value) {
return 'The session name cannot be null or empty.';
}
if (sessionsWithDescription.some((s) => s.label === value)) {
return 'The session name already exists.';
}
return ''; // input valid is OK
}
});
return sessionNameInput ? sessionNameInput : undefined;
}
return sessionName;
};

export const saveAsync = async (): Promise<void> => {
try {
Expand All @@ -13,57 +60,19 @@ export const saveAsync = async (): Promise<void> => {
window.showWarningMessage(constants.notExistConfiguration);
return;
}

const config = await Configuration.load();
if (!config) {
window.showWarningMessage(constants.notExistConfiguration);
return;
}

// Check the size of sessions
const { sessions } = config;
const sessionsWithDescription: QuickPickItem[] = getSessionQuickPickItems(sessions);
const sessionKeys = sessionsWithDescription.map((session) => session.label);

// Show quick pick to allow override on existed session
const quickPickSession = await window.showQuickPick(
sessionsWithDescription.concat({ label: constants.newSession, alwaysShow: true }),
{
title: constants.selectSessionSaveTitle,
placeHolder: constants.selectSessionSavePlaceHolder,
canPickMany: false,
ignoreFocusOut: true
}
);
if (!quickPickSession) {
// Select name of sessions to override or add new session
let sessionName = await chooseSessionName(config);
if (!sessionName) {
return;
}

// Set selected session
let selectedSession = quickPickSession.label;
if (quickPickSession.label === constants.newSession && quickPickSession.alwaysShow) {
// Show input box to enter the session name
const inputBoxContent = await showInputBox({
title: constants.enterSessionNameTitle,
placeHolder: constants.enterSessionNamePlaceHolder,
validateInput: (value) => {
if (!value) {
return constants.sessionNameNotEmpty;
}
if (sessionKeys.includes(value)) {
return constants.sessionNameIsDuplicated;
}
return ''; // input valid is OK
}
});
if (!inputBoxContent) {
return;
}

// Set input content as selected session
selectedSession = inputBoxContent;
}

// Scan all current terminal
const session: SessionItem[] = TerminalApi.instance().getCurrentTerminals();

Expand All @@ -73,7 +82,7 @@ export const saveAsync = async (): Promise<void> => {
default: []
};
}
const newestSessions = { ...config.sessions, ...{ [selectedSession]: session } };
const newestSessions = { ...config.sessions, ...{ [sessionName]: session } };
await Configuration.save({ sessions: newestSessions });

// Show message
Expand Down

0 comments on commit 3369073

Please sign in to comment.