Skip to content

Commit 89b0e3a

Browse files
authored
feat(shell-api): include db name in prompt MONGOSH-832 (#964)
1 parent 890d578 commit 89b0e3a

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

packages/cli-repl/src/mongosh-repl.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ describe('MongoshNodeRepl', () => {
400400
expect((mongoshRepl.runtimeState().repl as any)._prompt).to.equal('');
401401
input.write('\u0003'); // Ctrl+C for abort
402402
await tick();
403-
expect((mongoshRepl.runtimeState().repl as any)._prompt).to.equal('> ');
404-
expect(stripAnsi(output)).to.equal('db.foo\r\nbar\r\n\r\n> ');
403+
expect((mongoshRepl.runtimeState().repl as any)._prompt).to.equal('test> ');
404+
expect(stripAnsi(output)).to.equal('db.foo\r\nbar\r\n\r\ntest> ');
405405
});
406406
it('does not autocomplete tab-indented code', async() => {
407407
output = '';
@@ -655,7 +655,7 @@ describe('MongoshNodeRepl', () => {
655655
output = '';
656656
input.write('throw new Error("yellow")\n');
657657
await waitEval(bus);
658-
expect(stripAnsi(output)).to.match(/Error: yellow\n(> )+$/);
658+
expect(stripAnsi(output)).to.match(/Error: yellow\n(test> )+$/);
659659

660660
input.write('config.set("showStackTraces", true)\n');
661661
await waitEval(bus);
@@ -679,7 +679,7 @@ describe('MongoshNodeRepl', () => {
679679
output = '';
680680
outputStream.emit('resize');
681681
await tick();
682-
expect(stripAnsi(output)).to.equal('> ');
682+
expect(stripAnsi(output)).to.equal('test> ');
683683
});
684684

685685
it('does not refresh the prompt if a window resize occurs while evaluating', async() => {
@@ -701,7 +701,7 @@ describe('MongoshNodeRepl', () => {
701701
output = '';
702702
resolveInProgress();
703703
await tick();
704-
expect(stripAnsi(output)).to.equal('\n> ');
704+
expect(stripAnsi(output)).to.equal('\ntest> ');
705705
});
706706

707707
context('thrown non-Errors', () => {
@@ -1018,7 +1018,7 @@ describe('MongoshNodeRepl', () => {
10181018

10191019
const initialized = await mongoshRepl.initialize(serviceProvider);
10201020
await mongoshRepl.startRepl(initialized);
1021-
expect(output).to.contain('Enterprise > ');
1021+
expect(output).to.contain('Enterprise test> ');
10221022
});
10231023

10241024
it('defaults if an error occurs', async() => {
@@ -1057,11 +1057,11 @@ describe('MongoshNodeRepl', () => {
10571057

10581058
const initialized = await mongoshRepl.initialize(serviceProvider);
10591059
await mongoshRepl.startRepl(initialized);
1060-
expect(output).to.contain('Enterprise > ');
1060+
expect(output).to.contain('Enterprise test> ');
10611061

10621062
input.write('db = Mongo("foo").getDB("bar")\n');
10631063
await waitEval(bus);
1064-
expect(output).to.contain('Atlas Data Lake > ');
1064+
expect(output).to.contain('AtlasDataLake bar> ');
10651065
});
10661066

10671067
context('user-provided prompt', () => {

packages/shell-api/src/shell-internal-state.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('ShellInternalState', () => {
8989

9090
await internalState.fetchConnectionInfo();
9191
const prompt = await internalState.getDefaultPrompt();
92-
expect(prompt).to.equal('Atlas Data Lake > ');
92+
expect(prompt).to.equal('AtlasDataLake test> ');
9393
});
9494

9595
it('wins against enterprise', async() => {
@@ -103,7 +103,7 @@ describe('ShellInternalState', () => {
103103

104104
await internalState.fetchConnectionInfo();
105105
const prompt = await internalState.getDefaultPrompt();
106-
expect(prompt).to.equal('Atlas Data Lake > ');
106+
expect(prompt).to.equal('AtlasDataLake test> ');
107107
});
108108
});
109109

@@ -113,7 +113,7 @@ describe('ShellInternalState', () => {
113113

114114
await internalState.fetchConnectionInfo();
115115
const prompt = await internalState.getDefaultPrompt();
116-
expect(prompt).to.equal('Enterprise > ');
116+
expect(prompt).to.equal('Enterprise test> ');
117117
});
118118

119119
it('inferred from buildInfo modules', async() => {
@@ -124,7 +124,7 @@ describe('ShellInternalState', () => {
124124

125125
await internalState.fetchConnectionInfo();
126126
const prompt = await internalState.getDefaultPrompt();
127-
expect(prompt).to.equal('Enterprise > ');
127+
expect(prompt).to.equal('Enterprise test> ');
128128
});
129129
});
130130

@@ -156,7 +156,7 @@ describe('ShellInternalState', () => {
156156
setupServiceProviderWithTopology(topology);
157157

158158
const prompt = await internalState.getDefaultPrompt();
159-
expect(prompt).to.equal(`configset [direct: ${p}]> `);
159+
expect(prompt).to.equal(`configset [direct: ${p}] test> `);
160160
});
161161
});
162162

@@ -186,7 +186,7 @@ describe('ShellInternalState', () => {
186186
setupServiceProviderWithTopology(topology);
187187

188188
const prompt = await internalState.getDefaultPrompt();
189-
expect(prompt).to.equal('> ');
189+
expect(prompt).to.equal('test> ');
190190
});
191191
});
192192
});
@@ -203,7 +203,7 @@ describe('ShellInternalState', () => {
203203
setupServiceProviderWithTopology(topology);
204204

205205
const prompt = await internalState.getDefaultPrompt();
206-
expect(prompt).to.equal('leSet [secondary]> ');
206+
expect(prompt).to.equal('leSet [secondary] test> ');
207207
});
208208

209209
it('shows the setName and primary hint for ReplicaSetWithPrimary', async() => {
@@ -217,7 +217,7 @@ describe('ShellInternalState', () => {
217217
setupServiceProviderWithTopology(topology);
218218

219219
const prompt = await internalState.getDefaultPrompt();
220-
expect(prompt).to.equal('leSet [primary]> ');
220+
expect(prompt).to.equal('leSet [primary] test> ');
221221
});
222222
});
223223

@@ -232,7 +232,7 @@ describe('ShellInternalState', () => {
232232
setupServiceProviderWithTopology(topology);
233233

234234
const prompt = await internalState.getDefaultPrompt();
235-
expect(prompt).to.equal('[mongos]> ');
235+
expect(prompt).to.equal('[mongos] test> ');
236236
});
237237
it('shows mongos and a setName', async() => {
238238
const topology = {
@@ -245,7 +245,7 @@ describe('ShellInternalState', () => {
245245
setupServiceProviderWithTopology(topology);
246246

247247
const prompt = await internalState.getDefaultPrompt();
248-
expect(prompt).to.equal('leSet [mongos]> ');
248+
expect(prompt).to.equal('leSet [mongos] test> ');
249249
});
250250
});
251251

@@ -266,7 +266,7 @@ describe('ShellInternalState', () => {
266266

267267
await internalState.fetchConnectionInfo();
268268
const prompt = await internalState.getDefaultPrompt();
269-
expect(prompt).to.equal('> ');
269+
expect(prompt).to.equal('test> ');
270270
});
271271
});
272272

@@ -291,7 +291,7 @@ describe('ShellInternalState', () => {
291291
setupServiceProviderWithTopology(topology);
292292

293293
const prompt = await internalState.getDefaultPrompt();
294-
expect(prompt).to.equal('> ');
294+
expect(prompt).to.equal('test> ');
295295
});
296296
});
297297
});

packages/shell-api/src/shell-internal-state.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,21 @@ export default class ShellInternalState {
390390
}
391391

392392
async getDefaultPrompt(): Promise<string> {
393-
return `${this.getDefaultPromptPrefix()}${this.getTopologySpecificPrompt()}> `;
393+
const prefix = this.getDefaultPromptPrefix();
394+
const topologyInfo = this.getTopologySpecificPrompt();
395+
let dbname = '';
396+
try {
397+
dbname = this.currentDb.getName();
398+
} catch { /* nodb */ }
399+
return `${[prefix, topologyInfo, dbname].filter(Boolean).join(' ')}> `;
394400
}
395401

396402
private getDefaultPromptPrefix(): string {
397403
const extraConnectionInfo = this.connectionInfo?.extraInfo;
398404
if (extraConnectionInfo?.is_data_lake) {
399-
return 'Atlas Data Lake ';
405+
return 'AtlasDataLake';
400406
} else if (extraConnectionInfo?.is_enterprise || this.connectionInfo?.buildInfo?.modules?.indexOf('enterprise') >= 0) {
401-
return 'Enterprise ';
407+
return 'Enterprise';
402408
}
403409
return '';
404410
}

0 commit comments

Comments
 (0)