Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views5 pages

Message

This document contains code for a ConsoleCommands class that handles commands entered in a console. It checks the first word of the command and calls different methods depending on the command, such as starting a calculator, clock, calendar, or showing help. It also contains commands to display system information, format disks, check free space, list directories and files, write text to files, display file contents, delete files, and create directories.

Uploaded by

janglowacz53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Message

This document contains code for a ConsoleCommands class that handles commands entered in a console. It checks the first word of the command and calls different methods depending on the command, such as starting a calculator, clock, calendar, or showing help. It also contains commands to display system information, format disks, check free space, list directories and files, write text to files, display file contents, delete files, and create directories.

Uploaded by

janglowacz53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

using System.

Globalization;

namespace JachowskiOS.System
{
public static class ConsoleCommands
{
public static void RunCommand(string command)
{
string[] words = command.Split(' ');
if (words.Length > 0)
{
if (words[0] == "calculator")
{
Calculator.RunCalculator();
}

if (words[0].ToLower() == "clock")
{
Clock.Start();
return;
}
else if (words[0].ToLower() == "time")
{
ShowTime();
return;
}
}
else if (words[0] == "time")
{
Clock.Start();
return;
}
else if (words[0].ToLower() == "calendar")
{
DateTime currentDate = DateTime.Now;
int currentYear = currentDate.Year;
int currentMonth = currentDate.Month;
Calendar.ShowCalendar(currentYear, currentMonth);
return;
}
else if (words[0] == "info")
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(WriteMessage.CenterText("JachowskiOS"));
Console.WriteLine(WriteMessage.CenterText(Kernel.Version));
Console.WriteLine(WriteMessage.CenterText("Created by
Jachowski System "));
Console.ForegroundColor = ConsoleColor.White;

}
else if (words[0] == "help")
{
Console.WriteLine("Available commands:");
Console.WriteLine("- info: Display system information");
Console.WriteLine("- format: Format the disk");
Console.WriteLine("- space: Check available free space");
Console.WriteLine("- dir: List directories and files in the
current directory");
Console.WriteLine("- echo <text> > <file>: Write text to a
file");
Console.WriteLine("- cat <file>: Display the contents of a
file");
Console.WriteLine("- del <file>: Delete a file");
Console.WriteLine("- mkdir <directory>: Create a new
directory");
Console.WriteLine("- time: see actual time");
Console.WriteLine("- clock: stoper");
Console.WriteLine("- c");
}
else if (words[0] == "format")
{
if (Kernel.fs.Disks[0].Partitions.Count > 0)
{
Kernel.fs.Disks[0].DeletePartition(0);
}
Kernel.fs.Disks[0].Clear();

Kernel.fs.Disks[0].CreatePartition(Kernel.fs.Disks[0].Size / (1024 * 1024));


Kernel.fs.Disks[0].FormatPartition(0, "FAT32", true);
WriteMessage.WriteOK("Succes!");
WriteMessage.WriteWarn("JachowskiOS will reboot in 3
seconds!");
Thread.Sleep(3000);
Cosmos.System.Power.Reboot();
}
else if (words[0] == "space")
{
long free = Kernel.fs.GetAvailableFreeSpace(Kernel.Path);
Console.Write("Free space: " + free / 1024 + "kb");
}
if (words[0].ToLower() == "notepad")
{
if (words.Length > 1)
{
string fileName = words[1];
Notepad.OpenOrCreateFile(fileName);
}
else
{
WriteMessage.WriteError("Please provide a
filename.");
}
return;
}
else if (words[0] == "dir")
{
var Directories = Directory.GetDirectories(Kernel.Path);
var Files = Directory.GetFiles(Kernel.Path);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Directories (" + Directories.Length +
")");
Console.ForegroundColor = ConsoleColor.Gray;
for (int i = 0; i < Directories.Length; i++)
{
Console.WriteLine(Directories[i]);
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Files (" + Files.Length + ")");
Console.ForegroundColor = ConsoleColor.Gray;
for (int i = 0; i < Files.Length; i++)
{
Console.WriteLine(Files[i]);
}
}
else if (words[0] == "echo")
{
if (words.Length > 1)
{
string wholeString = "";
for (int i = 1; i < words.Length; i++)
{
wholeString += words[i] + " ";
}
int pathIndex = wholeString.LastIndexOf('>');
String text = wholeString.Substring(0, pathIndex);
String path = wholeString.Substring(pathIndex + 1);
if (!path.Contains(@"\"))
path = Kernel.Path + path;
if (path.EndsWith(' '))
{
path = path.Substring(0, path.Length - 1);
}
var file_stream = File.Create(path);
file_stream.Close();
File.WriteAllText(path, text);
}
else

WriteMessage.WriteError("Invalid Syntax!");
}
else if (words[0] == "cat")//Wypisujemy zawartość pliku
{
if (words.Length > 1)
{
string path = words[1];
if (!path.Contains(@"\"))
path = Kernel.Path + path;
if (path.EndsWith(' '))
{
path = path.Substring(0, path.Length - 1);
}
if (File.Exists(path))
{
string text = File.ReadAllText(path);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(text);
}
else
WriteMessage.WriteError("File " + path + " not
found!");
}
else
WriteMessage.WriteError("Invalid Syntax!");

}
else if (words[0] == "del")//Usuwamy plik
{
if (words.Length > 1)
{
string path = words[1];
if (!path.Contains(@"\"))
path = Kernel.Path + path;
if (path.EndsWith(' '))
{
path = path.Substring(0, path.Length - 1);
}
if (File.Exists(path))
{
File.Delete(path);
WriteMessage.WriteOK("Deleted " + path + "!");
}
else
WriteMessage.WriteError("File " + path + " not
found!");
}
else
WriteMessage.WriteError("Invalid Syntax!");
}
else if (words[0] == "mkdir")//Tworzymy folder
{
if (words.Length > 1)
{
string path = words[1];
if (!path.Contains(@"\"))
path = Kernel.Path + path;
if (path.EndsWith(' '))
{
path = path.Substring(0, path.Length - 1);
}
Directory.CreateDirectory(path);
}
else
WriteMessage.WriteError("Invalid Syntax!");
}
else if (words[0] == "cd")//Zmieniamy Kernel.Path
{
if (words.Length > 1)
{
if (words[1] == "..")
{
if (Kernel.Path != @"0:\")
{
string tempPath =
Kernel.Path.Substring(0, Kernel.Path.Length - 1);
Kernel.Path = tempPath.Substring(0,
tempPath.LastIndexOf(@"\") + 1);
return;
}
else
return;
}
string path = words[1];
if (!path.Contains(@"\"))
path = Kernel.Path + path + @"\";
if (path.EndsWith(' '))
{
path = path.Substring(0, path.Length - 1);
}
if (!path.EndsWith(@"\"))
path += @"\";
if (Directory.Exists(path))
Kernel.Path = path;
else
WriteMessage.WriteError("Directory " + path + "
not found!");
}
else
Kernel.Path = @"0:\";
}
else
{
WriteMessage.WriteError("Please enter a valid command!");
}

}
}
}

You might also like