Редагування консолі тексту
创建于:2025年1月10日
创建于:2025年1月10日
To modify your code to have a blue background, set the title to "TXT Editor," and display additional instructions like "Esc = Close the application," "Ctrl + S = Save TXT File," and "Enter = New line," you'll need to make a few adjustments in the BeforeRun
and Run
methods, as well as update the console settings.
Here's the modified code:
using System; using Sys = Cosmos.System; using System.Collections.Generic; namespace TLSPOS { public class Kernel : Sys.Kernel { // Словник для збереження файлів у пам'яті private Dictionary<string, string> virtualFileSystem = new Dictionary<string, string>(); protected override void BeforeRun() { // Clear the screen and set background and text color Console.Clear(); Console.BackgroundColor = ConsoleColor.Blue; Console.ForegroundColor = ConsoleColor.White; Console.Clear(); // Apply the background color change Console.Title = "TXT Editor"; // Show welcome message and instructions Console.WriteLine("Welcome to TXT Editor!"); Console.WriteLine("Esc = Close the application"); Console.WriteLine("Ctrl + S = Save TXT File"); Console.WriteLine("Enter = New line"); Console.WriteLine("Type 'help' for a list of commands."); } protected override void Run() { Console.Write("TXT Editor> "); string input = Console.ReadLine(); if (!string.IsNullOrEmpty(input)) { ProcessCommand(input); } } private void ProcessCommand(string input) { string[] parts = input.Split(' '); switch (parts[0].ToLower()) { case "help": ShowHelp(); break; case "echo": EchoCommand(); break; case "calc": Calculator(); break; case "createf": if (parts.Length > 1) CreateFolder(parts[1]); else Console.WriteLine("Please specify the folder name."); break; case "addtxt": if (parts.Length > 1) CreateTextFile(parts[1]); else Console.WriteLine("Please specify the file name."); break; case "edit": if (parts.Length > 1) EditTextFile(parts[1]); else Console.WriteLine("Please specify the file name."); break; case "dir": ListFiles(); break; case "clear": Console.Clear(); break; default: Console.WriteLine("Unknown command. Type 'help' for a list of commands."); break; } } private void ShowHelp() { Console.WriteLine("Available commands:"); Console.WriteLine(" help - Show this help message"); Console.WriteLine(" echo - Repeat your input"); Console.WriteLine(" calc - Simple calculator"); Console.WriteLine(" createf - Create a folder in memory"); Console.WriteLine(" addtxt - Create a text file in memory"); Console.WriteLine(" edit - Edit a text file in memory"); Console.WriteLine(" dir - List files in memory"); Console.WriteLine(" clear - Clear the screen"); } private void EchoCommand() { Console.Write("Enter text to echo: "); string text = Console.ReadLine(); Console.WriteLine($"You typed: {text}"); } private void Calculator() { try { Console.Write("Enter first number: "); int num1 = int.Parse(Console.ReadLine()); Console.Write("Enter operator (+, -, *, /): "); string op = Console.ReadLine(); Console.Write("Enter second number: "); int num2 = int.Parse(Console.ReadLine()); int result = op switch { "+" => num1 + num2, "-" => num1 - num2, "*" => num1 * num2, "/" => num2 != 0 ? num1 / num2 : throw new DivideByZeroException(), _ => throw new InvalidOperationException("Invalid operator") }; Console.WriteLine($"Result: {result}"); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } private void CreateFolder(string folderName) { string path = folderName.ToLower(); if (!virtualFileSystem.ContainsKey(path)) { virtualFileSystem[path] = null; // Папки не мають вмісту Console.WriteLine($"Folder '{folderName}' created successfully in memory."); } else { Console.WriteLine($"Folder '{folderName}' already exists in memory."); } } private void CreateTextFile(string fileName) { string path = fileName.ToLower(); if (!virtualFileSystem.ContainsKey(path)) { virtualFileSystem[path] = "This is a new text file."; Console.WriteLine($"Text file '{fileName}' created successfully in memory."); } else { Console.WriteLine($"Text file '{fileName}' already exists in memory."); } } private void EditTextFile(string fileName) { string path = fileName.ToLower(); if (virtualFileSystem.ContainsKey(path)) { Console.WriteLine($"Editing file: {fileName}"); Console.WriteLine($"Current content: {virtualFileSystem[path]}"); Console.Write("Enter new content: "); string newContent = Console.ReadLine(); virtualFileSystem[path] = newContent; Console.WriteLine($"File '{fileName}' updated successfully in memory."); } else { Console.WriteLine($"File '{fileName}' does not exist in memory."); } } private void ListFiles() { if (virtualFileSystem.Count == 0) { Console.WriteLine("No files or folders in memory."); } else { Console.WriteLine("Files and folders in memory:"); foreach (var item in virtualFileSystem) { Console.WriteLine($" {item.Key} {(item.Value == null ? "(Folder)" : "(File)")} "); } } } } }
Console.BackgroundColor
and Console.ForegroundColor
.Console.Title
.Console.Clear()
is called after changing the background color to make sure the screen is cleared properly.Esc
key or saving a file with Ctrl + S
, you'll need to add key event handling, which isn't directly supported in a simple Console.ReadLine()
environment. You might need a more advanced input method (like Console.ReadKey()
or custom key listening) for this to work.Let me know if you need further assistance!
To add the dirf
command that changes the current directory to the specified folder (if it exists), you need to maintain the current directory state in your program and update it when a valid directory is specified. Below is an updated version of your code with the dirf
command functionality added.
currentDirectory
variable to track the current directory.dirf
Command: This command allows the user to change the current directory to the specified folder (if it exists).ListFiles()
: Modified ListFiles()
to show the files within the current directory.using System; using Sys = Cosmos.System; using System.Collections.Generic; namespace TLSPOS { public class Kernel : Sys.Kernel { // Словник для збереження файлів у пам'яті private Dictionary<string, string> virtualFileSystem = new Dictionary<string, string>(); // Variable to keep track of the current directory private string currentDirectory = "/"; // Root directory initially protected override void BeforeRun() { Console.Clear(); Console.WriteLine("Welcome to TL&SPOS!"); Console.WriteLine("Type 'help' for a list of commands."); } protected override void Run() { Console.Write("TLSPOS> "); string input = Console.ReadLine(); if (!string.IsNullOrEmpty(input)) { ProcessCommand(input); } } private void ProcessCommand(string input) { string[] parts = input.Split(' '); switch (parts[0].ToLower()) { case "help": ShowHelp(); break; case "echo": EchoCommand(); break; case "calc": Calculator(); break; case "createf": if (parts.Length > 1) CreateFolder(parts[1]); else Console.WriteLine("Please specify the folder name."); break; case "addtxt": if (parts.Length > 1) CreateTextFile(parts[1]); else Console.WriteLine("Please specify the file name."); break; case "edit": if (parts.Length > 1) EditTextFile(parts[1]); else Console.WriteLine("Please specify the file name."); break; case "dir": ListFiles(); break; case "clear": Console.Clear(); break; case "dirf": if (parts.Length > 1) ChangeDirectory(parts[1]); else Console.WriteLine("Please specify the folder name."); break; default: Console.WriteLine("Unknown command. Type 'help' for a list of commands."); break; } } private void ShowHelp() { Console.WriteLine("Available commands:"); Console.WriteLine(" help - Show this help message"); Console.WriteLine(" echo - Repeat your input"); Console.WriteLine(" calc - Simple calculator"); Console.WriteLine(" createf - Create a folder in memory"); Console.WriteLine(" addtxt - Create a text file in memory"); Console.WriteLine(" edit - Edit a text file in memory"); Console.WriteLine(" dir - List files in memory"); Console.WriteLine(" dirf - Change current directory"); Console.WriteLine(" clear - Clear the screen"); } private void EchoCommand() { Console.Write("Enter text to echo: "); string text = Console.ReadLine(); Console.WriteLine($"You typed: {text}"); } private void Calculator() { try { Console.Write("Enter first number: "); int num1 = int.Parse(Console.ReadLine()); Console.Write("Enter operator (+, -, *, /): "); string op = Console.ReadLine(); Console.Write("Enter second number: "); int num2 = int.Parse(Console.ReadLine()); int result = op switch { "+" => num1 + num2, "-" => num1 - num2, "*" => num1 * num2, "/" => num2 != 0 ? num1 / num2 : throw new DivideByZeroException(), _ => throw new InvalidOperationException("Invalid operator") }; Console.WriteLine($"Result: {result}"); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } private void CreateFolder(string folderName) { string path = folderName.ToLower(); if (!virtualFileSystem.ContainsKey(path)) { virtualFileSystem[path] = null; // Папки не мають вмісту Console.WriteLine($"Folder '{folderName}' created successfully in memory."); } else { Console.WriteLine($"Folder '{folderName}' already exists in memory."); } } private void CreateTextFile(string fileName) { string path = fileName.ToLower(); if (!virtualFileSystem.ContainsKey(path)) { virtualFileSystem[path] = "This is a new text file."; Console.WriteLine($"Text file '{fileName}' created successfully in memory."); } else { Console.WriteLine($"Text file '{fileName}' already exists in memory."); } } private void EditTextFile(string fileName) { string path = fileName.ToLower(); if (virtualFileSystem.ContainsKey(path)) { Console.WriteLine($"Editing file: {fileName}"); Console.WriteLine($"Current content: {virtualFileSystem[path]}"); Console.Write("Enter new content: "); string newContent = Console.ReadLine(); virtualFileSystem[path] = newContent; Console.WriteLine($"File '{fileName}' updated successfully in memory."); } else { Console.WriteLine($"File '{fileName}' does not exist in memory."); } } private void ListFiles() { Console.WriteLine($"Current directory: {currentDirectory}"); // Display files and folders in the current directory bool foundFiles = false; foreach (var item in virtualFileSystem) { // Simple assumption: all folders are direct entries in the file system if (item.Key.StartsWith(currentDirectory)) { foundFiles = true; Console.WriteLine($" {item.Key} {(item.Value == null ? "(Folder)" : "(File)")}"); } } if (!foundFiles) { Console.WriteLine("No files or folders in this directory."); } } private void ChangeDirectory(string folderName) { string path = folderName.ToLower(); if (virtualFileSystem.ContainsKey(path) && virtualFileSystem[path] == null) { currentDirectory = path; Console.WriteLine($"Changed current directory to: {currentDirectory}"); } else { Console.WriteLine($"Folder '{folderName}' does not exist."); } } } }
Current Directory Tracking:
currentDirectory
variable to track the current working directory."/"
) is the starting point.New dirf
Command:
virtualFileSystem
and sets currentDirectory
to the new path.ListFiles()
Method:
currentDirectory
. It filters out entries that don’t start with the current directory path.Changing the Directory:
dirf folderName
will change the directory to the specified folder (if it exists).Listing Files in Current Directory:
dir
command to list files and folders in the current directory.Let me know if you need any further adjustments!