-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the InteractiveCLI using Jline3 and picocli
- Loading branch information
1 parent
7699060
commit d75dfb6
Showing
5 changed files
with
105 additions
and
76 deletions.
There are no files selected for viewing
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
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
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
60 changes: 26 additions & 34 deletions
60
...t-demo/src/main/java/org/eclipse/leshan/client/demo/cli/interactive/TerminalAppender.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 |
---|---|---|
@@ -1,46 +1,38 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Sierra Wireless and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.html. | ||
* | ||
* Contributors: | ||
* Sierra Wireless - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.leshan.client.demo.cli.interactive; | ||
|
||
import java.io.IOException; | ||
import org.jline.reader.LineReader; | ||
|
||
import ch.qos.logback.core.ConsoleAppender; | ||
import jline.console.ConsoleReader; | ||
|
||
/** | ||
* A logback Console appender compatible with a Jline 2 Console reader. | ||
*/ | ||
public class TerminalAppender<E> extends ConsoleAppender<E> { | ||
|
||
private ConsoleReader console; | ||
private String prompt; | ||
private LineReader reader; | ||
|
||
@Override | ||
protected void subAppend(E event) { | ||
if (console == null || !console.getTerminal().isAnsiSupported()) | ||
super.subAppend(event); | ||
else { | ||
// stash prompt | ||
String stashed = ""; | ||
try { | ||
stashed = console.getCursorBuffer().copy().toString(); | ||
console.resetPromptLine("", "", -1); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// Display logs | ||
super.subAppend(event); | ||
|
||
// unstash prompt | ||
try { | ||
console.resetPromptLine(prompt, stashed, -1); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
public void setReader(LineReader reader) { | ||
this.reader = reader; | ||
} | ||
|
||
public void setConsole(ConsoleReader console) { | ||
this.console = console; | ||
this.prompt = console.getPrompt(); | ||
@Override | ||
protected void append(E eventObject) { | ||
if (reader == null) { | ||
super.append(eventObject); | ||
} else { | ||
reader.printAbove(new String(getEncoder().encode(eventObject))); | ||
} | ||
} | ||
} |
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