Skip to content

Commit

Permalink
Inherit appName from the terminal, fixes #631 (#714)
Browse files Browse the repository at this point in the history
Co-authored-by: Brian Oxley <boxley@thoughtworks.com>
  • Loading branch information
gnodet and boxleytw authored Oct 14, 2021
1 parent 8843bbe commit 0a35dc7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
6 changes: 6 additions & 0 deletions reader/src/main/java/org/jline/reader/LineReaderBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public LineReader build() {
throw new IOError(e);
}
}

String appName = this.appName;
if (null == appName) {
appName = terminal.getName();
}

LineReaderImpl reader = new LineReaderImpl(terminal, appName, variables);
if (history != null) {
reader.setHistory(history);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ protected enum BellType {
int candidateStartPosition = 0;

public LineReaderImpl(Terminal terminal) throws IOException {
this(terminal, null, null);
this(terminal, terminal.getName(), null);
}

public LineReaderImpl(Terminal terminal, String appName) throws IOException {
Expand Down
49 changes: 49 additions & 0 deletions reader/src/test/java/org/jline/reader/LineReaderBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 20021, 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.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.reader;

import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class LineReaderBuilderTest {

@Test
public void testInheritAppNameFromTerminal() throws IOException {
final String expectedAppName = "BOB";
final Terminal terminal = TerminalBuilder.builder()
.name(expectedAppName)
.build();
final LineReader lineReader = LineReaderBuilder.builder()
.terminal(terminal)
.build();

assertEquals("Did not inherit appName from terminal",
expectedAppName, lineReader.getAppName());
}

@Test
public void testPreferAppNameFromBuilder() throws IOException {
final String expectedAppName = "NANCY";
final Terminal terminal = TerminalBuilder.builder()
.name(expectedAppName + "X")
.build();
final LineReader lineReader = LineReaderBuilder.builder()
.appName(expectedAppName)
.terminal(terminal)
.build();

assertEquals("Did not prefer appName from builder",
expectedAppName, lineReader.getAppName());
}
}
36 changes: 31 additions & 5 deletions reader/src/test/java/org/jline/reader/impl/LineReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public void testGroup() throws Exception {
c.add(new Candidate("option3", "option3", "group2", null, null, null, false));
c.add(new Candidate("option4", "option4", "group2", null, null, null, false));

assertEquals("group1\noption1 option2\ngroup2\noption3 option4", computeGroupPost(c, true, true));
assertEquals("group1\ngroup2\noption1 option2 option3 option4", computeGroupPost(c, true, false));
assertEquals("option1 option2 option3 option4", computeGroupPost(c, false, false));
assertEquals("option1 option2\noption3 option4", computeGroupPost(c, false, true));
assertEquals("group1\noption1 option2\ngroup2\noption3 option4", computeGroupPost(c, true, true));
assertEquals("group1\ngroup2\noption1 option2 option3 option4", computeGroupPost(c, true, false));
assertEquals("option1 option2 option3 option4", computeGroupPost(c, false, false));
assertEquals("option1 option2\noption3 option4", computeGroupPost(c, false, true));
}

private String computeGroupPost(List<Candidate> c, boolean autoGroup, boolean groupName) throws IOException {
Expand All @@ -101,19 +101,22 @@ private String computeGroupPost(List<Candidate> c, boolean autoGroup, boolean gr
public void testConEmuLineReaderClearScreen() throws IOException {
System.setProperty("org.jline.terminal.conemu.disable-activate", "false");
StringWriter sw = new StringWriter();
AbstractWindowsTerminal terminal = new AbstractWindowsTerminal(new BufferedWriter(sw), "name", TYPE_WINDOWS_CONEMU, Charset.defaultCharset(),0,
AbstractWindowsTerminal terminal = new AbstractWindowsTerminal(new BufferedWriter(sw), "name", TYPE_WINDOWS_CONEMU, Charset.defaultCharset(), 0,
false, Terminal.SignalHandler.SIG_DFL) {
@Override
protected int getConsoleMode() {
return 0;
}

@Override
protected void setConsoleMode(int mode) {
}

@Override
protected boolean processConsoleInput() throws IOException {
return false;
}

@Override
public Size getSize() {
return new Size(80, 25);
Expand All @@ -137,5 +140,28 @@ public Size getSize() {
assertTrue(sw.toString().contains("\u001b[9999E"));
}

@Test
public void testInheritAppNameFromTerminal() throws IOException {
final String expectedAppName = "BOB";
final Terminal terminal = TerminalBuilder.builder()
.name(expectedAppName)
.build();
final LineReader lineReader = new LineReaderImpl(terminal);

assertEquals("Did not inherit appName from terminal",
expectedAppName, lineReader.getAppName());
}

@Test
public void testPreferAppNameFromConstructor() throws IOException {
final String expectedAppName = "NANCY";
final Terminal terminal = TerminalBuilder.builder()
.name(expectedAppName + "X")
.build();
final LineReader lineReader =
new LineReaderImpl(terminal, expectedAppName);

assertEquals("Did not prefer appName from builder",
expectedAppName, lineReader.getAppName());
}
}

0 comments on commit 0a35dc7

Please sign in to comment.