Skip to content

Commit

Permalink
对终端的启动做兼容性处理
Browse files Browse the repository at this point in the history
  • Loading branch information
LingoJack committed Dec 5, 2024
2 parents d2ba024 + aed3bd0 commit 2e47159
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 115 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<artifactId>commons-configuration2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>3.21.0</version> <!-- 请确认最新版本 -->
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
package com.lingoutil.workcopilot;

import com.lingoutil.workcopilot.handler.CommandHandler;
import com.lingoutil.workcopilot.runner.CommandRunner;
import com.lingoutil.workcopilot.util.LogUtil;
import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.UserInterruptException;
import org.jline.reader.impl.history.DefaultHistory;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;

import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

import static com.lingoutil.workcopilot.constant.Constant.LOG_MODE;
import static com.lingoutil.workcopilot.constant.Constant.MODE_VERBOSE;
import static com.lingoutil.workcopilot.util.LogUtil.RESET;
import static com.lingoutil.workcopilot.util.LogUtil.YELLOW;

public class WorkCopilotApplication {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// 设置控制台输出为UTF-8编码
System.setOut(new PrintStream(System.out, true, StandardCharsets.UTF_8));

if (args.length == 1) {
// just j command
runWithMultiMode(sc);
if (CommandRunner.getOsType().equals(CommandRunner.MAC)) {
runWithMultiModeOnUnix();
}
else {
runWithMultiModeOnWin();
}
}
else {
// j another argument
Expand Down Expand Up @@ -52,7 +63,58 @@ private static void runWithSingleMode(String[] args) {
}
}

private static void runWithMultiMode(Scanner sc) {
private static void runWithMultiModeOnUnix() {
try {
// 创建 JLine 终端和读取器
Terminal terminal = TerminalBuilder.builder().system(true).build();
LineReader reader = LineReaderBuilder.builder()
.terminal(terminal)
.history(new DefaultHistory())
.build();

LogUtil.info("Welcome to use work copilot \uD83D\uDE80 ~");
String prompt = YELLOW + "copilot > " + RESET; // 设置为亮黄色
while (true) {
try {
// 显示提示符并读取输入
String input = reader.readLine(prompt);
String[] args = ("j " + input).split("\\s+(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");

boolean verboseMode = LOG_MODE.equals(MODE_VERBOSE);
if (verboseMode) {
LogUtil.log("verbose mode is start: %s", verboseMode);
long startTime = System.currentTimeMillis();
long endTime = 0;
if (!isValidArgsNum(args)) {
continue;
}
String command = args[1];
CommandHandler.execute(command, args);
endTime = System.currentTimeMillis();
LogUtil.log("duration: %s ms", endTime - startTime);
}
else {
if (!isValidArgsNum(args)) {
continue;
}
String command = args[1];
CommandHandler.execute(command, args);
}
System.out.println();
} catch (UserInterruptException e) {
LogUtil.info("\nProgram interrupted. Use 'exit' to quit.");
} catch (EndOfFileException e) {
LogUtil.info("\nGoodbye!");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

private static void runWithMultiModeOnWin() {
Scanner sc = new Scanner(System.in);
String[] args;
LogUtil.info("Welcome to use work copilot \uD83D\uDE80 ~");
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public static void addNestedProperty(String parentKey, String childKey, String v
String fullKey = parentKey + "." + childKey;
LogUtil.log("添加嵌套键值对: " + fullKey + " = " + value);
config.setProperty(fullKey, value);
// todo
propertyCache.put(fullKey, value);
propertiesMapCache.clear();
saveConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public class Constant {
public static final String CATEGORY_BROWSER = "browser";
public static final String CATEGORY_EDITOR = "editor";
public static final String CATEGORY_VPN = "vpn";
public static final String CATEGORY_OUTER_URL = "outer-url";
public static final String CATEGORY_OUTER_URL = "outer_url";

public static String LOG_MODE = YamlConfig.initializeProperty(LOG, MODE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.lingoutil.workcopilot.scanner.CommandHandlerScanner;
import com.lingoutil.workcopilot.util.LogUtil;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
Expand Down Expand Up @@ -63,6 +64,25 @@ protected final boolean checkArgs(String[] argv, int expectedNum, Consumer<Strin
return true;
}

protected final boolean checkArgs(String[] argv, Consumer<String[]> errorHandler, int... expectedNums) {
int length = argv.length;
if (!containInArray(expectedNums,length)) {
LogUtil.error("expected argument num is %s, but got %d", Arrays.toString(expectedNums), length);
errorHandler.accept(argv);
return false;
}
return true;
}

private boolean containInArray(int[] expected, int target) {
for (int i : expected) {
if (i == target) {
return true;
}
}
return false;
}

protected final boolean checkArgs(String[] argv, int expectedNum) {
return checkArgs(argv, expectedNum, this::hint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ protected List<String> loadCommandList() {
@Override
protected void process(String[] argv) {
String alias = argv[2];

String path = YamlConfig.getProperty(PATH, alias);
if (!YamlConfig.containProperty(PATH, alias)
&& !YamlConfig.containProperty(INNER_URL, alias)
&& !YamlConfig.containProperty(OUTER_URL, alias)) {
LogUtil.error("No such alias %s", alias);
return;
}

String path = YamlConfig.getProperty(PATH, alias);

String category = argv[3];
switch (category) {
case CATEGORY_BROWSER -> {
Expand All @@ -40,7 +40,9 @@ protected void process(String[] argv) {
LogUtil.info("Add alias %s to VPN successfully", alias);
}
case CATEGORY_OUTER_URL -> {
path = YamlConfig.getProperty(INNER_URL, alias);
YamlConfig.addNestedProperty(category, alias, path);
YamlConfig.removeNestedProperty(INNER_URL, alias);
LogUtil.info("Add alias %s to OUTER_URL successfully", alias);
}
case SCRIPT -> {
Expand Down
Loading

0 comments on commit 2e47159

Please sign in to comment.