Skip to content

Commit

Permalink
add color support to JLineTextTerminal
Browse files Browse the repository at this point in the history
  • Loading branch information
siordache committed Mar 3, 2017
1 parent 00d5b47 commit a11d035
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 149 deletions.
7 changes: 6 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,18 @@ project('text-io-demo') {
applicationName = "textio-demo"

startScripts {
defaultJvmOpts = ['-Dlogback.configurationFile=../logback.xml']
defaultJvmOpts = ['-Dlogback.configurationFile=logback.xml']
}

applicationDistribution.from("${rootProject.projectDir}/dist") {
exclude 'xbin'
filter(ReplaceTokens, tokens:[textIoVersion : textIoVersion])
}

applicationDistribution.from("${rootProject.projectDir}/dist/xbin") {
into 'bin'
}

applicationDistribution.from("${rootProject.projectDir}/..") {
include('LICENSE', 'NOTICE')
}
Expand Down
4 changes: 3 additions & 1 deletion dist/README
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=======================================================================
Text IO @textIoVersion@
Text IO @textIoVersion@ Demo
=======================================================================

Start the demo by executing 'textio-demo' or 'textio-demo.bat' in the bin directory.

Visit the Text IO site for the latest news and downloads: https://github.com/beryx/text-io
13 changes: 0 additions & 13 deletions dist/logback.xml

This file was deleted.

18 changes: 18 additions & 0 deletions dist/xbin/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="OFF">
<appender-ref ref="STDOUT"/>
</root>
<!--
<logger name="org.beryx.textio" level="trace" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
-->
</configuration>
13 changes: 13 additions & 0 deletions dist/xbin/textio.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# swing.user.interrupt.key = ctrl Q
# web.user.interrupt.key = ctrl Q

textio.user.interrupt.key = ctrl Q

textio.prompt.color = green
# textio.prompt.bgcolor = yellow
# textio.prompt.bold = true


textio.input.color = yellow
# textio.input.bgcolor = blue
# textio.input.bold = true
26 changes: 0 additions & 26 deletions text-io-demo/src/main/java/org/beryx/textio/demo/TextIoDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import org.beryx.textio.TextTerminal;
import org.beryx.textio.TextTerminalProvider;
import org.beryx.textio.console.ConsoleTextTerminalProvider;
import org.beryx.textio.jline.AnsiTextTerminal;
import org.beryx.textio.jline.JLineTextTerminalProvider;
import org.beryx.textio.swing.SwingTextTerminal;
import org.beryx.textio.swing.SwingTextTerminalProvider;
import org.beryx.textio.system.SystemTextTerminal;
import org.beryx.textio.system.SystemTextTerminalProvider;
Expand Down Expand Up @@ -89,7 +87,6 @@ private static TextIO chooseTextIO() {
new SystemTextTerminalProvider(),
new ConsoleTextTerminalProvider(),
new JLineTextTerminalProvider(),
new NamedProvider("ANSI terminal", () -> createAnsiTextTerminal(textIO)),
new SwingTextTerminalProvider(),
new NamedProvider("Web terminal", () -> createWebTextTerminal(textIO))
)
Expand All @@ -111,29 +108,6 @@ private static TextIO chooseTextIO() {
}
}

private static AnsiTextTerminal createAnsiTextTerminal(TextIO textIO) {
boolean bold = textIO.newBooleanInputReader()
.withDefaultValue(false)
.read("Bold text?");

String[] colors = AnsiTextTerminal.ANSI_COLOR_MAP.keySet().toArray(new String[0]);
String color = textIO.newStringInputReader()
.withNumberedPossibleValues(colors)
.withDefaultValue("yellow")
.read("Text color");

String bgColor = textIO.newStringInputReader()
.withNumberedPossibleValues(colors)
.withDefaultValue("blue")
.read("Background color");

AnsiTextTerminal terminal = new AnsiTextTerminal();
terminal.withBold(bold);
terminal.withColor(color);
terminal.withBackgroundColor(bgColor);
return terminal;
}

private static WebTextTerminal createWebTextTerminal(TextIO textIO) {
webServerPort = textIO.newIntInputReader()
.withDefaultValue(Service.SPARK_DEFAULT_PORT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.apache.commons.lang3.StringEscapeUtils;
import org.beryx.textio.AbstractTextTerminal;
import org.beryx.textio.PropertiesPrefixes;
import org.beryx.textio.TextTerminal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -38,6 +39,7 @@
* It works only in conjunction with a web server supporting the {@link DataApi} (such as {@link SparkDataServer})
* and a web component that accesses this API (typically via textterm.js).
*/
@PropertiesPrefixes({"web"})
public class WebTextTerminal extends AbstractTextTerminal<WebTextTerminal> implements DataApi {
private static final Logger logger = LoggerFactory.getLogger(WebTextTerminal.class);

Expand Down
13 changes: 8 additions & 5 deletions text-io/src/main/java/org/beryx/textio/AbstractTextTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public final String addDefaultProperty(String key, String value) {
}

public void initProperties() {
getPropertiesReader().ifPresent(reader -> initProperties(reader));
initProperties(getPropertiesReader().orElse(null));
}

public Optional<Reader> getPropertiesReader() {
Expand Down Expand Up @@ -113,15 +113,18 @@ public Optional<Reader> getPropertiesReader() {
} else {
logger.debug("No terminal properties file found in classpath.");
}
logger.debug("Using only default properties.");
return Optional.empty();
}

public void initProperties(Reader propsReader) {
Properties rawProps = new Properties();
try {
rawProps.load(propsReader);
} catch (IOException e) {
logger.warn("Failed to read terminal properties.", e);
if(propsReader != null) {
try {
rawProps.load(propsReader);
} catch (IOException e) {
logger.warn("Failed to read terminal properties.", e);
}
}
initProperties(rawProps);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.beryx.textio.console;

import org.beryx.textio.AbstractTextTerminal;
import org.beryx.textio.PropertiesPrefixes;
import org.beryx.textio.TextTerminal;

import java.io.Console;
Expand All @@ -24,6 +25,7 @@
/**
* A {@link TextTerminal} backed by a {@link Console}.
*/
@PropertiesPrefixes({"console"})
public class ConsoleTextTerminal extends AbstractTextTerminal<ConsoleTextTerminal> {
private final Console console;

Expand Down
91 changes: 0 additions & 91 deletions text-io/src/main/java/org/beryx/textio/jline/AnsiTextTerminal.java

This file was deleted.

Loading

0 comments on commit a11d035

Please sign in to comment.