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

Add option to supply the project name using a command or script #3

Open
wants to merge 2 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
## [Unreleased]

### Added
* A `commandOutput` option for the `projectNameInStatusBar.source` setting, which allows using a shell command to supply the project name. For instance, calling a script which prints the project name.
* A new `"projectNameInStatusBar.command` setting which specifies the command to be executed when using the `commandOutput` source option.

## [Released]
- Initial release
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ Also provides few options for label template and it's alignment in status bar.
"type": "string",
"enum": [
"none",
"folderName"
"folderName",
"commandOutput"
],
"default": "folderName",
"description": "Defines way of getting project name"
},
"projectNameInStatusBar.command": {
"type": "string",
"default": "echo \"ProjectName\"",
"description": "Specifies the command for when using the 'commandOutput' source option. Uses first line of the command output."
},
"projectNameInStatusBar.align": {
"type": "string",
"enum": [
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@
"type": "string",
"enum": [
"none",
"folderName"
"folderName",
"commandOutput"
],
"default": "folderName",
"description": "Defines way of getting project name"
},
"projectNameInStatusBar.command": {
"type": "string",
"default": "echo \"ProjectName\"",
"description": "Specifies the command for when using commandOutput as source. Uses first line of the command output."
},
"projectNameInStatusBar.align": {
"type": "string",
"enum": [
Expand Down
77 changes: 56 additions & 21 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

import * as vscode from 'vscode';
import { exec } from 'child_process';

declare type UpdateStatusBarItemCallback = (projectName: string | undefined) => void;

export function activate(context: vscode.ExtensionContext) {
let onDidChangeWorkspaceFoldersDisposable: vscode.Disposable | undefined;
Expand Down Expand Up @@ -35,14 +38,18 @@ export function activate(context: vscode.ExtensionContext) {
Array.isArray(vscode.workspace.workspaceFolders) && (vscode.workspace.workspaceFolders.length > 1)
? !onDidChangeActiveTextEditorDisposable && (onDidChangeActiveTextEditorDisposable =
vscode.window.onDidChangeActiveTextEditor(() => updateStatusBarItem()))
: onDidChangeActiveTextEditorDisposable && onDidChangeActiveTextEditorDisposable.dispose();;
: onDidChangeActiveTextEditorDisposable && onDidChangeActiveTextEditorDisposable.dispose();
}
}

function getSource(): string {
return <string>vscode.workspace.getConfiguration('projectNameInStatusBar').get('source');
}

function getCommand(): string {
return <string>vscode.workspace.getConfiguration('projectNameInStatusBar').get('command');
}

function getTextStyle(): string {
return <string>vscode.workspace.getConfiguration('projectNameInStatusBar').get('textStyle');
}
Expand All @@ -68,31 +75,40 @@ export function activate(context: vscode.ExtensionContext) {
}

function updateStatusBarItem() {
let projectName: string | undefined;
getProjectName(projectName => {
if (projectName) {
switch (getTextStyle()) {
case 'uppercase':
projectName = projectName.toUpperCase();
break;
case 'lowercase':
projectName = projectName.toLowerCase();
break;
}

switch (getSource()) {
statusBarItem.text = getTemplate().replace('${project-name}', projectName);
statusBarItem.show();
} else {
statusBarItem.text = '';
statusBarItem.hide();
}
});
}

function getProjectName(callback: UpdateStatusBarItemCallback) {
const source: string = getSource();
const command: string = getCommand();
switch (source) {
case 'none':
break;
case 'folderName':
projectName = getProjectNameByFolder();
callback(getProjectNameByFolder());
break;
case 'commandOutput':
if (command) {
getProjectNameByCommand(command, callback);
}
break;
}

if (projectName) {
switch (getTextStyle()) {
case 'uppercase':
projectName = projectName.toUpperCase();
break;
case 'lowercase':
projectName = projectName.toLowerCase();
break;
}

statusBarItem.text = getTemplate().replace('${project-name}', projectName);
statusBarItem.show();
} else {
statusBarItem.text = '';
statusBarItem.hide();
}
}

Expand All @@ -114,6 +130,25 @@ export function activate(context: vscode.ExtensionContext) {
}
}
}

function getProjectNameByCommand(command: string, callback: (projectName: string) => void) {
let workspaceFolder = (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0].uri.path);
let cwd = (workspaceFolder || '');

exec(command, {cwd: cwd}, (error, stdout, stderr) => {
let projectName = '';
if (error) {
console.log(`Command failed with error: ${error.message}`);
}
else if (stderr) {
console.log(`Command failed with stderr: ${stderr}`);
}
else {
projectName = stdout.split('\n')[0].trim();
}
callback(projectName);
});
}
}


Expand Down