Skip to content

Commit

Permalink
Refactor CLI prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 1, 2024
1 parent 862a0e8 commit f877046
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
40 changes: 40 additions & 0 deletions convex-cli/src/main/java/convex/cli/ACommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package convex.cli;

import java.io.Console;
import java.io.IOException;
import java.util.Scanner;

import convex.cli.output.Coloured;
Expand Down Expand Up @@ -106,6 +107,27 @@ public char[] readPassword(String prompt) {
if (isColoured()) prompt = Coloured.blue(prompt);
return c.readPassword(prompt);
}

/**
* Prompt the user for a Y/N answer
* @param string
* @return
*/
public boolean question(String string) {
if (!isInteractive()) throw new CLIError("Can't ask user question in non-interactive mode: "+string);
try {
if (isColoured()) string=Coloured.blue(string);
inform(0,string);
char c=(char)System.in.read(); // Doesn't work because console is not in non-blocking mode?
if (c==-1) throw new CLIError("Unexpected end of input stream when expecting a keypress");
if (Character.toLowerCase(c)=='y') return true;
} catch (IOException e) {
throw new CLIError("Unexpected error getting console input: "+e);
}
return false;
}



/**
* Checks if the CLI should output ANSI colours
Expand All @@ -114,6 +136,24 @@ public char[] readPassword(String prompt) {
protected boolean isColoured() {
return cli().isColoured();
}


public void informSuccess(String message) {
if (isColoured()) message=Coloured.green(message);
inform(1, message);
}

public void informError(String message) {
if (isColoured()) message=Coloured.red(message);
inform(1, message);
}

public void inform(String message) {
if (isColoured()) message=Coloured.yellow(message);
inform(1,message);
}




}
36 changes: 0 additions & 36 deletions convex-cli/src/main/java/convex/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,21 +333,6 @@ public boolean isInteractive() {
return !nonInteractive;
}



public void informSuccess(String message) {
inform(1, noColour?message:Coloured.green(message));
}

public void informError(String message) {
inform(1, noColour?message:Coloured.red(message));
}

public void inform(String message) {
inform(1, noColour?message:Coloured.yellow(message));
}


public void setOut(String outFile) {
if (outFile == null || outFile.equals("-")) {
commandLine.setOut(new PrintWriter(System.out));
Expand All @@ -363,27 +348,6 @@ public void setOut(String outFile) {
}
}

/**
* Prompt the user for a Y/N answer
* @param string
* @return
*/
public boolean question(String string) {
if (!isInteractive()) throw new CLIError("Can't ask user question in non-interactive mode: "+string);
try {
if (isColoured()) string=Coloured.blue(string);
inform(0,string);
char c=(char)System.in.read(); // Doesn't work because console is not in non-blocking mode?
if (c==-1) throw new CLIError("Unexpected end of input stream when expecting a keypress");
if (Character.toLowerCase(c)=='y') return true;
} catch (IOException e) {
throw new CLIError("Unexpected error getting console input: "+e);
}
return false;
}



@Override
public Main cli() {
// We are the top level command!
Expand Down

0 comments on commit f877046

Please sign in to comment.