Skip to content
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
2 changes: 1 addition & 1 deletion packages/cli-repl/src/arg-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CliOptions } from '@mongosh/service-provider-server';
import { USAGE } from './constants';
import i18n from '@mongosh/i18n';
import minimist from 'minimist';
import clr from './clr';
import { colorizeForStderr as clr } from './clr';

/**
* Unknown translation key.
Expand Down
45 changes: 33 additions & 12 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class CliRepl {
*/
async connect(driverUri: string, driverOptions: NodeOptions): Promise<any> {
if (!this.options.nodb) {
console.log(i18n.__(CONNECTING), ' ', clr(retractPassword(driverUri), ['bold', 'green']));
console.log(i18n.__(CONNECTING), ' ', this.clr(retractPassword(driverUri), ['bold', 'green']));
}
return await CliServiceProvider.connect(driverUri, driverOptions, this.options);
}
Expand All @@ -116,9 +116,9 @@ class CliRepl {
prompt: '> ',
writer: this.writer,
completer: completer.bind(null, version),
terminal: true,
breakEvalOnSigint: true,
preview: false,
terminal: process.env.MONGOSH_FORCE_TERMINAL ? true : undefined
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice and simple!

});

const originalDisplayPrompt = this.repl.displayPrompt.bind(this.repl);
Expand All @@ -128,12 +128,14 @@ class CliRepl {
this.lineByLineInput.nextLine();
};

const originalEditorAction = this.repl.commands.editor.action.bind(this.repl);
if (this.repl.commands.editor) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const originalEditorAction = this.repl.commands.editor.action.bind(this.repl);

this.repl.commands.editor.action = (): any => {
this.lineByLineInput.disableBlockOnNewline();
return originalEditorAction();
};
this.repl.commands.editor.action = (): any => {
this.lineByLineInput.disableBlockOnNewline();
return originalEditorAction();
};
}

this.repl.defineCommand('clear', {
help: '',
Expand Down Expand Up @@ -355,10 +357,10 @@ class CliRepl {
stack: result.stack
};
this.bus.emit('mongosh:error', output);
return formatOutput({ type: 'Error', value: output });
return this.formatOutput({ type: 'Error', value: output });
}

return formatOutput({ type: result.type, value: result.printable });
return this.formatOutput({ type: result.type, value: result.printable });
};

verifyNodeVersion(): void {
Expand All @@ -377,7 +379,7 @@ class CliRepl {
greet(): void {
const { version } = require('../package.json');
console.log(`Using MongoDB: ${this.internalState.connectionInfo.buildInfo.version}`);
console.log(`${clr('Using Mongosh Beta', ['bold', 'yellow'])}: ${version}`);
console.log(`${this.clr('Using Mongosh Beta', ['bold', 'yellow'])}: ${version}`);
console.log(`${MONGOSH_WIKI}`);
if (!this.disableGreetingMessage) console.log(TELEMETRY);
}
Expand Down Expand Up @@ -410,7 +412,7 @@ class CliRepl {
read(readOptions, (error, password) => {
if (error) {
this.bus.emit('mongosh:error', error);
return console.log(formatError(error));
return console.log(this.formatError(error));
}

driverOptions.auth.password = password;
Expand Down Expand Up @@ -465,7 +467,7 @@ class CliRepl {
this.bus.emit('mongosh:error', error);
}

console.error(formatError(error));
console.error(this.formatError(error));
return process.exit(1);
}

Expand All @@ -475,6 +477,25 @@ class CliRepl {
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/48646
(this.repl as any).output.write(joined + '\n');
}

formatOutput(value: any): string {
return formatOutput(value, this.getFormatOptions());
}

formatError(value: any): string {
return formatError(value, this.getFormatOptions());
}

clr(text: string, style: string|string[]): string {
return clr(text, style, this.getFormatOptions());
}

getFormatOptions(): { colors: boolean } {
return {
colors: this.repl ? this.repl.useColors :
process.stdout.isTTY && process.stdout.getColorDepth() > 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice stuff!

};
}
}

export default CliRepl;
17 changes: 15 additions & 2 deletions packages/cli-repl/src/clr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import ansi from 'ansi-escape-sequences';

export default function clr(text: string, style: any): string {
return process.stdout.isTTY ? ansi.format(text, style) : text;
export default function colorize(text: string, style: string|string[], options: { colors: boolean }): string {
if (options.colors) {
return ansi.format(text, style);
}
return text;
}

export function colorizeForStdout(text: string, style: string|string[]): string {
return colorize(text, style, {
colors: process.stdout.isTTY && process.stdout.getColorDepth() > 1 });
}

export function colorizeForStderr(text: string, style: string|string[]): string {
return colorize(text, style, {
colors: process.stderr.isTTY && process.stderr.getColorDepth() > 1 });
}
2 changes: 1 addition & 1 deletion packages/cli-repl/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import i18n from '@mongosh/i18n';
import clr from './clr';
import { colorizeForStderr as clr } from './clr';

export const TELEMETRY = `
${i18n.__('cli-repl.cli-repl.telemetry')}
Expand Down
Loading