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

Show the output of msgfmt --statistics in the status bar #33

Merged
merged 7 commits into from
Feb 23, 2024
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
61 changes: 61 additions & 0 deletions src/status.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left,
0
);

statusBarItem.show();
subscriptions.push(statusBarItem);

subscriptions.push(
vscode.window.onDidChangeActiveTextEditor(updateStsatusBarItem)
);

updateStsatusBarItem();
}

async function updateStsatusBarItem(): Promise<void> {
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<string | null> {
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);
}
});
});
}
5 changes: 4 additions & 1 deletion src/vscgettext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -53,6 +54,8 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.languages.registerDefinitionProvider("po", { provideDefinition })
);

activateStatusBar(context);
}

export function deactivate() {
Expand Down
Loading