diff --git a/README.md b/README.md index 9110d58..8dbd1d3 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Source tmLanguage file: https://github.com/textmate/gettext.tmbundle/blob/master ## Features +* Show the number of total messages, untranslated messages, and fuzzy messages in the status bar via `msgfmt --statistics`. * `vscgettext.moveToNextUntranslated`: Move focus to next untranslated message (`alt+n`) * `vscgettext.moveToPreviousUntranslated`: Move focus to previous untranslated message (`alt+shift+n`) * `vscgettext.moveToNextFuzzy`: Move focus to next fuzzy message (`alt+f`) diff --git a/src/status.ts b/src/status.ts new file mode 100644 index 0000000..ed81a9a --- /dev/null +++ b/src/status.ts @@ -0,0 +1,61 @@ +import * as vscode from "vscode"; +import { exec } from "child_process"; + +let statusBarItem: vscode.StatusBarItem; + +export async function activateStatusBar({ + subscriptions, +}: vscode.ExtensionContext): Promise { + statusBarItem = vscode.window.createStatusBarItem( + vscode.StatusBarAlignment.Left, + 0 + ); + + statusBarItem.show(); + subscriptions.push(statusBarItem); + + subscriptions.push( + vscode.window.onDidChangeActiveTextEditor(updateStsatusBarItem) + ); + + updateStsatusBarItem(); +} + +async function updateStsatusBarItem(): Promise { + const output = await runMsgfmtStatistics(); + + try { + if (output) { + statusBarItem.text = output; + + statusBarItem.show(); + } else { + statusBarItem.hide(); + } + } catch (error) { + console.error(error); + + statusBarItem.hide(); + } +} + +async function runMsgfmtStatistics(): Promise { + const path = vscode.window.activeTextEditor?.document.uri.fsPath; + + if (!path) { + return null; + } + + const command = `msgfmt --statistics -o /dev/null ${path}`; + + return new Promise((resolve, reject) => { + exec(command, (error, stdout, stderr) => { + if (error) { + reject(new Error(`Failed to run msgfmt: ${error.message}`)); + } else { + // Correct. The command will print the statistics to stderr. + resolve(stderr); + } + }); + }); +} diff --git a/src/vscgettext.ts b/src/vscgettext.ts index 3343f89..4f16e8a 100644 --- a/src/vscgettext.ts +++ b/src/vscgettext.ts @@ -12,8 +12,9 @@ import { moveToPreviousUntranslatedOrFuzzyMessage, } from "./lib"; import provideDefinition from "./provide_definition"; +import { activateStatusBar } from "./status"; -export function activate(context: vscode.ExtensionContext) { +export async function activate(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.commands.registerTextEditorCommand( "vscgettext.moveToNextUntranslated", @@ -53,6 +54,8 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.languages.registerDefinitionProvider("po", { provideDefinition }) ); + + activateStatusBar(context); } export function deactivate() {