diff --git a/package.json b/package.json index b7f72ad..c907d14 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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 " } } } diff --git a/src/changeLabel.ts b/src/changeLabel.ts new file mode 100644 index 0000000..dad33c3 --- /dev/null +++ b/src/changeLabel.ts @@ -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('title') || ''; + if (label) { + const newTitle = addOrReplaceLabel(label, currentTitleSetting); + settings.update('title', newTitle, vscode.ConfigurationTarget.Workspace); + } else { + settings.update('title', undefined, vscode.ConfigurationTarget.Workspace); + } +} \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index be5940b..acbe82c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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) { @@ -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); } diff --git a/src/getColor.ts b/src/getMapping.ts similarity index 60% rename from src/getColor.ts rename to src/getMapping.ts index 53cdf16..8b5e937 100644 --- a/src/getColor.ts +++ b/src/getMapping.ts @@ -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; } \ No newline at end of file diff --git a/src/getSettings.ts b/src/getSettings.ts index cf5cbb9..d21fcc4 100644 --- a/src/getSettings.ts +++ b/src/getSettings.ts @@ -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 \ No newline at end of file +export default () => vscode.workspace.getConfiguration('colorTabs') as AllSettings; \ No newline at end of file diff --git a/tslint.json b/tslint.json index 2bd680d..92e24bf 100644 --- a/tslint.json +++ b/tslint.json @@ -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,