拆分代码4

This commit is contained in:
2026-02-04 20:13:21 +08:00
parent b5ecee05b7
commit 3cc6d2c92a
20 changed files with 676 additions and 456 deletions

View File

@@ -0,0 +1,19 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class CatCommand
{
public static void ProcessCat(CMLeonOS.FileSystem fileSystem, string args, Action<string> showError)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify a file name");
}
else
{
Console.WriteLine(fileSystem.ReadFile(args));
}
}
}
}

View File

@@ -0,0 +1,10 @@
namespace CMLeonOS.Commands.FileSystem
{
public static class CdCommand
{
public static void ProcessCd(CMLeonOS.FileSystem fileSystem, string args)
{
fileSystem.ChangeDirectory(args);
}
}
}

View File

@@ -0,0 +1,45 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class CopyCommand
{
public static void CopyFile(CMLeonOS.FileSystem fileSystem, string args, Action<string> showError, Action<string> showSuccess)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify source and destination files");
showError("cp <source> <destination>");
return;
}
try
{
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
{
showError("Please specify both source and destination");
showError("cp <source> <destination>");
return;
}
string sourceFile = parts[0];
string destFile = parts[1];
string content = fileSystem.ReadFile(sourceFile);
if (content == null)
{
showError($"Source file '{sourceFile}' does not exist");
return;
}
fileSystem.WriteFile(destFile, content);
showSuccess($"File copied successfully from '{sourceFile}' to '{destFile}'");
}
catch (Exception ex)
{
showError($"Error copying file: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
using Sys = Cosmos.System;
namespace CMLeonOS.Commands.FileSystem
{
public static class DiskInfoCommand
{
public static void GetDiskInfo(Action<string> showError)
{
Console.WriteLine("====================================");
Console.WriteLine(" Disk Information");
Console.WriteLine("====================================");
try
{
var disks = Sys.FileSystem.VFS.VFSManager.GetDisks();
if (disks == null || disks.Count == 0)
{
Console.WriteLine("No disks found.");
return;
}
Console.WriteLine($"Total Disks: {disks.Count}");
}
catch (Exception ex)
{
showError($"Error getting disk info: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class FindCommand
{
public static void FindFile(CMLeonOS.FileSystem fileSystem, string args, Action<string> showError)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify a file name to search");
showError("find <filename>");
return;
}
try
{
var files = fileSystem.GetFileList(".");
bool found = false;
foreach (var file in files)
{
if (file.ToLower().Contains(args.ToLower()))
{
Console.WriteLine($"Found: {file}");
found = true;
}
}
if (!found)
{
Console.WriteLine($"No files found matching '{args}'");
}
}
catch (Exception ex)
{
showError($"Error finding file: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,53 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class HeadCommand
{
public static void HeadFile(CMLeonOS.FileSystem fileSystem, string args, Action<string> showError)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify a file name");
return;
}
try
{
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string fileName = parts[0];
int lineCount = 10;
if (parts.Length > 1)
{
if (int.TryParse(parts[1], out int count))
{
lineCount = count;
}
}
string content = fileSystem.ReadFile(fileName);
if (string.IsNullOrEmpty(content))
{
Console.WriteLine("File is empty");
return;
}
string[] lines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
int displayLines = Math.Min(lineCount, lines.Length);
Console.WriteLine($"First {displayLines} lines of {fileName}:");
Console.WriteLine("--------------------------------");
for (int i = 0; i < displayLines; i++)
{
Console.WriteLine($"{i + 1}: {lines[i]}");
}
}
catch (Exception ex)
{
showError($"Error reading file: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,10 @@
namespace CMLeonOS.Commands.FileSystem
{
public static class LsCommand
{
public static void ProcessLs(CMLeonOS.FileSystem fileSystem, string args)
{
fileSystem.ListFiles(args);
}
}
}

View File

@@ -0,0 +1,21 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class MkdirCommand
{
public static void ProcessMkdir(CMLeonOS.FileSystem fileSystem, string args)
{
if (string.IsNullOrEmpty(args))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Please specify a directory name");
Console.ResetColor();
}
else
{
fileSystem.MakeDirectory(args);
}
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class MoveCommand
{
public static void MoveFile(CMLeonOS.FileSystem fileSystem, string args, Action<string> showError, Action<string> showSuccess)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify source and destination files");
showError("mv <source> <destination>");
return;
}
try
{
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
{
showError("Please specify both source and destination");
showError("mv <source> <destination>");
return;
}
string sourceFile = parts[0];
string destFile = parts[1];
string content = fileSystem.ReadFile(sourceFile);
if (content == null)
{
showError($"Source file '{sourceFile}' does not exist");
return;
}
fileSystem.WriteFile(destFile, content);
fileSystem.DeleteFile(sourceFile);
showSuccess($"File moved/renamed successfully from '{sourceFile}' to '{destFile}'");
}
catch (Exception ex)
{
showError($"Error moving file: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class PwdCommand
{
public static void ProcessPwd(CMLeonOS.FileSystem fileSystem)
{
Console.WriteLine(fileSystem.CurrentDirectory);
}
}
}

View File

@@ -0,0 +1,56 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class RenameCommand
{
public static void RenameFile(CMLeonOS.FileSystem fileSystem, string args, Action<string> showError, Action<string> showSuccess)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify source and new name");
showError("rename <source> <newname>");
return;
}
try
{
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
{
showError("Please specify both source and new name");
showError("rename <source> <newname>");
return;
}
string sourceFile = parts[0];
string newName = parts[1];
string sourcePath = fileSystem.GetFullPath(sourceFile);
string destPath = fileSystem.GetFullPath(newName);
if (!System.IO.File.Exists(sourcePath))
{
showError($"Source file '{sourceFile}' does not exist");
return;
}
if (System.IO.File.Exists(destPath))
{
showError($"Destination '{newName}' already exists");
return;
}
string content = fileSystem.ReadFile(sourcePath);
System.IO.File.WriteAllText(destPath, content);
fileSystem.DeleteFile(sourcePath);
showSuccess($"File renamed successfully from '{sourceFile}' to '{newName}'");
}
catch (Exception ex)
{
showError($"Error renaming file: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,46 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class RmCommand
{
public static void ProcessRm(CMLeonOS.FileSystem fileSystem, string args, bool fixMode, Action<string> showError)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify a file name");
}
else
{
bool isInSysFolder = (args.Contains(@"\system\") || args.Contains(@"/sys/")) && !fixMode;
if (isInSysFolder)
{
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
bool hasNorisk = false;
string filePath = args;
if (parts.Length > 1)
{
hasNorisk = Array.IndexOf(parts, "-norisk") >= 0;
filePath = parts[0];
}
if (!hasNorisk)
{
showError("Cannot delete files in sys folder without -norisk parameter");
showError("Usage: rm <file> -norisk");
}
else
{
fileSystem.DeleteFile(filePath);
}
}
else
{
fileSystem.DeleteFile(args);
}
}
}
}
}

View File

@@ -0,0 +1,29 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class RmdirCommand
{
public static void ProcessRmdir(CMLeonOS.FileSystem fileSystem, string args, bool fixMode, Action<string> showError)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify a directory name");
}
else
{
bool isInSysFolder = (args.Contains(@"\system\") || args.Contains(@"/sys/")) && !fixMode;
if (isInSysFolder)
{
showError("Cannot delete directories in sys folder");
showError("Use fix mode to bypass this restriction");
}
else
{
fileSystem.DeleteDirectory(args);
}
}
}
}
}

View File

@@ -0,0 +1,54 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class TailCommand
{
public static void TailFile(CMLeonOS.FileSystem fileSystem, string args, Action<string> showError)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify a file name");
return;
}
try
{
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string fileName = parts[0];
int lineCount = 10;
if (parts.Length > 1)
{
if (int.TryParse(parts[1], out int count))
{
lineCount = count;
}
}
string content = fileSystem.ReadFile(fileName);
if (string.IsNullOrEmpty(content))
{
Console.WriteLine("File is empty");
return;
}
string[] lines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
int displayLines = Math.Min(lineCount, lines.Length);
int startLine = Math.Max(0, lines.Length - displayLines);
Console.WriteLine($"Last {displayLines} lines of {fileName}:");
Console.WriteLine("--------------------------------");
for (int i = startLine; i < lines.Length; i++)
{
Console.WriteLine($"{i + 1}: {lines[i]}");
}
}
catch (Exception ex)
{
showError($"Error reading file: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,27 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class TouchCommand
{
public static void CreateEmptyFile(CMLeonOS.FileSystem fileSystem, string args, Action<string> showError, Action<string> showSuccess)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify a file name");
showError("touch <filename>");
return;
}
try
{
fileSystem.WriteFile(args, "");
showSuccess($"Empty file '{args}' created successfully");
}
catch (Exception ex)
{
showError($"Error creating file: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,77 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class TreeCommand
{
public static void ShowTree(CMLeonOS.FileSystem fileSystem, string args, Action<string> showError)
{
string startPath = string.IsNullOrEmpty(args) ? "." : args;
string fullPath = fileSystem.GetFullPath(startPath);
if (!System.IO.Directory.Exists(fullPath))
{
showError($"Directory not found: {startPath}");
return;
}
try
{
Console.WriteLine(fullPath);
PrintDirectoryTree(fileSystem, fullPath, "", true, showError);
}
catch (Exception ex)
{
showError($"Error displaying tree: {ex.Message}");
}
}
private static void PrintDirectoryTree(CMLeonOS.FileSystem fileSystem, string path, string prefix, bool isLast, Action<string> showError)
{
try
{
var dirs = fileSystem.GetFullPathDirectoryList(path);
var files = fileSystem.GetFullPathFileList(path);
int totalItems = dirs.Count + files.Count;
int current = 0;
foreach (var dir in dirs)
{
current++;
bool isLastItem = current == totalItems;
string connector = isLastItem ? "+-- " : "|-- ";
string newPrefix = prefix + (isLastItem ? " " : "| ");
string dirName = System.IO.Path.GetFileName(dir);
Console.WriteLine($"{prefix}{connector}{dirName}/");
PrintDirectoryTree(fileSystem, dir, newPrefix, isLastItem, showError);
}
foreach (var file in files)
{
current++;
bool isLastItem = current == totalItems;
string connector = isLastItem ? "+-- " : "|-- ";
string fileName = System.IO.Path.GetFileName(file);
Console.WriteLine($"{prefix}{connector}{fileName}");
}
}
catch (System.IO.DirectoryNotFoundException)
{
showError($"Directory not found: {path}");
}
catch (Exception ex)
{
string errorMsg = ex.Message;
if (string.IsNullOrEmpty(errorMsg))
{
errorMsg = $"Error type: {ex.GetType().Name}";
}
showError($"Error reading directory {path}: {errorMsg}");
}
}
}
}

View File

@@ -0,0 +1,50 @@
using System;
namespace CMLeonOS.Commands.FileSystem
{
public static class WordCountCommand
{
public static void WordCount(CMLeonOS.FileSystem fileSystem, string args, Action<string> showError)
{
if (string.IsNullOrEmpty(args))
{
showError("Please specify a file name");
return;
}
try
{
string content = fileSystem.ReadFile(args);
if (string.IsNullOrEmpty(content))
{
Console.WriteLine("File is empty");
return;
}
string[] lines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
int lineCount = lines.Length;
int wordCount = 0;
int charCount = content.Length;
foreach (string line in lines)
{
if (!string.IsNullOrWhiteSpace(line))
{
string[] words = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
wordCount += words.Length;
}
}
Console.WriteLine($"Word count for {args}:");
Console.WriteLine("--------------------------------");
Console.WriteLine($"Lines: {lineCount}");
Console.WriteLine($"Words: {wordCount}");
Console.WriteLine($"Characters: {charCount}");
}
catch (Exception ex)
{
showError($"Error reading file: {ex.Message}");
}
}
}
}