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

feat: allow setting terminal color #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ Automatically spawn integrated terminal windows and split terminals, and run any

Simply configure your VSCode settings JSON file to look something like this:

```
```json
"restoreTerminals.terminals": [
{
"splitTerminals": [
{
"name": "server",
"commands": ["npm i", "npm run dev"]
"commands": ["npm i", "npm run dev"],
"color": "red"
},
{
"name": "client",
Expand Down Expand Up @@ -61,6 +62,8 @@ If you don't want the commands to actually run, just be pasted in the terminal,

If you don't like using split terminals, then just provide one object in each split terminal array, which should be the intuitive thing to do.

Color is not currently supported for split terminals. Only the first terminal will have the color.

Contributions to the [code](https://github.com/EthanSK/restore-terminals-vscode) are very welcome and much appreciated!

**Enjoy!**
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"@types/mocha": "^7.0.2",
"@types/node": "^13.11.0",
"@types/text-encoding": "^0.0.35",
"@types/vscode": "^1.46.0",
"@types/vscode": "^1.67.0",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it required to update the version? because this could possibly be breaking otherwise.

"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"eslint": "^7.22.0",
Expand Down
13 changes: 12 additions & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import type { TerminalOptions } from "vscode";

export enum TerminalColor {
"black" = "terminal.ansiBlack",
"red" = "terminal.ansiRed",
"green" = "terminal.ansiGreen",
"yellow" = "terminal.ansiYellow",
"blue" = "terminal.ansiBlue",
"magenta" = "terminal.ansiMagenta",
"cyan" = "terminal.ansiCyan",
"white" = "terminal.ansiWhite",
}
Comment on lines +3 to +12

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why put this in an enum if you're gonna use it like a map? Imo it's better to do a readonly object.


export interface TerminalConfig {
commands?: string[];
name?: string;
icon?: string;
color?: string;
color?: keyof typeof TerminalColor;
shouldRunCommands?: boolean; //whether to actually run the commands, or just paste them in
}

Expand Down
14 changes: 13 additions & 1 deletion src/restoreTerminals.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as vscode from "vscode";
import { delay } from "./utils";
import { Configuration, TerminalConfig, TerminalWindow } from "./model";
import {
Configuration,
TerminalColor,
TerminalConfig,
TerminalWindow,
} from "./model";

const DEFAULT_ARTIFICAL_DELAY = 300;
const SPLIT_TERM_CHECK_DELAY = 100;
Expand Down Expand Up @@ -66,8 +71,15 @@ export default async function restoreTerminals(configuration: Configuration) {
);
}
} else {
let color = TerminalColor.white;

if (terminalWindow.splitTerminals[0]?.color) {
color = TerminalColor[terminalWindow.splitTerminals[0].color];
}
Comment on lines +76 to +78

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check that terminalWindow.splitTerminals[0]?.color is an actual color and not something random the user has inputed. Otherwise color will be undefined.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case, it falls back to white


term = vscode.window.createTerminal({
name: name,
color: new vscode.ThemeColor(color),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what happens if you call vscode.ThemeColor constructor with undefined, but it's better to ensure that color never is white instead of undefined.

iconPath: new vscode.ThemeIcon(icon ?? "terminal"),
// cwd: vscode.window.activeTextEditor?.document.uri.fsPath, //i think this happens by default
});
Expand Down