Skip to content

Commit

Permalink
增加层级环境变量机制
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxel Black committed Mar 7, 2021
1 parent 03ca98a commit 0d1a781
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/main/java/ink/pd2/shell/api/CommandParameter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ink.pd2.shell.api;

public class CommandParameter {


public CommandParameter(String command) {
//TODO 指令参数处理
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/ink/pd2/shell/buildin/CorePlugin.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package ink.pd2.shell.buildin;

import ink.pd2.shell.api.Command;
import ink.pd2.shell.api.CommandParameter;
import ink.pd2.shell.api.plugin.PluginInterface;
import ink.pd2.shell.core.Resources;
import ink.pd2.shell.core.Shell;
import ink.pd2.shell.core.CommandExecutedListener;
import ink.pd2.shell.core.CommandEnteredListener;
import ink.pd2.shell.api.plugin.Plugin;

import java.util.*;
Expand All @@ -31,14 +32,16 @@ public CorePlugin() {
}

@Override
public void init( PluginInterface api) {
api.listener.add(new CommandExecutedListener() {
public void init(PluginInterface api) {
api.listener.add(new CommandEnteredListener() {
@Override
public int getPriority() {
return Shell.DEFAULT_PRIORITY;
}
@Override
public Boolean event(Shell shell, String command) {
CommandParameter parameter = new CommandParameter(command);

//TODO 解析指令
return true;
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/ink/pd2/shell/buildin/VariableMark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ink.pd2.shell.buildin;

import ink.pd2.shell.core.Mark;

import java.util.HashMap;

public class VariableMark extends Mark {
private final VariableMark parent; //上级标记对象
private final HashMap<String, String> variables = new HashMap<>(); //本级变量存储

protected VariableMark(VariableMark parent) {
this.parent = parent;
}

public String get(String key) {
String v = variables.get(key);
if (v == null)
if (parent == null) return null;
else return parent.get(key);
return v;
}
public String put(String key, String value) {
return variables.put(key, value);
}
public String remove(String key) {
return variables.remove(key);
}

public VariableMark getParent() {
return parent;
}

public VariableMark newChild() {
return new VariableMark(this);
}
}
21 changes: 10 additions & 11 deletions src/main/java/ink/pd2/shell/buildin/VariableMarkProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
import ink.pd2.shell.core.Mark;
import ink.pd2.shell.core.MarkProvider;

import java.util.HashMap;

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

private final HashMap<String, String> variables = new HashMap<>();
public HashMap<String, String> getVariables() {
private final VariableMark variables = new VariableMark(null);
public VariableMark getVariables() {
return variables;
}

//TODO 基于对象区分的环境变量层叠式分配

public String getSign() {
return "v";
}

public String onMarkUpdate(Mark mark, String value) {

String variable = variables.get(value);
if (variable == null) {
return "\\&v:" + value + "\\&";
if (mark instanceof VariableMark) {
String variable = ((VariableMark) mark).get(value);
if (variable == null) {
return "\\&v:" + value + "\\&";
}
return variable;
} else {
throw new ClassCastException("The argument is NOT a VariableMark object.");
}
return variable;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ink.pd2.shell.core;

public abstract class CommandExecutedListener implements Listener {
public abstract class CommandEnteredListener implements Listener {

@Override
public String getType() {
return "command-executed";
return "command-entered";
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ink/pd2/shell/core/Mark.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.HashMap;
import java.util.Map;

public final class Mark {
public class Mark {
public final static Mark INS = new Mark();

private final static Map<String, MarkProvider> marks = new HashMap<>();
Expand Down
42 changes: 32 additions & 10 deletions src/main/java/ink/pd2/shell/core/Shell.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ink.pd2.shell.core;

import ink.pd2.shell.Main;
import ink.pd2.shell.buildin.VariableMark;
import ink.pd2.shell.buildin.VariableMarkProvider;

import java.io.File;
import java.io.IOException;
Expand All @@ -14,6 +16,26 @@ public class Shell {
public final static int PRIORITY_LOW = 2;
public final static int DEFAULT_PRIORITY = PRIORITY_MEDIUM;

//环境变量标记
private VariableMark variables;
public void putVariable(String key, String value) {
variables.put(key, value);
Logger.INS.debug("Shell@" + thread.getId() + "<Variable>",
key + " -> " + value);
}
public String getVariableValue(String key) {
String value = variables.get(key);
Logger.INS.debug("Shell@" + thread.getId() + "<Variable>",
"^ " + key + " : " + (value != null));
return value;
}
public String removeVariable(String key) {
String value = VariableMarkProvider.INS.getVariables().remove(key);
Logger.INS.debug("Shell@" + thread.getId() + "<Variable>",
"- " + key + " : " + (value != null));
return value;
}

//Shell信息
private Thread thread;
public Thread getThread() {
Expand All @@ -37,11 +59,11 @@ public void setDirectory(File file) {
}

public String getCurrentUser() {
return Main.getVariableValue("user");
return getVariableValue("user");
}

//分优先级的指令执行事件
private final ArrayList<ArrayList<CommandExecutedListener>> listeners
private final ArrayList<ArrayList<CommandEnteredListener>> listeners
= new ArrayList<>(5);

public Shell() {
Expand All @@ -50,13 +72,13 @@ public Shell() {
listeners.add(new ArrayList<>(1));
Logger.INS.info("Shell", Resources.INS.getString("psh.log-init-shell"));
//将事件按优先级排序
List<Listener> ls = Resources.INS.getListeners("psh.command-executed");
List<Listener> ls = Resources.INS.getListeners("psh.command-entered");
for (Listener li : ls) {
CommandExecutedListener l = (CommandExecutedListener) li;
CommandEnteredListener l = (CommandEnteredListener) li;
listeners.get(l.getPriority()).add(l);
}
try {
setDirectory(new File(Main.getVariableValue("current_dir")).getCanonicalFile());
setDirectory(new File(getVariableValue("current_dir")).getCanonicalFile());
} catch (IOException e) {
Logger.INS.writeException("Shell.init", e);
}
Expand All @@ -73,17 +95,17 @@ public void run() {
//更新变量值
user = getCurrentUser();
try {
Main.putVariable("current_dir", dir.getCanonicalPath());
Main.putVariable("current_folder", dir.getCanonicalFile().getName());
putVariable("current_dir", dir.getCanonicalPath());
putVariable("current_folder", dir.getCanonicalFile().getName());
} catch (IOException e) {
Logger.INS.writeException(getLogLocation(), e);
}
//指令处理和执行
String c = Main.input.getCommand(this);
String c = Main.input.getCommand(this, variables);

event:
for (ArrayList<CommandExecutedListener> ls : listeners)
for (CommandExecutedListener l : ls) {
for (ArrayList<CommandEnteredListener> ls : listeners)
for (CommandEnteredListener l : ls) {
Boolean b = l.event(this, c);
if (b == null) break event;
if (!b) break shell;
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/ink/pd2/shell/io/ConsoleInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ink.pd2.shell.core.Shell;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.UserInterruptException;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;

Expand Down Expand Up @@ -48,10 +49,14 @@ public String readLine() {
}

@Override
public String getCommand(Shell shell) {
return reader.readLine(
Mark.INS.update(Resources.INS.getString("psh.shell-prompt-text-left")),
Mark.INS.update(Resources.INS.getString("psh.shell-prompt-text-right")),
(Character) null, null);
public String getCommand(Shell shell, Mark mark) {
try {
return reader.readLine(
mark.update(Resources.INS.getString("psh.shell-prompt-text-left")),
mark.update(Resources.INS.getString("psh.shell-prompt-text-right")),
(Character) null, null);
} catch (UserInterruptException e) {
return null;
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/ink/pd2/shell/io/Input.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ink.pd2.shell.io;

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

public abstract class Input {
Expand All @@ -10,7 +11,7 @@ public String readPassword() {
public String newLine(String lPrompt, String rPrompt) {
return readLine();
}
public String getCommand(Shell shell) {
public String getCommand(Shell shell, Mark mark) {
return readLine();
}
}

0 comments on commit 0d1a781

Please sign in to comment.