vscode-texinfo/src/indicator.ts

111 lines
3.6 KiB
TypeScript
Raw Normal View History

/**
* indicator.ts
2021-03-16 12:01:13 +00:00
*
* Copyright (C) 2020,2021 CismonX <admin@cismon.net>
*
* This file is part of vscode-texinfo.
*
* vscode-texinfo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* vscode-texinfo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* vscode-texinfo. If not, see <https://www.gnu.org/licenses/>.
*/
import * as vscode from 'vscode';
2021-04-19 12:43:20 +00:00
import GlobalContext from './global_context';
import { exec } from './utils/misc';
2021-03-15 12:43:38 +00:00
/**
* Shows whether GNU Texinfo is properly installed and configured.
*/
2021-08-11 17:23:04 +00:00
export default class Indicator implements vscode.Disposable
{
2021-04-19 12:43:20 +00:00
get canDisplayPreview() {
return this._canDisplayPreview;
}
2021-04-19 12:43:20 +00:00
dispose() {
2021-05-25 04:16:56 +00:00
this._statusBarItem.dispose();
2021-03-15 12:43:38 +00:00
}
2021-08-11 17:23:04 +00:00
constructor(private readonly _globalContext: GlobalContext) {
_globalContext.subscribe(
vscode.commands.registerCommand(
'texinfo.indicator.click',
this._click.bind(this),
),
vscode.window.onDidChangeActiveTextEditor(
2021-08-11 17:23:04 +00:00
this._refresh.bind(this),
),
2021-04-19 12:43:20 +00:00
);
this._updateStatus()
.then(() => this._refresh(vscode.window.activeTextEditor));
2021-03-15 12:43:38 +00:00
}
2021-04-19 12:43:20 +00:00
private _canDisplayPreview = false;
2021-03-15 12:43:38 +00:00
private readonly _statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Right, 100);
2021-04-22 09:07:13 +00:00
/**
* Calls when the status bar item is clicked.
*/
2021-05-25 04:16:56 +00:00
private async _click() {
await this._updateStatus();
this._refresh(vscode.window.activeTextEditor);
}
2021-04-22 09:07:13 +00:00
/**
* Refresh the show/hide status of the indicator based on given editor.
*
* @param editor
*/
2021-05-25 04:16:56 +00:00
private _refresh(editor?: vscode.TextEditor) {
2021-04-19 12:43:20 +00:00
if (editor?.document.languageId === 'texinfo') {
2021-05-25 04:16:56 +00:00
this._statusBarItem.show();
2021-04-19 12:43:20 +00:00
} else {
2021-05-25 04:16:56 +00:00
this._statusBarItem.hide();
}
}
2021-04-22 09:07:13 +00:00
/**
* Update the installation status of GNU Texinfo,
* by checking `makeinfo --version`.
2021-04-22 09:07:13 +00:00
*/
2021-05-25 04:16:56 +00:00
private async _updateStatus() {
2021-08-11 17:23:04 +00:00
const options = this._globalContext.options;
const output
= await exec(options.makeinfo, ['--version'], options.maxSize);
const result = output.data?.match(/\(GNU texinfo\) (.*)\n/);
let tooltip = '', icon: string, version = '';
if (result && result[1]) {
version = result[1];
2021-10-02 15:15:57 +00:00
if (!isNaN(+version) && +version < 6.8) {
icon = '$(warning)';
tooltip = `GNU Texinfo (${options.makeinfo}) ` +
2021-10-02 15:15:57 +00:00
`is outdated (${version} < 6.8).`;
} else {
2021-04-19 12:43:20 +00:00
// Unrecognizable version. Assume it is okay.
icon = '$(check)';
}
2021-04-19 12:43:20 +00:00
this._canDisplayPreview = true;
} else {
icon = '$(close)';
tooltip = `GNU Texinfo (${options.makeinfo}) ` +
`is not correctly installed or configured.`;
2021-04-19 12:43:20 +00:00
this._canDisplayPreview = false;
}
2021-05-25 04:16:56 +00:00
this._statusBarItem.command = 'texinfo.indicator.click';
this._statusBarItem.text = `${icon} GNU Texinfo ${version}`;
this._statusBarItem.tooltip = tooltip;
}
}