From ce546bafe5d8a008ad5bfa2ac9eb0db997c04750 Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Tue, 13 Feb 2024 11:06:46 +0900 Subject: [PATCH 1/7] Show the output of `msgfmt --statistics` in the status bar --- src/status.ts | 39 +++++++++++++++++++++++++++++++++++++++ src/vscgettext.ts | 5 ++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/status.ts diff --git a/src/status.ts b/src/status.ts new file mode 100644 index 0000000..deefa76 --- /dev/null +++ b/src/status.ts @@ -0,0 +1,39 @@ +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(updateStsatusBarmItem) + ); + + updateStsatusBarmItem(); +} + +async function updateStsatusBarmItem(): Promise { + statusBarItem.text = await runMsgfmtStatistics(); +} + +async function runMsgfmtStatistics(): Promise { + const path = vscode.window.activeTextEditor?.document.uri.fsPath; + + const command = `msgfmt --statistics -o /dev/null ${path}`; + + return new Promise((resolve, reject) => { + exec(command, (error, stdout, stderr) => { + // 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() { From a6acbbea9516d87a19949ed8b1ded06907edd57a Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Tue, 13 Feb 2024 11:14:38 +0900 Subject: [PATCH 2/7] Hide on error --- src/status.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/status.ts b/src/status.ts index deefa76..63ab5fc 100644 --- a/src/status.ts +++ b/src/status.ts @@ -22,12 +22,23 @@ export async function activateStatusBar({ } async function updateStsatusBarmItem(): Promise { - statusBarItem.text = await runMsgfmtStatistics(); + const output = await runMsgfmtStatistics(); + + if (output) { + statusBarItem.text = output; + statusBarItem.show(); + } else { + statusBarItem.hide(); + } } -async function runMsgfmtStatistics(): Promise { +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) => { From 8e016a980f8df447c74617ac4c60c9fa22d763e3 Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Tue, 13 Feb 2024 23:31:31 +0900 Subject: [PATCH 3/7] Add a line in README --- README.md | 1 + 1 file changed, 1 insertion(+) 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`) From 0d1344d0f52fbf4f3bc42521923de078948c697c Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Wed, 14 Feb 2024 09:19:23 +0900 Subject: [PATCH 4/7] Fix typo --- src/status.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/status.ts b/src/status.ts index 63ab5fc..2a14d14 100644 --- a/src/status.ts +++ b/src/status.ts @@ -15,13 +15,13 @@ export async function activateStatusBar({ subscriptions.push(statusBarItem); subscriptions.push( - vscode.window.onDidChangeActiveTextEditor(updateStsatusBarmItem) + vscode.window.onDidChangeActiveTextEditor(updateStsatusBarItem) ); - updateStsatusBarmItem(); + updateStsatusBarItem(); } -async function updateStsatusBarmItem(): Promise { +async function updateStsatusBarItem(): Promise { const output = await runMsgfmtStatistics(); if (output) { From 108ad6044d303fb78bd0fcbfe782fe4b0b04d267 Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Wed, 14 Feb 2024 09:21:44 +0900 Subject: [PATCH 5/7] Logging --- src/status.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/status.ts b/src/status.ts index 2a14d14..e3d9551 100644 --- a/src/status.ts +++ b/src/status.ts @@ -43,6 +43,10 @@ async function runMsgfmtStatistics(): Promise { return new Promise((resolve, reject) => { exec(command, (error, stdout, stderr) => { + console.log("error: ", error); + console.log("stdout: ", stdout); + console.log("stderr: ", stderr); + // Correct. The command will print the statistics to stderr. resolve(stderr); }); From 3ca3cfe846e18a647d89c8fe9ca77af3b2b0ba7c Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Thu, 15 Feb 2024 21:01:05 +0900 Subject: [PATCH 6/7] Handle an error --- src/status.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/status.ts b/src/status.ts index e3d9551..eb19e10 100644 --- a/src/status.ts +++ b/src/status.ts @@ -43,12 +43,14 @@ async function runMsgfmtStatistics(): Promise { return new Promise((resolve, reject) => { exec(command, (error, stdout, stderr) => { - console.log("error: ", error); - console.log("stdout: ", stdout); - console.log("stderr: ", stderr); - - // Correct. The command will print the statistics to stderr. - resolve(stderr); + if (error) { + console.error(stderr); + + reject(error); + } else { + // Correct. The command will print the statistics to stderr. + resolve(stderr); + } }); }); } From 8147255de83fdbe422af77f5ab6dc4b312306804 Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Thu, 15 Feb 2024 21:04:27 +0900 Subject: [PATCH 7/7] With exception --- src/status.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/status.ts b/src/status.ts index eb19e10..ed81a9a 100644 --- a/src/status.ts +++ b/src/status.ts @@ -24,10 +24,17 @@ export async function activateStatusBar({ async function updateStsatusBarItem(): Promise { const output = await runMsgfmtStatistics(); - if (output) { - statusBarItem.text = output; - statusBarItem.show(); - } else { + try { + if (output) { + statusBarItem.text = output; + + statusBarItem.show(); + } else { + statusBarItem.hide(); + } + } catch (error) { + console.error(error); + statusBarItem.hide(); } } @@ -44,9 +51,7 @@ async function runMsgfmtStatistics(): Promise { return new Promise((resolve, reject) => { exec(command, (error, stdout, stderr) => { if (error) { - console.error(stderr); - - reject(error); + reject(new Error(`Failed to run msgfmt: ${error.message}`)); } else { // Correct. The command will print the statistics to stderr. resolve(stderr);