forked from Drive-for-Java/MyCMD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellContext.java
More file actions
63 lines (53 loc) · 1.61 KB
/
ShellContext.java
File metadata and controls
63 lines (53 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.mycmd;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class ShellContext {
private File currentDir;
private List<String> commandHistory;
private final long startTime;
private static final int MAX_HISTORY = 10;
public ShellContext() {
this.currentDir = new File(System.getProperty("user.dir"));
this.commandHistory = new ArrayList<>();
this.startTime = System.currentTimeMillis();
}
public File getCurrentDir() {
return currentDir;
}
public void setCurrentDir(File dir) {
this.currentDir = dir;
}
public long getStartTime() {
return startTime;
}
public List<String> getCommandHistory() {
return commandHistory;
}
public void addToHistory(String command) {
if (command != null && !command.trim().isEmpty()) {
commandHistory.add(command.trim());
if (commandHistory.size() > MAX_HISTORY) {
commandHistory.remove(0);
}
}
}
public void clearHistory() {
commandHistory.clear();
}
/**
* Resolve the given path (absolute or relative) to a File using the current directory.
* If the provided path is absolute, returns it directly; otherwise returns a File rooted at currentDir.
*/
public File resolvePath(String path) {
if (path == null || path.trim().isEmpty()) {
return currentDir;
}
File f = new File(path);
if (f.isAbsolute()) {
return f;
} else {
return new File(currentDir, path);
}
}
}