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

The following examples show how to use jline.console.ConsoleReader#setPrompt() . 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: 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 3
Source File: Main.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Override
public void doit(PrintWriter out, Blur.Iface client, String[] args) throws CommandException, TException,
    BlurException {
  ConsoleReader reader = getConsoleReader();
  if (reader != null) {
    try {
      reader.setPrompt("");
      reader.clearScreen();
    } catch (IOException e) {
      if (debug) {
        e.printStackTrace();
      }
      throw new CommandException(e.getMessage());
    }
  }
}
 
Example 4
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 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: HeroicInteractiveShell.java    From heroic with Apache License 2.0 6 votes vote down vote up
public static HeroicInteractiveShell buildInstance(
    final List<CommandDefinition> commands, FileInputStream input
) throws Exception {
    final ConsoleReader reader = new ConsoleReader("heroicsh", input, System.out, null);

    final FileHistory history = setupHistory(reader);

    if (history != null) {
        reader.setHistory(history);
    }

    reader.setPrompt(String.format("heroic> "));
    reader.addCompleter(new StringsCompleter(
        ImmutableList.copyOf(commands.stream().map((d) -> d.getName()).iterator())));
    reader.setHandleUserInterrupt(true);

    return new HeroicInteractiveShell(reader, commands, history);
}
 
Example 7
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 8
Source File: TopCommand.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Override
public void doit(PrintWriter out, Blur.Iface client, String[] args) throws CommandException, TException,
    BlurException {
  try {
    doitInternal(out, client, args);
  } finally {
    ConsoleReader reader = this.getConsoleReader();
    if (reader != null) {
      reader.setPrompt(Main.PROMPT);
    }
  }
}
 
Example 9
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 10
Source File: Main.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
private static void setPrompt(Iface client, ConsoleReader reader, PrintWriter out) throws BlurException, TException,
    CommandException, IOException {
  List<String> shardClusterList;
  try {
    shardClusterList = client.shardClusterList();
  } catch (BlurException e) {
    out.println("Unable to retrieve cluster information - " + e.getMessage());
    out.flush();
    if (debug) {
      e.printStackTrace(out);
    }
    System.exit(1);
    throw e; // will never be called
  }
  String currentPrompt = reader.getPrompt();
  String prompt;
  if (shardClusterList.size() == 1) {
    prompt = "blur (" + getCluster(client) + ")> ";
  } else if (cluster == null) {
    prompt = PROMPT;
  } else {
    prompt = "blur (" + cluster + ")> ";
  }
  if (currentPrompt == null || !currentPrompt.equals(prompt)) {
    reader.setPrompt(prompt);
  }
}
 
Example 11
Source File: WorfInteractive.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
private static String readInputString(ConsoleReader reader, PrintWriter out, String prompt, String defaultString) {
  try {
    // save file
    StringBuilder sb = new StringBuilder("warp10:");
    sb.append(prompt);
    if (!Strings.isNullOrEmpty(defaultString)) {
      sb.append(", default(");
      sb.append(defaultString);
      sb.append(")");
    }
    sb.append(">");
    reader.setPrompt(sb.toString());
    String line;
    while ((line = reader.readLine()) != null) {
      line = line.trim();
      if (Strings.isNullOrEmpty(line) && Strings.isNullOrEmpty(defaultString)) {
        continue;
      }
      if (Strings.isNullOrEmpty(line) && !Strings.isNullOrEmpty(defaultString)) {
        return defaultString;
      }
      if (line.equalsIgnoreCase("cancel")) {
        break;
      }
      return line;
    }
  } catch (Exception exp) {
    if (WorfCLI.verbose) {
      exp.printStackTrace();
    }
    out.println("Error, unable to read the string. error=" + exp.getMessage());
  }
  return null;

}
 
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: 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 14
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 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: 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 17
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 18
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 19
Source File: ShellCommand.java    From citeproc-java with Apache License 2.0 4 votes vote down vote up
@Override
public int doRun(String[] remainingArgs, InputReader in, PrintWriter out)
        throws OptionParserException, IOException {
    // prepare console
    final ConsoleReader reader = new ConsoleReader();
    reader.setPrompt("> ");
    reader.addCompleter(new ShellCommandCompleter(EXCLUDED_COMMANDS));
    FileHistory history = new FileHistory(new File(
            CSLToolContext.current().getConfigDir(), "shell_history.txt"));
    reader.setHistory(history);

    // enable colored error stream for ANSI terminals
    if (reader.getTerminal().isAnsiSupported()) {
        OutputStream errout = new ErrorOutputStream(reader.getTerminal()
                .wrapOutIfNeeded(System.out));
        System.setErr(new PrintStream(errout, false,
                ((OutputStreamWriter)reader.getOutput()).getEncoding()));
    }

    PrintWriter cout = new PrintWriter(reader.getOutput(), true);

    // print welcome message
    cout.println("Welcome to " + CSLToolContext.current().getToolName() +
            " " + CSLTool.getVersion());
    cout.println();
    cout.println("Type `help' for a list of commands and `help "
            + "<command>' for information");
    cout.println("on a specific command. Type `quit' to exit " +
            CSLToolContext.current().getToolName() + ".");
    cout.println();

    String line;
    ShellContext.enter();
    try {
        line = mainLoop(reader, cout);
    } finally {
        ShellContext.exit();

        // make sure we save the history before we exit
        history.flush();
    }

    // print Goodbye message
    if (line == null) {
        // user pressed Ctrl+D
        cout.println();
    }
    cout.println("Bye!");

    return 0;
}
 
Example 20
Source File: WorfInteractive.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
private static String readInputPath(ConsoleReader reader, PrintWriter out, String prompt, String defaultPath) {
  try {
    // save file
    StringBuilder sb = new StringBuilder();
    sb.append("warp10:");
    sb.append(prompt);
    if (!Strings.isNullOrEmpty(defaultPath)) {
      sb.append(", default(");
      sb.append(defaultPath);
      sb.append(")");
    }
    sb.append(">");

    reader.setPrompt(sb.toString());
    String line;
    while ((line = reader.readLine()) != null) {
      line = line.trim();
      if (Strings.isNullOrEmpty(line) && Strings.isNullOrEmpty(defaultPath)) {
        continue;
      }

      if (Strings.isNullOrEmpty(line) && !Strings.isNullOrEmpty(defaultPath)) {
        return defaultPath;
      }

      if (line.equalsIgnoreCase("cancel")) {
        break;
      }

      Path outputPath = Paths.get(line);
      if (Files.notExists(outputPath)) {
        out.println("The path " + line + " does not exists");
        continue;
      }
      return line;
    }
  } catch (Exception exp) {
    if (WorfCLI.verbose) {
      exp.printStackTrace();
    }
    out.println("Error, unable to read the path. error=" + exp.getMessage());
  }
  return null;
}