Java MiniShell 命令行解释器:自定义提示符、背景、字体、大小和颜色
{
"title": "Java MiniShell 命令行解释器:自定义提示符、背景、字体、大小和颜色",
"description": "这是一个用 Java 实现的简单命令行解释器 MiniShell,支持自定义提示符、背景、字体、大小和颜色,并具有命令历史记录功能。",
"keywords": "Java, MiniShell, 命令行解释器, 自定义提示符, 背景, 字体, 大小, 颜色, 命令历史记录",
"content": "import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MiniShell {
private static final String CONFIG_FILE = "miniShell.conf";
private static final String DEFAULT_PROMPT = "# ";
private static final String DEFAULT_BACKGROUND = "black";
private static final String DEFAULT_FONT = "宋体";
private static final int DEFAULT_FONTSIZE = 30;
private static final String DEFAULT_FONTCOLOR = "white";
private String prompt;
private String background;
private String font;
private int fontSize;
private String fontColor;
private List
public MiniShell() {
this.prompt = DEFAULT_PROMPT;
this.background = DEFAULT_BACKGROUND;
this.font = DEFAULT_FONT;
this.fontSize = DEFAULT_FONTSIZE;
this.fontColor = DEFAULT_FONTCOLOR;
this.commandHistory = new ArrayList<>();
}
public void loadConfig() {
try (BufferedReader reader = new BufferedReader(new FileReader(CONFIG_FILE))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (!line.isEmpty() && !line.startsWith("#")) {
String[] parts = line.split("=");
if (parts.length == 2) {
String key = parts[0].trim();
String value = parts[1].trim();
switch (key) {
case "promote":
this.prompt = value;
break;
case "background":
this.background = value;
break;
case "font":
this.font = value;
break;
case "fontsize":
this.fontSize = Integer.parseInt(value);
break;
case "fontcolor":
this.fontColor = value;
break;
}
}
}
}
} catch (IOException e) {
// Ignore if config file is not found or cannot be read
}
}
public void start() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print(prompt);
String command = scanner.nextLine();
commandHistory.add(command);
if (command.equals("exit")) {
break;
}
executeCommand(command);
}
scanner.close();
}
private void executeCommand(String command) {
String[] parts = command.split(" ");
String cmd = parts[0];
switch (cmd) {
case "ls":
// Handle ls command
break;
case "cd":
// Handle cd command
break;
case "mkdir":
// Handle mkdir command
break;
case "rmdir":
// Handle rmdir command
break;
case "rm":
// Handle rm command
break;
case "touch":
// Handle touch command
break;
case "chmod":
// Handle chmod command
break;
case "cat":
// Handle cat command
break;
case "tar":
// Handle tar command
break;
case "history":
printCommandHistory();
break;
case "pwd":
// Handle pwd command
break;
default:
System.out.println("Unknown command: " + cmd);
}
}
private void printCommandHistory() {
for (int i = 0; i < commandHistory.size(); i++) {
System.out.println(i + ": " + commandHistory.get(i));
}
}
public static void main(String[] args) {
MiniShell miniShell = new MiniShell();
miniShell.loadConfig();
miniShell.start();
}
}
原文地址: https://www.cveoy.top/t/topic/pBWU 著作权归作者所有。请勿转载和采集!