Java Code Examples for jline.console.ConsoleReader#readLine()

The following examples show how to use jline.console.ConsoleReader#readLine() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PasswordPrompt.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public char[] getPassword() throws IOException {
    char[] password = new char[0];

    final ConsoleReader reader = getReader();
    reader.setPrompt("Password: ");
    String passwordStr = reader.readLine('*');
    password = passwordStr.toCharArray();

    // Reading the password into memory as a String is less safe than a char[]
    // because the String is immutable. We can't clear it out. At best, we can
    // remove all references to it and suggest the GC clean it up. There are no
    // guarantees though.
    passwordStr = null;
    System.gc();

    return password;
}
 
Example 2
Source File: Cli.java    From incubator-iotdb with Apache License 2.0 6 votes vote down vote up
private static void receiveCommands(ConsoleReader reader) throws TException, IOException {
  try (IoTDBConnection connection = (IoTDBConnection) DriverManager
      .getConnection(Config.IOTDB_URL_PREFIX + host + ":" + port + "/", username, password)) {
    String s;
    properties = connection.getServerProperties();
    AGGREGRATE_TIME_LIST.addAll(properties.getSupportedTimeAggregationOperations());
    TIMESTAMP_PRECISION = properties.getTimestampPrecision();

    echoStarting();
    displayLogo(properties.getVersion());
    println(IOTDB_CLI_PREFIX + "> login successfully");
    while (true) {
      s = reader.readLine(IOTDB_CLI_PREFIX + "> ", null);
      boolean continues = processCommand(s, connection);
      if (!continues) {
        break;
      }
    }
  } catch (SQLException e) {
    println(String
        .format("%s> %s Host is %s, port is %s.", IOTDB_CLI_PREFIX, e.getMessage(), host,
            port));
  }
}
 
Example 3
Source File: Util.java    From ecosys with Apache License 2.0 6 votes vote down vote up
/**
 * Create a prompt with color and customized prompt text.
 * @param text, the prompt text
 * @return input string
 */
public static String ColorPrompt(String text) {
  String ANSI_BLUE = "\u001B[1;34m";
  String ANSI_RESET = "\u001B[0m";
  String input = null;
  try {
    ConsoleReader tempConsole = new ConsoleReader();
    String prmpt = ANSI_BLUE + text + ANSI_RESET;
    tempConsole.setPrompt(prmpt);
    input = tempConsole.readLine();
  } catch (IOException e) {
    LogExceptions(e);
    System.out.println("Prompt input error!");
  }
  return input;
}
 
Example 4
Source File: CloudConfig.java    From CloudNet with Apache License 2.0 6 votes vote down vote up
private void defaultInitDoc(ConsoleReader consoleReader) throws Exception {
    if (Files.exists(servicePath)) {
        return;
    }

    String hostName = NetworkUtils.getHostName();
    if (hostName.equals("127.0.0.1") || hostName.equalsIgnoreCase("127.0.1.1") || hostName.split("\\.").length != 4) {
        String input;
        System.out.println("Please write the first Wrapper IP address:");
        while ((input = consoleReader.readLine()) != null) {
            if ((input.equals("127.0.0.1") || input.equalsIgnoreCase("127.0.1.1") || input.split("\\.").length != 4)) {
                System.out.println("Please write the real ip address :)");
                continue;
            }

            hostName = input;
            break;
        }
    }
    new Document("wrapper", Collections.singletonList(new WrapperMeta("Wrapper-1", hostName, "admin"))).append("proxyGroups",
                                                                                                               Collections.singletonList(
                                                                                                                   new BungeeGroup()))
                                                                                                       .saveAsConfig(servicePath);

    new Document("group", new LobbyGroup()).saveAsConfig(Paths.get("groups/Lobby.json"));
}
 
Example 5
Source File: Util.java    From ecosys with Apache License 2.0 6 votes vote down vote up
/**
 * Create a prompt with color and customized prompt text.
 * @param text, the prompt text
 * @return input string
 */
public static String ColorPrompt(String text) {
  String ANSI_BLUE = "\u001B[1;34m";
  String ANSI_RESET = "\u001B[0m";
  String input = null;
  try {
    ConsoleReader tempConsole = new ConsoleReader();
    String prmpt = ANSI_BLUE + text + ANSI_RESET;
    tempConsole.setPrompt(prmpt);
    input = tempConsole.readLine();
  } catch (IOException e) {
    LogExceptions(e);
    System.out.println("Prompt input error!");
  }
  return input;
}
 
Example 6
Source File: Util.java    From ecosys with Apache License 2.0 6 votes vote down vote up
/**
 * Create a prompt with color and customized prompt text.
 * @param text, the prompt text
 * @return input string
 */
public static String ColorPrompt(String text) {
  String ANSI_BLUE = "\u001B[1;34m";
  String ANSI_RESET = "\u001B[0m";
  String input = null;
  try {
    ConsoleReader tempConsole = new ConsoleReader();
    String prmpt = ANSI_BLUE + text + ANSI_RESET;
    tempConsole.setPrompt(prmpt);
    input = tempConsole.readLine();
  } catch (IOException e) {
    LogExceptions(e);
    System.out.println("Prompt input error!");
  }
  return input;
}
 
Example 7
Source File: JLinePrompt.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Prompts a user for a boolean value. The prompt will be repeated until
 * a value true/false string has been submitted. If a default value is
 * provided, then it will be used if the user doens't enter anything.
 *
 * @param prompt - The prompt for the input. (not null)
 * @param defaultValue - The default value for the input if one is provided. (not null)
 * @return The value the user entered.
 * @throws IOException There was a problem reading values from the user.
 */
protected boolean promptBoolean(final String prompt, final Optional<Boolean> defaultValue) throws IOException {
    requireNonNull(prompt);
    requireNonNull(defaultValue);

    final ConsoleReader reader = getReader();
    reader.setPrompt(prompt);

    Boolean value = null;
    boolean prompting = true;

    while(prompting) {
        // An empty input means to use the default value.
        final String input = reader.readLine();
        if(input.isEmpty() && defaultValue.isPresent()) {
            value = defaultValue.get();
            prompting = false;
        }

        // Check if it is one of the affirmative answers.
        if(isAffirmative(input)) {
            value = true;
            prompting = false;
        }

        // Check if it is one of the negative answers.
        if(isNegative(input)) {
            value = false;
            prompting = false;
        }

        // If we are still prompting, the input was invalid.
        if(prompting) {
            reader.println("Invalid response (true/false)");
        }
    }

    return value;
}
 
Example 8
Source File: Util.java    From ecosys with Apache License 2.0 5 votes vote down vote up
/**
 * function to generate a prompt for user to input username
 * @param doubleCheck, notes whether the password should be confirmed one more time.
 * @param isNew, indicates whether it is inputting a new password
 * @return SHA-1 hashed password on success, null on error
 */
public static String Prompt4Password(boolean doubleCheck, boolean isNew, String username) {
  String ANSI_BLUE = "\u001B[1;34m";
  String ANSI_RESET = "\u001B[0m";
  String pass = null;
  try {
    ConsoleReader tempConsole = new ConsoleReader();
    String prompttext = isNew ? "New Password : " : "Password for " + username + " : ";
    String prompt = ANSI_BLUE + prompttext + ANSI_RESET;
    tempConsole.setPrompt(prompt);
    tempConsole.setExpandEvents(false);
    String pass1 = tempConsole.readLine(new Character('*'));
    if (doubleCheck) {
      String pass2 = pass1;
      prompt = ANSI_BLUE + "Re-enter Password : " + ANSI_RESET;
      tempConsole.setPrompt(prompt);
      pass2 = tempConsole.readLine(new Character('*'));
      if (!pass1.equals(pass2)) {
        System.out.println("The two passwords do not match.");
        return null;
      }
    }
    // need to hash the password so that we do not store it as plain text
    pass = pass1;
  } catch (Exception e) {
    LogExceptions(e);
    System.out.println("Error while inputting password.");
  }
  return pass;
}
 
Example 9
Source File: Util.java    From ecosys with Apache License 2.0 5 votes vote down vote up
/**
 * function to generate a prompt for user to input username
 * @param doubleCheck, notes whether the password should be confirmed one more time.
 * @param isNew, indicates whether it is inputting a new password
 * @return SHA-1 hashed password on success, null on error
 */
public static String Prompt4Password(boolean doubleCheck, boolean isNew, String username) {
  String ANSI_BLUE = "\u001B[1;34m";
  String ANSI_RESET = "\u001B[0m";
  String pass = null;
  try {
    ConsoleReader tempConsole = new ConsoleReader();
    String prompttext = isNew ? "New Password : " : "Password for " + username + " : ";
    String prompt = ANSI_BLUE + prompttext + ANSI_RESET;
    tempConsole.setPrompt(prompt);
    tempConsole.setExpandEvents(false);
    String pass1 = tempConsole.readLine(new Character('*'));
    if (doubleCheck) {
      String pass2 = pass1;
      prompt = ANSI_BLUE + "Re-enter Password : " + ANSI_RESET;
      tempConsole.setPrompt(prompt);
      pass2 = tempConsole.readLine(new Character('*'));
      if (!pass1.equals(pass2)) {
        System.out.println("The two passwords do not match.");
        return null;
      }
    }
    // need to hash the password so that we do not store it as plain text
    pass = pass1;
  } catch (Exception e) {
    System.out.println("Error while inputting password.");
  }
  return pass;
}
 
Example 10
Source File: Util.java    From ecosys with Apache License 2.0 5 votes vote down vote up
/**
 * Create a prompt with color and customized prompt text.
 * @param text, the prompt text
 * @return input string
 */
public static String ColorPrompt(String text) {
  String ANSI_BLUE = "\u001B[1;34m";
  String ANSI_RESET = "\u001B[0m";
  String input = null;
  try {
    ConsoleReader tempConsole = new ConsoleReader();
    String prmpt = ANSI_BLUE + text + ANSI_RESET;
    tempConsole.setPrompt(prmpt);
    input = tempConsole.readLine();
  } catch (IOException e) {
    System.out.println("Prompt input error!");
  }
  return input;
}
 
Example 11
Source File: Util.java    From ecosys with Apache License 2.0 5 votes vote down vote up
/**
 * function to generate a prompt for user to input username
 * @param doubleCheck, notes whether the password should be confirmed one more time.
 * @param isNew, indicates whether it is inputting a new password
 * @return SHA-1 hashed password on success, null on error
 */
public static String Prompt4Password(boolean doubleCheck, boolean isNew, String username) {
  String ANSI_BLUE = "\u001B[1;34m";
  String ANSI_RESET = "\u001B[0m";
  String pass = null;
  try {
    ConsoleReader tempConsole = new ConsoleReader();
    String prompttext = isNew ? "New Password : " : "Password for " + username + " : ";
    String prompt = ANSI_BLUE + prompttext + ANSI_RESET;
    tempConsole.setPrompt(prompt);
    tempConsole.setExpandEvents(false);
    String pass1 = tempConsole.readLine(new Character('*'));
    if (doubleCheck) {
      String pass2 = pass1;
      prompt = ANSI_BLUE + "Re-enter Password : " + ANSI_RESET;
      tempConsole.setPrompt(prompt);
      pass2 = tempConsole.readLine(new Character('*'));
      if (!pass1.equals(pass2)) {
        System.out.println("The two passwords do not match.");
        return null;
      }
    }
    // need to hash the password so that we do not store it as plain text
    pass = pass1;
  } catch (Exception e) {
    LogExceptions(e);
    System.out.println("Error while inputting password.");
  }
  return pass;
}
 
Example 12
Source File: Util.java    From ecosys with Apache License 2.0 5 votes vote down vote up
/**
 * function to generate a prompt for user to input username
 * @param doubleCheck, notes whether the password should be confirmed one more time.
 * @param isNew, indicates whether it is inputting a new password
 * @return SHA-1 hashed password on success, null on error
 */
public static String Prompt4Password(boolean doubleCheck, boolean isNew, String username) {
  String ANSI_BLUE = "\u001B[1;34m";
  String ANSI_RESET = "\u001B[0m";
  String pass = null;
  try {
    ConsoleReader tempConsole = new ConsoleReader();
    String prompttext = isNew ? "New Password : " : "Password for " + username + " : ";
    String prompt = ANSI_BLUE + prompttext + ANSI_RESET;
    tempConsole.setPrompt(prompt);
    tempConsole.setExpandEvents(false);
    String pass1 = tempConsole.readLine(new Character('*'));
    if (doubleCheck) {
      String pass2 = pass1;
      prompt = ANSI_BLUE + "Re-enter Password : " + ANSI_RESET;
      tempConsole.setPrompt(prompt);
      pass2 = tempConsole.readLine(new Character('*'));
      if (!pass1.equals(pass2)) {
        System.out.println("The two passwords do not match.");
        return null;
      }
    }
    // need to hash the password so that we do not store it as plain text
    pass = pass1;
  } catch (Exception e) {
    LogExceptions(e);
    System.out.println("Error while inputting password.");
  }
  return pass;
}
 
Example 13
Source File: SparqlPrompt.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<String> getSparql() throws IOException {
    final ConsoleReader reader = getReader();
    reader.setCopyPasteDetection(true); // disable tab completion from activating
    reader.setHistoryEnabled(false);    // don't store SPARQL fragments in the command history
    try {
        reader.println("Enter a SPARQL Query.");
        reader.println("Type '" + EXECUTE_COMMAND + "' to execute the current query.");
        reader.println("Type '" + CLEAR_COMMAND + "' to clear the current query.");
        reader.flush();

        final StringBuilder sb = new StringBuilder();
        String line = reader.readLine("SPARQL> ");
        while (!line.endsWith(CLEAR_COMMAND) && !line.endsWith(EXECUTE_COMMAND)) {
            sb.append(line).append("\n");
            line = reader.readLine("     -> ");
        }

        if (line.endsWith(EXECUTE_COMMAND)) {
            sb.append(line.substring(0, line.length() - EXECUTE_COMMAND.length()));
            return Optional.of(sb.toString());
        }
        return Optional.absent();
    } finally {
        reader.setHistoryEnabled(true);      // restore the ConsoleReader's settings
        reader.setCopyPasteDetection(false); // restore tab completion
    }
}
 
Example 14
Source File: ApexCli.java    From Bats with Apache License 2.0 5 votes vote down vote up
private String readLine(ConsoleReader reader)
  throws IOException
{
  if (forcePrompt == null) {
    prompt = "";
    if (consolePresent) {
      if (changingLogicalPlan) {
        prompt = "logical-plan-change";
      } else {
        prompt = "apex";
      }
      if (currentApp != null) {
        prompt += " (";
        prompt += currentApp.getApplicationId().toString();
        prompt += ") ";
      }
      prompt += "> ";
    }
  } else {
    prompt = forcePrompt;
  }
  String line = reader.readLine(prompt, consolePresent ? null : (char)0);
  if (line == null) {
    return null;
  }
  return ltrim(line);
}
 
Example 15
Source File: Util.java    From ecosys with Apache License 2.0 5 votes vote down vote up
/**
 * function to generate a prompt for user to input username
 * @param doubleCheck, notes whether the password should be confirmed one more time.
 * @param isNew, indicates whether it is inputting a new password
 * @return SHA-1 hashed password on success, null on error
 */
public static String Prompt4Password(boolean doubleCheck, boolean isNew, String username) {
  String ANSI_BLUE = "\u001B[1;34m";
  String ANSI_RESET = "\u001B[0m";
  String pass = null;
  try {
    ConsoleReader tempConsole = new ConsoleReader();
    String prompttext = isNew ? "New Password : " : "Password for " + username + " : ";
    String prompt = ANSI_BLUE + prompttext + ANSI_RESET;
    tempConsole.setPrompt(prompt);
    tempConsole.setExpandEvents(false);
    String pass1 = tempConsole.readLine(new Character('*'));
    if (doubleCheck) {
      String pass2 = pass1;
      prompt = ANSI_BLUE + "Re-enter Password : " + ANSI_RESET;
      tempConsole.setPrompt(prompt);
      pass2 = tempConsole.readLine(new Character('*'));
      if (!pass1.equals(pass2)) {
        System.out.println("The two passwords do not match.");
        return null;
      }
    }
    // need to hash the password so that we do not store it as plain text
    pass = pass1;
  } catch (Exception e) {
    LogExceptions(e);
    System.out.println("Error while inputting password.");
  }
  return pass;
}
 
Example 16
Source File: ApexCli.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
private String readLine(ConsoleReader reader)
  throws IOException
{
  if (forcePrompt == null) {
    prompt = "";
    if (consolePresent) {
      if (changingLogicalPlan) {
        prompt = "logical-plan-change";
      } else {
        prompt = "apex";
      }
      if (currentApp != null) {
        prompt += " (";
        prompt += currentApp.getApplicationId().toString();
        prompt += ") ";
      }
      prompt += "> ";
    }
  } else {
    prompt = forcePrompt;
  }
  String line = reader.readLine(prompt, consolePresent ? null : (char)0);
  if (line == null) {
    return null;
  }
  return ltrim(line);
}
 
Example 17
Source File: JLinePrompt.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Prompts a user for a String value. The prompt will be repeated until a
 * value has been submitted. If a default value is provided, then it will be
 * used if the user doesn't enter anything.
 *
 * @param prompt - The prompt for the input. (not null)
 * @param defaultValue - The default value for the input if one is provided. (not null)
 * @return The value the user entered.
 * @throws IOException There was a problem reading values from the user.
 */
protected String promptString(final String prompt, final Optional<String> defaultValue) throws IOException {
    requireNonNull(prompt);
    requireNonNull(defaultValue);

    final ConsoleReader reader = getReader();
    reader.setPrompt(prompt);

    String value = null;
    boolean prompting = true;

    while(prompting) {
        // Read a line of input.
        final String input = reader.readLine();

        if(!input.isEmpty()) {
            // If a value was provided, return it.
            value = input;
            prompting = false;
        } else {
            // Otherwise, if a default value was provided, return it;
            if(defaultValue.isPresent()) {
                value = defaultValue.get();
                prompting = false;
            } else {
                // Otherwise, the user must provide a value.
                reader.println("Invalid response. Must provide a value.");
            }
        }
    }

    return value;
}
 
Example 18
Source File: CQLClient.java    From PoseidonX with Apache License 2.0 4 votes vote down vote up
private int readConconsole()
    throws IOException
{
    ConsoleReader reader = new ConsoleReader();
    reader.setBellEnabled(false);
    //这样就可以支持'!='这样的输入了。
    reader.setExpandEvents(false);
    reader.setCopyPasteDetection(true);
    
    String line;
    int ret = 0;
    StringBuilder prefix = new StringBuilder();
    printHelp();
    
    String tip = DEFAULT_CLI_TIP;
    
    while ((line = reader.readLine(tip)) != null)
    {
        tip = WAITING_INPUT_TIP;
        if (!prefix.toString().equals(""))
        {
            prefix.append("\n");
        }
        
        if (line.trim().startsWith("--"))
        {
            continue;
        }
        
        if (line.trim().endsWith(";") && !line.trim().endsWith("\\;"))
        {
            line = prefix.toString() + line;
            ret = processLine(line);
            prefix.delete(0, prefix.length());
            tip = DEFAULT_CLI_TIP;
        }
        else
        {
            prefix.append(line);
            continue;
        }
    }
    return ret;
}
 
Example 19
Source File: Setup.java    From CloudNet with Apache License 2.0 4 votes vote down vote up
@Override
public void start(ConsoleReader consoleReader) {
    SetupRequest setupRequest = null;
    while (!requests.isEmpty()) {
        if (setupRequest == null) {
            setupRequest = requests.poll();
        }
        System.out.print(setupRequest.getQuestion() + " | " + setupRequest.getResponseType().toString());

        String input;
        try {
            input = consoleReader.readLine();
        } catch (Exception ex) {
            System.out.println("Error while reading input: " + ex.getLocalizedMessage());
            continue;
        }

        if (input.equalsIgnoreCase(CANCEL)) {
            if (setupCancel != null) {
                setupCancel.cancel();
            }
            return;
        }

        if (!input.isEmpty() && !input.equals(NetworkUtils.SPACE_STRING)) {
            switch (setupRequest.getResponseType()) {
                case NUMBER:
                    if (!NetworkUtils.checkIsNumber(input)) {
                        System.out.println(setupRequest.getInValidMessage());
                        continue;
                    }
                    if (setupRequest.getValidater() != null) {
                        if (setupRequest.getValidater().doCatch(input)) {
                            document.append(setupRequest.getName(), Integer.parseInt(input));
                            setupRequest = null;
                        } else {
                            System.out.println(setupRequest.getInValidMessage());
                            continue;
                        }
                    } else {
                        document.append(setupRequest.getName(), Integer.parseInt(input));
                        setupRequest = null;
                    }
                    break;
                case BOOL:
                    if (input.equalsIgnoreCase("yes") || (setupRequest.getValidater() != null && setupRequest.getValidater().doCatch(
                        input))) {
                        document.append(setupRequest.getName(), true);
                        setupRequest = null;
                        continue;
                    }
                    if (input.equalsIgnoreCase("no") || (setupRequest.getValidater() != null && setupRequest.getValidater().doCatch(
                        input))) {
                        document.append(setupRequest.getName(), false);
                        setupRequest = null;
                        continue;
                    }

                    System.out.println(setupRequest.getInValidMessage());
                    break;
                case STRING:
                    if (setupRequest.getValidater() != null) {
                        if (setupRequest.getValidater().doCatch(input)) {
                            document.append(setupRequest.getName(), input);
                            setupRequest = null;
                        } else {
                            System.out.println(setupRequest.getInValidMessage());
                            continue;
                        }
                    } else {
                        document.append(setupRequest.getName(), input);
                        setupRequest = null;
                    }
                    break;
            }
        } else {
            System.out.println(setupRequest.getInValidMessage());
        }

    }

    if (setupComplete != null) {
        setupComplete.complete(document);
    }

}
 
Example 20
Source File: CloudConfig.java    From CloudNet with Apache License 2.0 4 votes vote down vote up
private void defaultInit(ConsoleReader consoleReader) throws Exception {
    if (Files.exists(configPath)) {
        return;
    }

    String hostName = NetworkUtils.getHostName();
    if (hostName.equals("127.0.0.1") || hostName.equals("127.0.1.1") || hostName.split("\\.").length != 4) {
        String input;
        System.out.println("Your IP address where located is 127.0.0.1 please write your service ip");
        while ((input = consoleReader.readLine()) != null) {
            if ((input.equals("127.0.0.1") || input.equals("127.0.1.1")) || input.split("\\.").length != 4) {
                System.out.println("Please write your real ip address :)");
                continue;
            }
            hostName = input;
            break;
        }
    }

    Configuration configuration = new Configuration();

    configuration.set("general.auto-update", false);
    configuration.set("general.dynamicservices", false);
    configuration.set("general.server-name-splitter", "-");
    configuration.set("general.notify-service", true);
    configuration.set("general.disabled-modules", new ArrayList<>());
    configuration.set("general.cloudGameServer-wrapperList", Collections.singletonList("Wrapper-1"));

    configuration.set("general.haste.server",
                      Arrays.asList("https://hastebin.com",
                                    "https://hasteb.in",
                                    "https://haste.llamacloud.io",
                                    "https://paste.dsyn.ga"));

    configuration.set("server.hostaddress", hostName);
    configuration.set("server.ports", Collections.singletonList(1410));
    configuration.set("server.webservice.hostaddress", hostName);
    configuration.set("server.webservice.port", 1420);

    configuration.set("cloudnet-statistics.enabled", true);
    configuration.set("cloudnet-statistics.uuid", UUID.randomUUID().toString());
    configuration.set("networkproperties.test", true);

    try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(configPath), StandardCharsets.UTF_8)) {
        CONFIGURATION_PROVIDER.save(configuration, outputStreamWriter);
    }
}