2017-10-15 18:57:09 +02:00
|
|
|
const BaseWidget = require('tkwidgets/BaseWidget.js');
|
|
|
|
const chalk = require('chalk');
|
2017-10-17 19:18:31 +02:00
|
|
|
const termutils = require('tkwidgets/framework/termutils.js');
|
2022-11-13 16:25:06 +02:00
|
|
|
const stripAnsi = require('strip-ansi');
|
2017-12-14 05:13:43 +02:00
|
|
|
const { handleAutocompletion } = require('../autocompletion.js');
|
2017-10-15 18:57:09 +02:00
|
|
|
|
2022-11-13 14:30:47 +02:00
|
|
|
export default class StatusBarWidget extends BaseWidget {
|
2023-03-06 16:22:01 +02:00
|
|
|
public constructor() {
|
2017-10-15 18:57:09 +02:00
|
|
|
super();
|
|
|
|
|
|
|
|
this.promptState_ = null;
|
|
|
|
this.inputEventEmitter_ = null;
|
|
|
|
this.history_ = [];
|
|
|
|
this.items_ = [];
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public get name() {
|
2017-10-15 18:57:09 +02:00
|
|
|
return 'statusBar';
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public get canHaveFocus() {
|
2017-10-17 19:18:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public setItemAt(index: number, text: string) {
|
2017-10-24 20:03:12 +02:00
|
|
|
this.items_[index] = stripAnsi(text).trim();
|
2017-10-15 18:57:09 +02:00
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-03-06 16:22:01 +02:00
|
|
|
public async prompt(initialText = '', promptString: any = null, options: any = null) {
|
2017-10-15 18:57:09 +02:00
|
|
|
if (this.promptState_) throw new Error('Another prompt already active');
|
2017-10-24 21:40:15 +02:00
|
|
|
if (promptString === null) promptString = ':';
|
|
|
|
if (options === null) options = {};
|
2017-10-15 18:57:09 +02:00
|
|
|
|
|
|
|
this.root.globalDisableKeyboard(this);
|
|
|
|
|
|
|
|
this.promptState_ = {
|
|
|
|
promise: null,
|
2022-11-13 14:30:47 +02:00
|
|
|
initialText: (initialText),
|
2017-10-24 20:03:12 +02:00
|
|
|
promptString: stripAnsi(promptString),
|
2017-10-15 18:57:09 +02:00
|
|
|
};
|
|
|
|
|
2017-10-24 21:40:15 +02:00
|
|
|
if ('cursorPosition' in options) this.promptState_.cursorPosition = options.cursorPosition;
|
2017-12-12 20:17:30 +02:00
|
|
|
if ('secure' in options) this.promptState_.secure = options.secure;
|
2017-10-24 21:40:15 +02:00
|
|
|
|
2017-10-15 18:57:09 +02:00
|
|
|
this.promptState_.promise = new Promise((resolve, reject) => {
|
|
|
|
this.promptState_.resolve = resolve;
|
|
|
|
this.promptState_.reject = reject;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.invalidate();
|
|
|
|
|
|
|
|
return this.promptState_.promise;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public get promptActive() {
|
2017-10-15 18:57:09 +02:00
|
|
|
return !!this.promptState_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public get history() {
|
2017-10-15 18:57:09 +02:00
|
|
|
return this.history_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public resetCursor() {
|
2017-10-19 00:13:53 +02:00
|
|
|
if (!this.promptActive) return;
|
|
|
|
if (!this.inputEventEmitter_) return;
|
|
|
|
|
|
|
|
this.inputEventEmitter_.redraw();
|
|
|
|
this.inputEventEmitter_.rebase(this.absoluteInnerX + termutils.textLength(this.promptState_.promptString), this.absoluteInnerY);
|
|
|
|
this.term.moveTo(this.absoluteInnerX + termutils.textLength(this.promptState_.promptString) + this.inputEventEmitter_.getInput().length, this.absoluteInnerY);
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public render() {
|
2017-10-15 18:57:09 +02:00
|
|
|
super.render();
|
2017-10-19 00:13:53 +02:00
|
|
|
|
|
|
|
const doSaveCursor = !this.promptActive;
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2017-10-19 00:13:53 +02:00
|
|
|
if (doSaveCursor) this.term.saveCursor();
|
|
|
|
|
2017-10-15 18:57:09 +02:00
|
|
|
this.innerClear();
|
|
|
|
|
2017-10-25 19:23:45 +02:00
|
|
|
// Note:
|
|
|
|
// On Ubuntu, bgBlueBright looks too bright which makes the white text illegible
|
|
|
|
// On Windows, bgBlueBright is fine and looks dark enough (Windows is probably in the wrong though)
|
|
|
|
// For now, just don't use any colour at all.
|
|
|
|
|
2019-10-09 21:35:13 +02:00
|
|
|
// const textStyle = this.promptActive ? (s) => s : chalk.bgBlueBright.white;
|
|
|
|
// const textStyle = (s) => s;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2022-11-13 16:25:06 +02:00
|
|
|
const textStyle = this.promptActive ? (s: any) => s : chalk.gray;
|
2017-10-15 18:57:09 +02:00
|
|
|
|
|
|
|
this.term.drawHLine(this.absoluteInnerX, this.absoluteInnerY, this.innerWidth, textStyle(' '));
|
|
|
|
|
|
|
|
this.term.moveTo(this.absoluteInnerX, this.absoluteInnerY);
|
|
|
|
|
|
|
|
if (this.promptActive) {
|
|
|
|
this.term.write(textStyle(this.promptState_.promptString));
|
|
|
|
|
2017-10-15 19:20:54 +02:00
|
|
|
if (this.inputEventEmitter_) {
|
2017-10-19 00:13:53 +02:00
|
|
|
// inputField is already waiting for input so in that case just make
|
|
|
|
// sure that the cursor is at the right position and exit.
|
|
|
|
this.resetCursor();
|
2017-10-15 19:20:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-17 19:18:31 +02:00
|
|
|
this.term.showCursor(true);
|
|
|
|
|
2017-12-28 21:51:24 +02:00
|
|
|
const isSecurePrompt = !!this.promptState_.secure;
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2022-11-13 14:30:47 +02:00
|
|
|
const options: any = {
|
2017-10-15 18:57:09 +02:00
|
|
|
cancelable: true,
|
|
|
|
history: this.history,
|
|
|
|
default: this.promptState_.initialText,
|
2017-12-14 15:01:00 +02:00
|
|
|
autoComplete: handleAutocompletion,
|
2019-07-30 09:35:42 +02:00
|
|
|
autoCompleteHint: true,
|
|
|
|
autoCompleteMenu: true,
|
2017-10-15 18:57:09 +02:00
|
|
|
};
|
|
|
|
|
2017-10-24 21:40:15 +02:00
|
|
|
if ('cursorPosition' in this.promptState_) options.cursorPosition = this.promptState_.cursorPosition;
|
2017-12-28 21:51:24 +02:00
|
|
|
if (isSecurePrompt) options.echoChar = true;
|
2017-10-24 21:40:15 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2022-11-13 16:25:06 +02:00
|
|
|
this.inputEventEmitter_ = this.term.inputField(options, (error: any, input: any) => {
|
2017-10-15 18:57:09 +02:00
|
|
|
let resolveResult = null;
|
|
|
|
const resolveFn = this.promptState_.resolve;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
this.logger().error('StatusBar: inputField error:', error);
|
|
|
|
} else {
|
|
|
|
if (input === undefined) {
|
|
|
|
// User cancel
|
|
|
|
} else {
|
2017-10-24 23:09:22 +02:00
|
|
|
resolveResult = input ? input.trim() : input;
|
|
|
|
// Add the command to history but only if it's longer than one character.
|
|
|
|
// Below that it's usually an answer like "y"/"n", etc.
|
2018-02-26 21:25:54 +02:00
|
|
|
const isConfigPassword = input.indexOf('config ') >= 0 && input.indexOf('password') >= 0;
|
|
|
|
if (!isSecurePrompt && input && input.length > 1 && !isConfigPassword) this.history_.push(input);
|
2017-10-15 18:57:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 19:18:31 +02:00
|
|
|
// If the inputField spans several lines invalidate the root so that
|
|
|
|
// the interface is relayouted.
|
|
|
|
if (termutils.textLength(this.promptState_.promptString) + termutils.textLength(input) >= this.innerWidth - 5) {
|
|
|
|
this.root.invalidate();
|
|
|
|
}
|
|
|
|
|
2017-10-15 18:57:09 +02:00
|
|
|
this.inputEventEmitter_ = null;
|
|
|
|
this.term.showCursor(false);
|
|
|
|
this.promptState_ = null;
|
|
|
|
this.root.globalEnableKeyboard(this);
|
|
|
|
this.invalidate();
|
|
|
|
|
|
|
|
// Only callback once everything has been cleaned up and reset
|
|
|
|
resolveFn(resolveResult);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
for (let i = 0; i < this.items_.length; i++) {
|
2017-10-24 20:03:12 +02:00
|
|
|
const s = this.items_[i].substr(0, this.innerWidth - 1);
|
|
|
|
this.term.write(textStyle(s));
|
2017-10-15 18:57:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-19 00:13:53 +02:00
|
|
|
|
|
|
|
if (doSaveCursor) this.term.restoreCursor();
|
2017-10-15 18:57:09 +02:00
|
|
|
}
|
|
|
|
}
|