-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce MaskingCallback to provide hooks to customize line output, f…
…ixes #163 Patch provided by John Poth, thx !
- Loading branch information
Showing
7 changed files
with
302 additions
and
41 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
builtins/src/test/java/org/jline/example/ArgumentMaskCallbackReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright (c) 2002-2017, the original author or authors. | ||
* | ||
* This software is distributable under the BSD license. See the terms of the | ||
* BSD license in the documentation provided with this software. | ||
* | ||
* http://www.opensource.org/licenses/bsd-license.php | ||
*/ | ||
package org.jline.example; | ||
|
||
import java.io.IOException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jline.reader.LineReader; | ||
import org.jline.reader.LineReaderBuilder; | ||
import org.jline.reader.MaskingCallback; | ||
import org.jline.terminal.Terminal; | ||
import org.jline.terminal.TerminalBuilder; | ||
|
||
public class ArgumentMaskCallbackReader | ||
{ | ||
/** | ||
* Mask a certain argument for a given command in the console. So for example 'add-user 2', 'add-user username password' | ||
* will be displayed as 'add-user username ********'. | ||
*/ | ||
public static void usage() { | ||
System.out.println("Usage: java " | ||
+ ArgumentMaskCallbackReader.class.getName() + " [command] [argument-pos-to-mask]"); | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
Terminal terminal = TerminalBuilder.terminal(); | ||
LineReader reader = LineReaderBuilder.builder() | ||
.terminal(terminal).build(); | ||
|
||
String command = args[0]; | ||
int pos = Integer.valueOf(args[1]); | ||
|
||
MaskingCallback maskingCallback = new CommandArgumentMask(command, pos); | ||
|
||
String line; | ||
do { | ||
line = reader.readLine("prompt> ", null, maskingCallback, null); | ||
System.out.println("Got line: " + line); | ||
} | ||
while (line != null && line.length() > 0); | ||
} | ||
|
||
private static class CommandArgumentMask implements MaskingCallback { | ||
|
||
private final Pattern pattern; | ||
private static final Character mask = '*'; | ||
private final int pos; | ||
|
||
public CommandArgumentMask(String command, int pos) { | ||
StringBuilder regex = new StringBuilder(); | ||
regex.append(Pattern.quote(command)); | ||
for (int i = 0; i < pos; i++) { | ||
regex.append(" +([^ ]+)"); | ||
} | ||
this.pattern = Pattern.compile(regex.toString()); | ||
this.pos = pos; | ||
} | ||
|
||
@Override | ||
public String display(String line) { | ||
return filter(line); | ||
} | ||
|
||
@Override | ||
public String history(String line) | ||
{ | ||
final String filter = filter(line); | ||
System.out.println(); | ||
System.out.print("Adding to history: " + filter ); | ||
return filter; | ||
} | ||
|
||
public String filter(String line) { | ||
Matcher m = pattern.matcher(line); | ||
|
||
if( m.find() ) { | ||
StringBuilder maskedLine = new StringBuilder(line); | ||
for(int i = m.start(pos); i < m.end(pos); i++){ | ||
maskedLine.replace(i,i+1, String.valueOf(mask)); | ||
} | ||
return maskedLine.toString(); | ||
} else { | ||
return line; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
builtins/src/test/java/org/jline/example/OptionMaskCallbackReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (c) 2002-2017, the original author or authors. | ||
* | ||
* This software is distributable under the BSD license. See the terms of the | ||
* BSD license in the documentation provided with this software. | ||
* | ||
* http://www.opensource.org/licenses/bsd-license.php | ||
*/ | ||
package org.jline.example; | ||
|
||
import java.io.IOException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jline.reader.LineReader; | ||
import org.jline.reader.LineReaderBuilder; | ||
import org.jline.reader.MaskingCallback; | ||
import org.jline.terminal.Terminal; | ||
import org.jline.terminal.TerminalBuilder; | ||
|
||
public class OptionMaskCallbackReader | ||
{ | ||
/** | ||
* Mask a certain option value in the command line. So for example for the option '--password', '--password mypassword' | ||
* will be displayed as '--password **********'. | ||
*/ | ||
public static void usage() { | ||
System.out.println("Usage: java " | ||
+ OptionMaskCallbackReader.class.getName() + " [option-name-to-mask]"); | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
Terminal terminal = TerminalBuilder.terminal(); | ||
LineReader reader = LineReaderBuilder.builder() | ||
.terminal(terminal).build(); | ||
|
||
String optionToMask = args[0]; | ||
|
||
MaskingCallback maskingCallback = new OptionValueMask(optionToMask); | ||
|
||
String line; | ||
do { | ||
line = reader.readLine("prompt> ", null, maskingCallback, null); | ||
System.out.println("Got line: " + line); | ||
} | ||
while (line != null && line.length() > 0); | ||
} | ||
|
||
private static class OptionValueMask implements MaskingCallback { | ||
|
||
private final Pattern pattern; | ||
private static final Character mask = '*'; | ||
|
||
public OptionValueMask(String option) { | ||
String patternString= ".*?" + Pattern.quote(option) + " ??([^ ]+)"; | ||
this.pattern = Pattern.compile(patternString); | ||
} | ||
|
||
@Override | ||
public String display(String line) { | ||
return filter(line); | ||
} | ||
|
||
@Override | ||
public String history(String line) | ||
{ | ||
final String filter = filter(line); | ||
System.out.println(); | ||
System.out.print("Adding to history: " + filter ); | ||
return filter; | ||
} | ||
|
||
public String filter(String line) { | ||
Matcher m = pattern.matcher(line); | ||
|
||
if( m.find() ) { | ||
StringBuilder maskedLine = new StringBuilder(line); | ||
for(int i = m.start(1); i < m.end(1); i++){ | ||
maskedLine.replace(i,i+1, String.valueOf(mask)); | ||
} | ||
return maskedLine.toString(); | ||
} else { | ||
return line; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
reader/src/main/java/org/jline/reader/MaskingCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2002-2017, the original author or authors. | ||
* | ||
* This software is distributable under the BSD license. See the terms of the | ||
* BSD license in the documentation provided with this software. | ||
* | ||
* http://www.opensource.org/licenses/bsd-license.php | ||
*/ | ||
package org.jline.reader; | ||
|
||
/** | ||
* Callback used to mask parts of the line | ||
*/ | ||
public interface MaskingCallback { | ||
|
||
/** | ||
* Transforms the line before it is displayed so that | ||
* some parts can be hidden. | ||
*/ | ||
String display(String line); | ||
|
||
/** | ||
* Transforms the line before storing in the history. | ||
*/ | ||
String history(String line); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.