Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit 901d364

Browse files
committed
Improve prompt
- Add newline to make multi-line statement formatting independent of database name - Substitute an absent database name for the default default name
1 parent 91cb686 commit 901d364

File tree

6 files changed

+39
-12
lines changed

6 files changed

+39
-12
lines changed

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellMultiDatabaseIntegrationTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
import static org.hamcrest.MatcherAssert.assertThat;
1919
import static org.junit.Assume.assumeTrue;
2020
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
21+
import static org.neo4j.shell.DatabaseManager.DEFAULT_DEFAULT_DB_NAME;
22+
import static org.neo4j.shell.DatabaseManager.SYSTEM_DB_NAME;
2123
import static org.neo4j.shell.Versions.majorVersion;
2224

2325
public class CypherShellMultiDatabaseIntegrationTest
2426
{
25-
private static final String SYSTEM_DB_NAME = "system";
26-
private static final String DEFAULT_DB_NAME = "neo4j";
27-
2827
@Rule
2928
public final ExpectedException thrown = ExpectedException.none();
3029

@@ -62,7 +61,7 @@ public void switchingToSystemDatabaseWorks() throws CommandException {
6261
@Test
6362
public void switchingToSystemDatabaseAndBackToDefaultWorks() throws CommandException {
6463
useCommand.execute(SYSTEM_DB_NAME);
65-
useCommand.execute(DEFAULT_DB_NAME);
64+
useCommand.execute(DEFAULT_DEFAULT_DB_NAME);
6665

6766
assertThat(linePrinter.output(), is(""));
6867
}

cypher-shell/src/main/java/org/neo4j/shell/DatabaseManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
*/
88
public interface DatabaseManager
99
{
10+
String DEFAULT_DEFAULT_DB_NAME = "neo4j";
11+
String SYSTEM_DB_NAME = "system";
12+
1013
void setActiveDatabase(String databaseName) throws CommandException;
1114

1215
String getActiveDatabase();

cypher-shell/src/main/java/org/neo4j/shell/cli/InteractiveShellRunner.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@
2525
import java.util.List;
2626
import java.util.concurrent.atomic.AtomicBoolean;
2727

28+
import static org.neo4j.driver.internal.messaging.request.MultiDatabaseUtil.ABSENT_DB_NAME;
29+
import static org.neo4j.shell.DatabaseManager.DEFAULT_DEFAULT_DB_NAME;
30+
2831
/**
2932
* A shell runner intended for interactive sessions where lines are input one by one and execution should happen
3033
* along the way.
3134
*/
3235
public class InteractiveShellRunner implements ShellRunner, SignalHandler {
3336
static final String INTERRUPT_SIGNAL = "INT";
3437
private final static String freshPrompt = "> ";
35-
private final static AnsiFormattedText continuationPrompt = AnsiFormattedText.s().bold().append(" ");
38+
private final static AnsiFormattedText continuationPrompt = AnsiFormattedText.s().bold().append(" ");
3639
private final static String transactionPrompt = "# ";
3740
// Need to know if we are currently executing when catch Ctrl-C, needs to be atomic due to
3841
// being called from different thread
@@ -155,10 +158,21 @@ AnsiFormattedText getPrompt() {
155158
return continuationPrompt;
156159
}
157160

161+
String databaseName = databaseManager.getActiveDatabase();
162+
163+
// Substitute empty name for the default default-database-name
164+
// For now we just use a hard-coded default name
165+
// Ideally we would like to receive the actual name in the ResultSummary when we connect (in BoltStateHandler.reconnect())
166+
// (If the user is an admin we could also query for the default database config value with:
167+
// "CALL dbms.listConfig() YIELD name, value WHERE name = "dbms.default_database" RETURN value"
168+
// but that does not work in general)
169+
databaseName = ABSENT_DB_NAME.equals(databaseName) ? DEFAULT_DEFAULT_DB_NAME : databaseName;
170+
158171
AnsiFormattedText prompt = AnsiFormattedText.s().bold()
159172
.append(connectionConfig.username())
160173
.append("@")
161-
.append(databaseManager.getActiveDatabase())
174+
.append(databaseName)
175+
.appendNewLine()
162176
.append(txHandler.isTransactionOpen()? transactionPrompt : freshPrompt);
163177
return prompt;
164178
}

cypher-shell/src/main/java/org/neo4j/shell/log/AnsiFormattedText.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ public AnsiFormattedText append(String s) {
132132
return this;
133133
}
134134

135+
/**
136+
* Append a new line
137+
* @return this
138+
*/
139+
public AnsiFormattedText appendNewLine() {
140+
pieces.add(new AnsiFormattedString(color, attributes, System.lineSeparator()));
141+
return this;
142+
}
143+
135144
/**
136145
* Set formatting to bold. Note that this has no effect on strings already in the text.
137146
*

cypher-shell/src/main/java/org/neo4j/shell/state/BoltStateHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ private void reconnect(boolean keepBookmark) {
151151

152152
StatementResult run = session.run("RETURN 1");
153153
this.version = run.summary().server().version();
154+
// It would be nice if we could also get the actual database name here, in the case where we used ABSENT_DB_NAME
154155
run.consume();
155156
}
156157

cypher-shell/src/test/java/org/neo4j/shell/cli/InteractiveShellRunnerTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.List;
3535
import java.util.concurrent.atomic.AtomicReference;
3636

37+
import static java.lang.String.format;
3738
import static org.hamcrest.CoreMatchers.is;
3839
import static org.hamcrest.MatcherAssert.assertThat;
3940
import static org.junit.Assert.assertEquals;
@@ -269,21 +270,21 @@ public void testPrompt() throws Exception {
269270
AnsiFormattedText prompt = runner.getPrompt();
270271

271272
// then
272-
assertEquals("myusername@mydb> ", prompt.plainString());
273+
assertEquals( format("myusername@mydb%n> "), prompt.plainString());
273274

274275
// when
275276
statementParser.parseMoreText(" \t \n "); // whitespace
276277
prompt = runner.getPrompt();
277278

278279
// then
279-
assertEquals("myusername@mydb> ", prompt.plainString());
280+
assertEquals( format("myusername@mydb%n> "), prompt.plainString());
280281

281282
// when
282283
statementParser.parseMoreText("bla bla"); // non whitespace
283284
prompt = runner.getPrompt();
284285

285286
// then
286-
assertEquals(" ", prompt.plainString());
287+
assertEquals(" ", prompt.plainString());
287288
}
288289

289290
@Test
@@ -298,21 +299,21 @@ public void testPromptInTx() throws Exception {
298299
AnsiFormattedText prompt = runner.getPrompt();
299300

300301
// then
301-
assertEquals("myusername@mydb# ", prompt.plainString());
302+
assertEquals(format("myusername@mydb%n# "), prompt.plainString());
302303

303304
// when
304305
statementParser.parseMoreText(" \t \n "); // whitespace
305306
prompt = runner.getPrompt();
306307

307308
// then
308-
assertEquals("myusername@mydb# ", prompt.plainString());
309+
assertEquals(format("myusername@mydb%n# "), prompt.plainString());
309310

310311
// when
311312
statementParser.parseMoreText("bla bla"); // non whitespace
312313
prompt = runner.getPrompt();
313314

314315
// then
315-
assertEquals(" ", prompt.plainString());
316+
assertEquals(" ", prompt.plainString());
316317
}
317318

318319
@Test

0 commit comments

Comments
 (0)