From 40632935ee920516d1ad2fe51fac177818ed6b0f Mon Sep 17 00:00:00 2001 From: Gordon Date: Thu, 25 Mar 2021 01:42:39 +0000 Subject: [PATCH] Add status bar functionality --- ext-src/extension.ts | 104 ++++++++++++++++++++++++++++--------------- package.json | 5 +-- 2 files changed, 70 insertions(+), 39 deletions(-) diff --git a/ext-src/extension.ts b/ext-src/extension.ts index db98b2c..8c172f2 100644 --- a/ext-src/extension.ts +++ b/ext-src/extension.ts @@ -1,15 +1,13 @@ -import * as kanbn from '@basementuniverse/kanbn/src/main'; import * as vscode from 'vscode'; import ReactPanel from './ReactPanel'; let statusBarItem: vscode.StatusBarItem; -export function activate(context: vscode.ExtensionContext) { +export async function activate(context: vscode.ExtensionContext) { // Register a command to initialise kanbn in the current workspace. This command will be invoked when the status // bar item is clicked in a workspace where kanbn isn't already initialised. - const initialiseCommandId = 'kanbn.init'; - context.subscriptions.push(vscode.commands.registerCommand(initialiseCommandId, async () => { + context.subscriptions.push(vscode.commands.registerCommand('kanbn.init', async () => { // If no workspace folder is opened, we can't initialise kanbn if (vscode.workspace.workspaceFolders === undefined) { @@ -17,49 +15,75 @@ export function activate(context: vscode.ExtensionContext) { return; } - // Otherwise, check if kanbn is already initialised in the current workspace - console.log(kanbn.getMainFolder()); - // Prints /home/gordon/.kanbn because presumably vscode runs extension code with process.cwd() as /home/ - // TODO update kanbn to inject root (see const ROOT in kanbn/src/main.js) into every method that needs it... + // Set the node process directory and import kanbn + process.chdir(vscode.workspace.workspaceFolders[0].uri.fsPath); + const kanbn = await import('@basementuniverse/kanbn/src/main'); - // if (vscode.workspace.workspaceFolders !== undefined) { - // const name = await vscode.window.showInputBox({ - // value: '', - // placeHolder: 'The project name.', - // validateInput: text => { - // return text.length < 1 ? 'The project name cannot be empty.' : null; - // } - // }); - // if (name !== undefined) { - // vscode.window.showInformationMessage(`creating with ${name}`); - // } - // } - // TODO initialise kanbn board - // updateStatusBarItem(); - // console.log(kanbn); - // console.log(`kanbn initialised: ${await kanbn.initialised()}`); + // If kanbn is already initialised, get the project name + let projectName = ''; + if (await kanbn.initialised()) { + projectName = (await kanbn.getIndex()).name; + } + + // Prompt for a new project name + const newProjectName = await vscode.window.showInputBox({ + value: projectName, + placeHolder: 'The project name.', + validateInput: text => { + return text.length < 1 ? 'The project name cannot be empty.' : null; + } + }); + + // If the input prompt wasn't cancelled, initialise kanbn + if (newProjectName !== undefined) { + await kanbn.initialise({ + name: newProjectName + }); + vscode.window.showInformationMessage(`Initialised kanbn project '${newProjectName}'.`); + updateStatusBarItem(kanbn); + } })); // Register a command to open the kanbn board. This command will be invoked when the status bar item is clicked // in a workspace where kanbn has already been initialised. - const openBoardCommandId = 'kanbn.open'; - context.subscriptions.push(vscode.commands.registerCommand(openBoardCommandId, () => { - ReactPanel.createOrShow(context.extensionPath); + context.subscriptions.push(vscode.commands.registerCommand('kanbn.board', async () => { + + // If no workspace folder is opened, we can't open the kanbn board + if (vscode.workspace.workspaceFolders === undefined) { + vscode.window.showErrorMessage('You need to open a workspace before viewing the kanbn board.'); + return; + } + + // Set the node process directory and import kanbn + process.chdir(vscode.workspace.workspaceFolders[0].uri.fsPath); + const kanbn = await import('@basementuniverse/kanbn/src/main'); + + // If kanbn is initialised, view the kanbn board + if (await kanbn.initialised()) { + ReactPanel.createOrShow(context.extensionPath); + } else { + vscode.window.showErrorMessage('You need to initialise kanbn before viewing the kanbn board.'); + } })); // If a workspace folder is open, add a status bar item and start watching for file changes if (vscode.workspace.workspaceFolders !== undefined) { - statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0); - statusBarItem.command = openBoardCommandId; - context.subscriptions.push(statusBarItem); - updateStatusBarItem(); + // Set the node process directory and import kanbn + process.chdir(vscode.workspace.workspaceFolders[0].uri.fsPath); + const kanbn = await import('@basementuniverse/kanbn/src/main'); + + // Create status bar item + statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 0); + context.subscriptions.push(statusBarItem); + updateStatusBarItem(kanbn); + + // Initialise file watcher const uri = vscode.workspace.workspaceFolders[0].uri.fsPath; const fileWatcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(uri, '.kanbn/*')); - fileWatcher.onDidChange(e => { + fileWatcher.onDidChange(() => { + updateStatusBarItem(kanbn); // TODO update kanbn board - updateStatusBarItem(); - console.log(e); }); } } @@ -68,10 +92,18 @@ export function deactivate(): void { // } -function updateStatusBarItem(): void { +async function updateStatusBarItem(kanbn: typeof import('@basementuniverse/kanbn/src/main')): Promise { if (statusBarItem === undefined) { return; } - statusBarItem.text = `$(project) Not initialised`; + const initialised = await kanbn.initialised(); + const text = initialised ? await getStatusBarText(kanbn) : 'Not initialised'; + statusBarItem.text = `$(project) ${text}`; + statusBarItem.command = initialised ? 'kanbn.board' : 'kanbn.init'; statusBarItem.show(); } + +async function getStatusBarText(kanbn: typeof import('@basementuniverse/kanbn/src/main')): Promise { + const status = await kanbn.status(true); + return Object.values(status.columnTasks).join(' / '); +} diff --git a/package.json b/package.json index 11bd840..370eeef 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,7 @@ }, "publisher": "basementuniverse", "activationEvents": [ - "onStartupFinished", - "onCommand:kanbn.open" + "onStartupFinished" ], "main": "./build/ext-src/extension.js", "contributes": { @@ -18,7 +17,7 @@ "category": "Kanbn" }, { - "command": "kanbn.open", + "command": "kanbn.board", "title": "Open board", "category": "Kanbn" }