feat: 新增JLine实现更好的控制台输入输出.
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -66,6 +66,12 @@
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>${sqlite.jdbc.version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.jline/jline -->
|
||||
<dependency>
|
||||
<groupId>org.jline</groupId>
|
||||
<artifactId>jline</artifactId>
|
||||
<version>3.28.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -7,6 +7,12 @@ import com.serliunx.ddns.constant.SystemConstants;
|
||||
import com.serliunx.ddns.core.context.FileInstanceContext;
|
||||
import com.serliunx.ddns.core.context.MultipleSourceInstanceContext;
|
||||
import com.serliunx.ddns.support.SystemInitializer;
|
||||
import com.serliunx.ddns.support.log.JLineAdaptAppender;
|
||||
import org.jline.reader.LineReader;
|
||||
import org.jline.reader.LineReaderBuilder;
|
||||
import org.jline.reader.impl.history.DefaultHistory;
|
||||
import org.jline.terminal.Terminal;
|
||||
import org.jline.terminal.TerminalBuilder;
|
||||
|
||||
/**
|
||||
* 启动类
|
||||
@@ -30,7 +36,7 @@ public final class ManagerLite {
|
||||
*/
|
||||
private static SystemInitializer systemInitializer;
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
// 配置初始化
|
||||
initConfiguration(args);
|
||||
@@ -40,6 +46,33 @@ public final class ManagerLite {
|
||||
|
||||
// 系统初始化
|
||||
initSystem();
|
||||
|
||||
Terminal terminal = TerminalBuilder.builder().system(true).build();
|
||||
LineReader lineReader = LineReaderBuilder.builder()
|
||||
.terminal(terminal)
|
||||
// 如果想记录历史命令,可以配置一个 History
|
||||
.history(new DefaultHistory())
|
||||
.build();
|
||||
|
||||
JLineAdaptAppender.setLineReader(lineReader);
|
||||
|
||||
String prompt = "client> ";
|
||||
|
||||
while (true) {
|
||||
// 该方法会阻塞,直到用户敲回车
|
||||
String line = lineReader.readLine(prompt);
|
||||
|
||||
// 当用户输入 exit 或 quit,就退出循环
|
||||
if ("exit".equalsIgnoreCase(line)
|
||||
|| "quit".equalsIgnoreCase(line)) {
|
||||
break;
|
||||
}
|
||||
// 在这里可以对用户输入做进一步处理
|
||||
terminal.writer().println("You entered: " + line);
|
||||
terminal.flush();
|
||||
}
|
||||
|
||||
terminal.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.serliunx.ddns.support.log;
|
||||
|
||||
import ch.qos.logback.classic.PatternLayout;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import ch.qos.logback.core.Layout;
|
||||
import org.jline.reader.LineReader;
|
||||
|
||||
/**
|
||||
* 适配JLine的控制台日志输出
|
||||
*
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @since 2025/1/15
|
||||
*/
|
||||
public final class JLineAdaptAppender extends AppenderBase<ILoggingEvent> {
|
||||
|
||||
/**
|
||||
* JLine的输入读取
|
||||
*/
|
||||
private static LineReader lineReader;
|
||||
|
||||
/**
|
||||
* 格式控制
|
||||
*/
|
||||
private Layout<ILoggingEvent> layout;
|
||||
|
||||
/**
|
||||
* 所配置的格式
|
||||
*/
|
||||
private String pattern;
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
super.start();
|
||||
PatternLayout patternLayout = new PatternLayout();
|
||||
patternLayout.setPattern(pattern);
|
||||
patternLayout.setContext(getContext());
|
||||
patternLayout.start();
|
||||
this.layout = patternLayout;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent event) {
|
||||
if (lineReader != null) {
|
||||
String formattedLog = layout.doLayout(event);
|
||||
lineReader.printAbove(formattedLog);
|
||||
} else {
|
||||
System.out.print(layout.doLayout(event));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置输入读取
|
||||
*
|
||||
* @param lr 读取
|
||||
*/
|
||||
public static void setLineReader(LineReader lr) {
|
||||
lineReader = lr;
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
public void setPattern(String pattern) {
|
||||
this.pattern = pattern;
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,15 @@
|
||||
<conversionRule conversionWord="instance" converterClass="com.serliunx.ddns.support.log.InstanceNameConverter"/>
|
||||
<conversionRule conversionWord="highlight" converterClass="com.serliunx.ddns.support.log.HighlightingCompositeConverter"/>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<appender name="JLINE" class="com.serliunx.ddns.support.log.JLineAdaptAppender">
|
||||
<pattern>
|
||||
%boldGreen(%d{yyyy-MM-dd HH:mm:ss(SSS)}) %cyan([%pid]) %magenta([%15.15thread]) %green([%16.16instance]) %highlight([%-6level]) %boldCyan(%-36logger{32}): %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="com.serliunx.ddns" level="DEBUG"/>
|
||||
<logger name="feign" level="DEBUG"/>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
<appender-ref ref="JLINE" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user