@Test public void testPrompt() throws Exception { InputStream inputStream = new ByteArrayInputStream("".getBytes()); InteractiveShellRunner runner = new InteractiveShellRunner(cmdExecuter, txHandler, databaseManager, logger, statementParser, inputStream, historyFile, userMessagesHandler, connectionConfig); when(txHandler.isTransactionOpen()).thenReturn(false); AnsiFormattedText prompt = runner.updateAndGetPrompt(); String wantedPrompt = "myusername@mydb> "; assertEquals(wantedPrompt, prompt.plainString()); statementParser.parseMoreText(" \t \n "); prompt = runner.updateAndGetPrompt(); assertEquals(wantedPrompt, prompt.plainString()); statementParser.parseMoreText("bla bla"); prompt = runner.updateAndGetPrompt(); assertEquals(OutputFormatter.repeat(' ', wantedPrompt.length()), prompt.plainString()); }
AnsiFormattedText updateAndGetPrompt() { if (statementParser.containsText()) { return continuationPrompt; } String databaseName = databaseManager.getActualDatabaseAsReportedByServer(); if (databaseName == null || ABSENT_DB_NAME.equals(databaseName)) { String dbNameSetByUser = databaseManager.getActiveDatabaseAsSetByUser(); databaseName = ABSENT_DB_NAME.equals(dbNameSetByUser)? UNRESOLVED_DEFAULT_DB_PROPMPT_TEXT : dbNameSetByUser; } String errorSuffix = getErrorPrompt(executer.lastNeo4jErrorCode()); int promptIndent = connectionConfig.username().length() + USERNAME_DB_DELIMITER.length() + databaseName.length() + errorSuffix.length() + FRESH_PROMPT.length(); AnsiFormattedText prePrompt = AnsiFormattedText.s().bold() .append(connectionConfig.username()) .append("@") .append(databaseName); if (!errorSuffix.isEmpty()) { prePrompt.colorRed().append(errorSuffix).colorDefault(); } if (promptIndent <= ONELINE_PROMPT_MAX_LENGTH) { continuationPrompt = AnsiFormattedText.s().bold().append(OutputFormatter.repeat(' ', promptIndent)); return prePrompt .append( txHandler.isTransactionOpen() ? TRANSACTION_PROMPT : FRESH_PROMPT ); } else { continuationPrompt = AnsiFormattedText.s().bold(); return prePrompt .appendNewLine() .append( txHandler.isTransactionOpen() ? TRANSACTION_PROMPT : FRESH_PROMPT ); } }
InteractiveShellRunner implements ShellRunner, SignalHandler { AnsiFormattedText updateAndGetPrompt() { if (statementParser.containsText()) { return continuationPrompt; } String databaseName = databaseManager.getActualDatabaseAsReportedByServer(); if (databaseName == null || ABSENT_DB_NAME.equals(databaseName)) { String dbNameSetByUser = databaseManager.getActiveDatabaseAsSetByUser(); databaseName = ABSENT_DB_NAME.equals(dbNameSetByUser)? UNRESOLVED_DEFAULT_DB_PROPMPT_TEXT : dbNameSetByUser; } String errorSuffix = getErrorPrompt(executer.lastNeo4jErrorCode()); int promptIndent = connectionConfig.username().length() + USERNAME_DB_DELIMITER.length() + databaseName.length() + errorSuffix.length() + FRESH_PROMPT.length(); AnsiFormattedText prePrompt = AnsiFormattedText.s().bold() .append(connectionConfig.username()) .append("@") .append(databaseName); if (!errorSuffix.isEmpty()) { prePrompt.colorRed().append(errorSuffix).colorDefault(); } if (promptIndent <= ONELINE_PROMPT_MAX_LENGTH) { continuationPrompt = AnsiFormattedText.s().bold().append(OutputFormatter.repeat(' ', promptIndent)); return prePrompt .append( txHandler.isTransactionOpen() ? TRANSACTION_PROMPT : FRESH_PROMPT ); } else { continuationPrompt = AnsiFormattedText.s().bold(); return prePrompt .appendNewLine() .append( txHandler.isTransactionOpen() ? TRANSACTION_PROMPT : FRESH_PROMPT ); } } }
InteractiveShellRunner implements ShellRunner, SignalHandler { AnsiFormattedText updateAndGetPrompt() { if (statementParser.containsText()) { return continuationPrompt; } String databaseName = databaseManager.getActualDatabaseAsReportedByServer(); if (databaseName == null || ABSENT_DB_NAME.equals(databaseName)) { String dbNameSetByUser = databaseManager.getActiveDatabaseAsSetByUser(); databaseName = ABSENT_DB_NAME.equals(dbNameSetByUser)? UNRESOLVED_DEFAULT_DB_PROPMPT_TEXT : dbNameSetByUser; } String errorSuffix = getErrorPrompt(executer.lastNeo4jErrorCode()); int promptIndent = connectionConfig.username().length() + USERNAME_DB_DELIMITER.length() + databaseName.length() + errorSuffix.length() + FRESH_PROMPT.length(); AnsiFormattedText prePrompt = AnsiFormattedText.s().bold() .append(connectionConfig.username()) .append("@") .append(databaseName); if (!errorSuffix.isEmpty()) { prePrompt.colorRed().append(errorSuffix).colorDefault(); } if (promptIndent <= ONELINE_PROMPT_MAX_LENGTH) { continuationPrompt = AnsiFormattedText.s().bold().append(OutputFormatter.repeat(' ', promptIndent)); return prePrompt .append( txHandler.isTransactionOpen() ? TRANSACTION_PROMPT : FRESH_PROMPT ); } else { continuationPrompt = AnsiFormattedText.s().bold(); return prePrompt .appendNewLine() .append( txHandler.isTransactionOpen() ? TRANSACTION_PROMPT : FRESH_PROMPT ); } } InteractiveShellRunner(@Nonnull StatementExecuter executer,
                                  @Nonnull TransactionHandler txHandler,
                                  @Nonnull DatabaseManager databaseManager,
                                  @Nonnull Logger logger,
                                  @Nonnull StatementParser statementParser,
                                  @Nonnull InputStream inputStream,
                                  @Nonnull File historyFile,
                                  @Nonnull UserMessagesHandler userMessagesHandler,
                                  @Nonnull ConnectionConfig connectionConfig); }
InteractiveShellRunner implements ShellRunner, SignalHandler { AnsiFormattedText updateAndGetPrompt() { if (statementParser.containsText()) { return continuationPrompt; } String databaseName = databaseManager.getActualDatabaseAsReportedByServer(); if (databaseName == null || ABSENT_DB_NAME.equals(databaseName)) { String dbNameSetByUser = databaseManager.getActiveDatabaseAsSetByUser(); databaseName = ABSENT_DB_NAME.equals(dbNameSetByUser)? UNRESOLVED_DEFAULT_DB_PROPMPT_TEXT : dbNameSetByUser; } String errorSuffix = getErrorPrompt(executer.lastNeo4jErrorCode()); int promptIndent = connectionConfig.username().length() + USERNAME_DB_DELIMITER.length() + databaseName.length() + errorSuffix.length() + FRESH_PROMPT.length(); AnsiFormattedText prePrompt = AnsiFormattedText.s().bold() .append(connectionConfig.username()) .append("@") .append(databaseName); if (!errorSuffix.isEmpty()) { prePrompt.colorRed().append(errorSuffix).colorDefault(); } if (promptIndent <= ONELINE_PROMPT_MAX_LENGTH) { continuationPrompt = AnsiFormattedText.s().bold().append(OutputFormatter.repeat(' ', promptIndent)); return prePrompt .append( txHandler.isTransactionOpen() ? TRANSACTION_PROMPT : FRESH_PROMPT ); } else { continuationPrompt = AnsiFormattedText.s().bold(); return prePrompt .appendNewLine() .append( txHandler.isTransactionOpen() ? TRANSACTION_PROMPT : FRESH_PROMPT ); } } InteractiveShellRunner(@Nonnull StatementExecuter executer,
                                  @Nonnull TransactionHandler txHandler,
                                  @Nonnull DatabaseManager databaseManager,
                                  @Nonnull Logger logger,
                                  @Nonnull StatementParser statementParser,
                                  @Nonnull InputStream inputStream,
                                  @Nonnull File historyFile,
                                  @Nonnull UserMessagesHandler userMessagesHandler,
                                  @Nonnull ConnectionConfig connectionConfig); @Override int runUntilEnd(); @Nonnull @Override Historian getHistorian(); @Nonnull List<String> readUntilStatement(); @Override void handle(final Signal signal); }
InteractiveShellRunner implements ShellRunner, SignalHandler { AnsiFormattedText updateAndGetPrompt() { if (statementParser.containsText()) { return continuationPrompt; } String databaseName = databaseManager.getActualDatabaseAsReportedByServer(); if (databaseName == null || ABSENT_DB_NAME.equals(databaseName)) { String dbNameSetByUser = databaseManager.getActiveDatabaseAsSetByUser(); databaseName = ABSENT_DB_NAME.equals(dbNameSetByUser)? UNRESOLVED_DEFAULT_DB_PROPMPT_TEXT : dbNameSetByUser; } String errorSuffix = getErrorPrompt(executer.lastNeo4jErrorCode()); int promptIndent = connectionConfig.username().length() + USERNAME_DB_DELIMITER.length() + databaseName.length() + errorSuffix.length() + FRESH_PROMPT.length(); AnsiFormattedText prePrompt = AnsiFormattedText.s().bold() .append(connectionConfig.username()) .append("@") .append(databaseName); if (!errorSuffix.isEmpty()) { prePrompt.colorRed().append(errorSuffix).colorDefault(); } if (promptIndent <= ONELINE_PROMPT_MAX_LENGTH) { continuationPrompt = AnsiFormattedText.s().bold().append(OutputFormatter.repeat(' ', promptIndent)); return prePrompt .append( txHandler.isTransactionOpen() ? TRANSACTION_PROMPT : FRESH_PROMPT ); } else { continuationPrompt = AnsiFormattedText.s().bold(); return prePrompt .appendNewLine() .append( txHandler.isTransactionOpen() ? TRANSACTION_PROMPT : FRESH_PROMPT ); } } InteractiveShellRunner(@Nonnull StatementExecuter executer,
                                  @Nonnull TransactionHandler txHandler,
                                  @Nonnull DatabaseManager databaseManager,
                                  @Nonnull Logger logger,
                                  @Nonnull StatementParser statementParser,
                                  @Nonnull InputStream inputStream,
                                  @Nonnull File historyFile,
                                  @Nonnull UserMessagesHandler userMessagesHandler,
                                  @Nonnull ConnectionConfig connectionConfig); @Override int runUntilEnd(); @Nonnull @Override Historian getHistorian(); @Nonnull List<String> readUntilStatement(); @Override void handle(final Signal signal);  }