Skip to content

Commit

Permalink
Mark系统的又一bug修复 | 完成Shell部分 | 优化源码目录结构
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxel Black committed Feb 19, 2021
1 parent 84c53fe commit 1844c10
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 114 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'ink.pd2'
version '1.0.0-a110'
version '1.0.0-a111'

repositories {
mavenCentral()
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/ink/pd2/shell/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public static void println(String s) {

//环境变量
public static void putVariable(String key, String value) {
VariableMarkProvider.INSTANCE.getVariables().put(key, value);
VariableMarkProvider.INS.getVariables().put(key, value);
}
public static String getVariableValue(String key) {
return VariableMarkProvider.INSTANCE.getVariables().get(key);
return VariableMarkProvider.INS.getVariables().get(key);
}
public static String removeVariable(String key) {
return VariableMarkProvider.INSTANCE.getVariables().remove(key);
return VariableMarkProvider.INS.getVariables().remove(key);
}

/**
Expand All @@ -46,25 +46,25 @@ public static String removeVariable(String key) {
*
* <h3>用法</h3>
*
* 主进程由 main() 方法自动执行, 若无特别需要, 切记:
* <p>主进程由 main() 方法自动执行, 若无特别需要, 切记:</p>
*
* 请勿尝试调用此方法!
* <p>请勿尝试调用此方法!</p>
*
* 请勿尝试调用此方法!!
* <p>请勿尝试调用此方法!!</p>
*
* 请勿尝试调用此方法!!!
* <p>请勿尝试调用此方法!!!</p>
*/

public static void mainProcess() {
Logger.INS.writeDebugLog("Main[OBJECT]", "output: " + output);
Logger.INS.debug("Main[OBJECT]", "output: " + output);
print(Resources.INS.getString("psh.shell-greet-text"));
try {
putVariable("user", System.getProperty("user.name"));
putVariable("host", InetAddress.getLocalHost().getHostName());
// startShell(new Shell());
startShell(new Shell());
//TODO 监听网络Shell
} catch (Exception e) {
e.printStackTrace();
Logger.INS.writeException("Main", e);
}
}

Expand Down Expand Up @@ -99,23 +99,24 @@ public static void exit(String reason) {
*/

public static void main(String[] args) {
output = new ConsoleOutput(); //设置output流
output = new ConsoleOutput(); //设置output流

Initializer.INS.initMarks(); //初始化默认标记

Logger.INS.writeDebugLog("Main.PreInit", "Initialization started.");
Logger.INS.debug("Main.PreInit", "Initialization started.");

Initializer.INS.initResources(); //初始化资源
Initializer.INS.initTheme(); //初始化主题

input = new ConsoleInput(); //设置input流

//TODO 初始化插件

//TODO 判断是否有另一个psh进程正在运行

Logger.INS.writeDebugLog("Main.PreInit", "Initialization finished.");
Logger.INS.debug("Main.PreInit", "Initialization finished.");

//主进程
mainProcess();

Logger.INS.writeLog("Main", "The shell on '&v:user&' exit.");
Logger.INS.info("Main", "The shell on '&v:user&' exit.");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package ink.pd2.shell.core;
package ink.pd2.shell.api;

import ink.pd2.shell.core.CommandParameter;
import ink.pd2.shell.core.Shell;

import java.util.Arrays;
import java.util.HashSet;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ink/pd2/shell/api/CommandEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ink.pd2.shell.api;

import ink.pd2.shell.core.CommandParameter;
import ink.pd2.shell.core.Shell;

public interface CommandEvent {
void run(Shell shell, CommandParameter parameter);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ink.pd2.shell.plugin;
package ink.pd2.shell.api;

import ink.pd2.shell.core.Command;
import ink.pd2.shell.core.Resources;

public abstract class Plugin {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ink.pd2.shell.plugin;
package ink.pd2.shell.api;

public class PluginInitializationException extends RuntimeException {
public PluginInitializationException(String message) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ink/pd2/shell/buildin/ColorMarkProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.HashMap;

public class ColorMarkProvider implements MarkProvider {
public final static ColorMarkProvider INSTANCE = new ColorMarkProvider();
public final static ColorMarkProvider INS = new ColorMarkProvider();

private final HashMap<String, Integer> colors = new HashMap<>();
public void putColor(String key, int value) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ink/pd2/shell/buildin/CorePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ink.pd2.shell.core.Shell;
import ink.pd2.shell.core.CommandExecutedListener;
import ink.pd2.shell.plugin.Plugin;
import ink.pd2.shell.api.Plugin;

public class CorePlugin extends Plugin {
public CorePlugin() {
Expand Down
43 changes: 25 additions & 18 deletions src/main/java/ink/pd2/shell/buildin/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@
public final class Initializer {
public final static Initializer INS = new Initializer();

public void initPlugins() {
Logger.INS.info("Initializer",
Resources.INS.getString("psh.log-init-plugin"));
//TODO 初始化插件
}

public void initTheme() {
Logger.INS.writeLog("Initializer",
Logger.INS.info("Initializer",
Resources.INS.getString("psh.log-init-theme"));
//初始化主题
//TODO 初始化主题
}

public void initResources() {
Logger.INS.writeLog("Initializer", "Initializing core resources...");
Logger.INS.info("Initializer", "Initializing core resources...");

//初始化资源
putStr("psh.log-init-plugin", "Initializing Plugins...");
putStr("psh.log-init-shell", "Initializing new shell thread...");
putStr("psh.log-init-theme", "Loading UI theme...");
putStr("psh.log-init-jline-reader", "Initializing JLine terminal reader...");
Expand All @@ -38,26 +45,26 @@ public void initResources() {
=====================================================
*/

Logger.INS.writeLog("Initializer", "Resources initialization finished.");
Logger.INS.info("Initializer", "Resources initialization finished.");
}
private void putStr(String k, String v) {
Resources.INS.putString(k, v);
}

public void initMarks() {
Mark.INS.regMarkProvider(VariableMarkProvider.INSTANCE);
VariableMarkProvider.INSTANCE.getVariables().put("user", "user");
VariableMarkProvider.INSTANCE.getVariables().put("host", "localhost");
VariableMarkProvider.INSTANCE.getVariables().put("current_dir", ".");
Mark.INS.regMarkProvider(ColorMarkProvider.INSTANCE);
ColorMarkProvider.INSTANCE.putColor("black", Ansi.Color.BLACK.value());
ColorMarkProvider.INSTANCE.putColor("blue", Ansi.Color.BLUE.value());
ColorMarkProvider.INSTANCE.putColor("cyan", Ansi.Color.CYAN.value());
ColorMarkProvider.INSTANCE.putColor("default", Ansi.Color.DEFAULT.value());
ColorMarkProvider.INSTANCE.putColor("green", Ansi.Color.GREEN.value());
ColorMarkProvider.INSTANCE.putColor("magenta", Ansi.Color.MAGENTA.value());
ColorMarkProvider.INSTANCE.putColor("red", Ansi.Color.RED.value());
ColorMarkProvider.INSTANCE.putColor("white", Ansi.Color.WHITE.value());
ColorMarkProvider.INSTANCE.putColor("yellow", Ansi.Color.YELLOW.value());
Mark.INS.regMarkProvider(VariableMarkProvider.INS);
VariableMarkProvider.INS.getVariables().put("user", "user");
VariableMarkProvider.INS.getVariables().put("host", "localhost");
VariableMarkProvider.INS.getVariables().put("current_dir", ".");
Mark.INS.regMarkProvider(ColorMarkProvider.INS);
ColorMarkProvider.INS.putColor("black", Ansi.Color.BLACK.value());
ColorMarkProvider.INS.putColor("blue", Ansi.Color.BLUE.value());
ColorMarkProvider.INS.putColor("cyan", Ansi.Color.CYAN.value());
ColorMarkProvider.INS.putColor("default", Ansi.Color.DEFAULT.value());
ColorMarkProvider.INS.putColor("green", Ansi.Color.GREEN.value());
ColorMarkProvider.INS.putColor("magenta", Ansi.Color.MAGENTA.value());
ColorMarkProvider.INS.putColor("red", Ansi.Color.RED.value());
ColorMarkProvider.INS.putColor("white", Ansi.Color.WHITE.value());
ColorMarkProvider.INS.putColor("yellow", Ansi.Color.YELLOW.value());
}
}
4 changes: 2 additions & 2 deletions src/main/java/ink/pd2/shell/buildin/VariableMarkProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.HashMap;

public class VariableMarkProvider implements MarkProvider {
public final static VariableMarkProvider INSTANCE = new VariableMarkProvider();
public final static VariableMarkProvider INS = new VariableMarkProvider();

private final HashMap<String, String> variables = new HashMap<>();
public HashMap<String, String> getVariables() {
Expand All @@ -19,7 +19,7 @@ public String getSign() {
public String onMarkUpdate(String value) {
String variable = variables.get(value);
if (variable == null) {
return "&v:" + value + "&";
return "\\&v:" + value + "\\&";
}
return variable;
}
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/ink/pd2/shell/buildin/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
"class": "ink.pd2.shell.core.CorePlugin",
"sign": "",
"order": 1
},
{
"class": "ink.maxelbk.psh.edge.PshEdgePlugin",
"sign": "",
"order": 1
}
]
5 changes: 0 additions & 5 deletions src/main/java/ink/pd2/shell/core/CommandEvent.java

This file was deleted.

27 changes: 27 additions & 0 deletions src/main/java/ink/pd2/shell/core/CommandExecutedListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
package ink.pd2.shell.core;

public interface CommandExecutedListener extends ink.pd2.shell.core.Listener {

/**
* <h2>getPriority() | 事件执行的优先级</h2>
*
* <p>可用的优先级有 {@code PRIORITY_HIGH} (高优先级),
* {@code PRIORITY_MEDIUM} (中等优先级),
* {@code PRIORITY_LOW} (低优先级)</p>
*
* @return 优先级
*
* @see ink.pd2.shell.core.Shell
*
* @since PSH 1.0
*/

int getPriority();

/**
* <h2>event() | 事件活动</h2>
*
* @param shell 执行指令的Shell对象
* @param command 执行的指令对象
*
* @return 下一步操作: true为继续运行, false为退出, null为跳过其他事件直接进入下一循环
*
* @since PSH 1.0
*/

Boolean event(Shell shell, String command);
}
14 changes: 7 additions & 7 deletions src/main/java/ink/pd2/shell/core/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
public final class Logger {
public static final Logger INS = new Logger();

public void writeDebugLog(String location, String message) {
Main.println("[&color:blue.null[" + getDate(true) + "]&] [&color:yellow.null[Debug]&|&color:cyan.null[" + location + "]&] " + message);
public void debug(String location, String message) {
Main.println("[&color:blue.null[" + getDate(true) + "]&] [&color:yellow.null[D]&|&color:cyan.null[" + location + "]&] " + message);
}

public void writeLog(String location, String message) {
Main.println("[&color:blue.null[" + getDate(false) + "]&] [&color:green.null[Info]&|&color:cyan.null[" + location + "]&] " + message);
public void info(String location, String message) {
Main.println("[&color:blue.null[" + getDate(false) + "]&] [&color:green.null[I]&|&color:cyan.null[" + location + "]&] " + message);
}

public void writeErrorLog(String location, String message) {
Main.println("[&color:blue.null[" + getDate(false) + "]&] [&color:red.null[Error]&|&color:cyan.null[" + location + "]&] " + message);
public void error(String location, String message) {
Main.println("[&color:blue.null[" + getDate(false) + "]&] [&color:red.null[E]&|&color:cyan.null[" + location + "]&] " + message);
}

public void writeException(String location, Exception exception) {
StringWriter s = new StringWriter();
PrintWriter p = new PrintWriter(s);
exception.printStackTrace(p);
writeDebugLog(location, "&color:red.null[EXCEPTION:\n" + s + "]&");
debug(location, "&color:red.null[EXCEPTION:\n" + s + "]&");
}

private String getDate(boolean ms) {
Expand Down
46 changes: 29 additions & 17 deletions src/main/java/ink/pd2/shell/core/Mark.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public String update(String s) {
*/

public String update(String s, boolean isTruth) {
//若有&nomark&标记则直接返回去掉标记的原字符串
//优化: 若有&nomark&标记则直接返回去掉标记的原字符串 (因效率提升不高且综合效率下降已注释)
// if (s.startsWith("&nomark&")) return s.substring(8);
StringBuilder str = new StringBuilder(s);
if (s == null) return null;
StringBuilder str = new StringBuilder(s);
int l, i; //字符串总长, 查找进度
while (true) {
i = 0;
Expand All @@ -78,8 +79,13 @@ public String update(String s, boolean isTruth) {
while (i < l) {
i1 = str.indexOf("&", i);
if (i1 == -1) break;
if (i1 != 0 && str.charAt(i1 - 1) == '\\') {
i = i1 + 1;
int i1c = i1 - 1;
if (i1 != 0 && str.charAt(i1c) == '\\') {
i = i1;
if (isTruth) {
str.deleteCharAt(i1c);
l--;
}
continue;
}
b1 = true; i = i1 + 1; break;
Expand All @@ -88,8 +94,13 @@ public String update(String s, boolean isTruth) {
while (b1 && i < l) {
i2 = str.indexOf("&", i);
if (i2 == -1) break;
if (str.charAt(i2 - 1) == '\\') {
i = i2 + 1;
int i2c = i2 - 1;
if (str.charAt(i2c) == '\\') {
i = i2;
if (isTruth) {
str.deleteCharAt(i2c);
l--;
}
continue;
}
b2 = true; break;
Expand All @@ -102,17 +113,18 @@ public String update(String s, boolean isTruth) {
if (ss.length == 2) {
MarkProvider mark = marks.get(ss[0]);
if (mark != null) {
str.replace(i1, i2 + 1, mark.onMarkUpdate(ss[1])); //替换标记
//查找转义字符 '\&' 并替换
if (isTruth) {
int str_i = 0, str_l = str.length();
while (str_i < str_l) {
str_i = str.indexOf("\\&", str_i);
if (str_i == -1) break;
str.deleteCharAt(str_i);
str_i++; str_l--;
}
}
String r = mark.onMarkUpdate(ss[1]); //用于替换的字符串
str.replace(i1, i2 + 1, r); //替换标记
//查找转义字符 '\&' 并替换 (因算法死循环问题已注释)
// if (isTruth) {
// str_l = str_i + r.length();
// while (str_i < str_l) {
// str_i = str.indexOf("\\&", str_i);
// if (str_i == -1 || str_i >= str_l) break;
// str.deleteCharAt(str_i);
// str_i++; str_l--;
// }
// }
continue; //在当前i位置之后继续查找
}
} else if (ss.length == 1) {
Expand Down
Loading

0 comments on commit 1844c10

Please sign in to comment.