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/title bar label #4

Merged
merged 3 commits into from
Feb 26, 2019
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
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
"default": [
{
"regex": ".*/web/.*",
"color": "#c0d8e3"
"color": "#c0d8e3",
"label": "WEB"
},
{
"regex": ".*/mobile/.*",
"color": "#e18a7a"
"color": "#e18a7a",
"label": "MOBILE"
}
],
"description": "list of mappings from path to color"
Expand All @@ -55,6 +57,11 @@
"type": "boolean",
"default": false,
"description": "color title bar background if regex match found"
},
"colorTabs.titleLabel": {
"type": "boolean",
"default": false,
"description": "append label to the title bar using the provided regex label "
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/changeLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as vscode from 'vscode';
import getSettings from './getSettings';

const addOrReplaceLabel = (label: string, originalString: string): string => {
const regex = new RegExp(/(~\[.*\]~)(.*)/, 'g');
const hasLabel = regex.test(originalString);


if (hasLabel) {
return originalString.replace(regex, `~[${label}]~\$\{\separator}$2`);
} else {
return `~[${label}]~\$\{\separator}${originalString}`;
}
}

const isFeatureEnabled = (): boolean => {
const extensionSettings = getSettings();
const shouldAppendLabel = extensionSettings.titleLabel;
return !!shouldAppendLabel;
}

export default async (label?: string) => {
if (!isFeatureEnabled()) return;

const settings = vscode.workspace.getConfiguration('window');
const currentTitleSetting = settings.get<string>('title') || '';
if (label) {
const newTitle = addOrReplaceLabel(label, currentTitleSetting);
settings.update('title', newTitle, vscode.ConfigurationTarget.Workspace);
} else {
settings.update('title', undefined, vscode.ConfigurationTarget.Workspace);
}
}
10 changes: 7 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
import * as vscode from 'vscode';
import getColorForPath from './getColor';
import getMapping from './getMapping';
import changeColors from './changeColors';
import changeLabel from './changeLabel';

export function activate(context: vscode.ExtensionContext) {

Expand All @@ -10,9 +11,12 @@ export function activate(context: vscode.ExtensionContext) {
if (!e) return null;
const currentlyOpenTabfilePath = e.document.fileName;

const color = getColorForPath(currentlyOpenTabfilePath);
const mapping = getMapping(currentlyOpenTabfilePath);
try {
await changeColors(color);
await Promise.all([
changeColors(mapping && mapping.color),
changeLabel(mapping && mapping.label)
]);
} catch (error) {
console.log("ERROR", error);
}
Expand Down
6 changes: 3 additions & 3 deletions src/getColor.ts → src/getMapping.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import getSettings from './getSettings'
import getSettings, {ColorRegex} from './getSettings';

export default (path: string) => {
export default (path: string): ColorRegex | undefined => {
const config = getSettings().config;
if (!config) return undefined;

const map = config.find(mapping => new RegExp(mapping.regex, 'g').test(path));
if (!map) return undefined;
return map.color;
return map;
}
8 changes: 5 additions & 3 deletions src/getSettings.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import * as vscode from 'vscode';

type ColorRegex = {
export type ColorRegex = {
regex: string;
color: string;
}
label?: string;
};

type AllSettings = {
config?: ColorRegex[];
tabBorder?: boolean;
titleBackground?: boolean;
titleLabel?: boolean;
}

export default () => vscode.workspace.getConfiguration('colorTabs') as unknown as AllSettings
export default () => vscode.workspace.getConfiguration('colorTabs') as AllSettings;
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"no-string-throw": true,
"no-unused-expression": true,
"no-duplicate-variable": true,
"curly": true,
"curly": false,
"class-name": true,
"semicolon": [
true,
Expand Down