mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 11:37:01 +00:00
alias
This commit is contained in:
@@ -187,6 +187,12 @@ namespace CMLeonOS.shell
|
||||
case "testgui":
|
||||
shell.ProcessTestGui();
|
||||
break;
|
||||
case "alias":
|
||||
shell.ProcessAlias(args);
|
||||
break;
|
||||
case "unalias":
|
||||
shell.ProcessUnalias(args);
|
||||
break;
|
||||
default:
|
||||
shell.ShowError($"Unknown command: {command}");
|
||||
break;
|
||||
|
||||
@@ -66,6 +66,9 @@ namespace CMLeonOS.Commands
|
||||
" whoami - Show current username",
|
||||
" base64 encrypt <text> - Encode text to Base64",
|
||||
" base64 decrypt <text> - Decode Base64 to text",
|
||||
" alias <name> <cmd> - Create command alias",
|
||||
" alias - List all aliases",
|
||||
" unalias <name> - Remove command alias",
|
||||
" lua <file> - Execute Lua script",
|
||||
" version - Show OS version",
|
||||
" about - Show about information",
|
||||
|
||||
@@ -1,43 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Sys = Cosmos.System;
|
||||
using System.Drawing;
|
||||
using Cosmos.System.Graphics;
|
||||
using Microsoft.VisualBasic;
|
||||
using Cosmos.System.Graphics.Fonts;
|
||||
using Cosmos.System;
|
||||
using Cosmos.HAL;
|
||||
using Cosmos.System.FileSystem.VFS;
|
||||
using Cosmos.System.FileSystem;
|
||||
using System.IO;
|
||||
|
||||
namespace CMLeonOS.Commands
|
||||
{
|
||||
public static class TestGuiCommand
|
||||
{
|
||||
public static Canvas Screen;
|
||||
public static Bitmap Wallpaper;
|
||||
public static Mode display;
|
||||
|
||||
public static void DrawCursor()
|
||||
{
|
||||
if ((int)MouseManager.X >= 0 && (int)MouseManager.Y >= 0 && (int)MouseManager.X < Screen.Mode.Width && (int)MouseManager.Y < Screen.Mode.Height)
|
||||
{
|
||||
MouseManager.ScreenWidth = Screen.Mode.Width;
|
||||
MouseManager.ScreenHeight = Screen.Mode.Height;
|
||||
Screen.DrawFilledCircle(Color.FromArgb(75, 255, 255, 255), (int)MouseManager.X, (int)MouseManager.Y, 10);
|
||||
}
|
||||
}
|
||||
|
||||
public static void DrawBackground()
|
||||
{
|
||||
Screen.Clear(Color.Indigo);
|
||||
}
|
||||
|
||||
public static void RunTestGui()
|
||||
{
|
||||
Canvas canvas;
|
||||
|
||||
Console.WriteLine("Cosmos booted successfully. Let's go in Graphical Mode");
|
||||
|
||||
canvas = FullScreenCanvas.GetFullScreenCanvas(new Mode(640, 480, ColorDepth.ColorDepth32));
|
||||
|
||||
canvas.Clear(global::System.Drawing.Color.FromArgb(0, 0, 255));
|
||||
|
||||
try
|
||||
{
|
||||
canvas.DrawPoint(global::System.Drawing.Color.FromArgb(255, 0, 0), 69, 69);
|
||||
display = new Mode(1024, 768, ColorDepth.ColorDepth24);
|
||||
Screen = FullScreenCanvas.GetFullScreenCanvas(display);
|
||||
|
||||
canvas.DrawLine(global::System.Drawing.Color.FromArgb(173, 255, 47), 250, 100, 400, 100);
|
||||
try
|
||||
{
|
||||
if (File.Exists(@"0:\system\wallpaper.bmp"))
|
||||
{
|
||||
Wallpaper = new Bitmap(File.ReadAllBytes(@"0:\system\wallpaper.bmp"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Wallpaper = null;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Wallpaper = null;
|
||||
}
|
||||
|
||||
canvas.DrawLine(global::System.Drawing.Color.FromArgb(205, 92, 92), 350, 150, 350, 250);
|
||||
global::System.Console.WriteLine("Starting graphical mode...");
|
||||
global::System.Console.WriteLine("Press ESC to return to shell.");
|
||||
|
||||
canvas.DrawLine(global::System.Drawing.Color.FromArgb(245, 245, 220), 250, 150, 400, 250);
|
||||
while (true)
|
||||
{
|
||||
Screen.Clear();
|
||||
DrawBackground();
|
||||
Screen.DrawString(DateTime.Now.ToString("H:mm") + ", Screen:" + Screen.Mode.Width + "x" + Screen.Mode.Height, PCScreenFont.Default, Color.White, 15, 10);
|
||||
DrawCursor();
|
||||
Screen.Display();
|
||||
|
||||
canvas.DrawRectangle(global::System.Drawing.Color.FromArgb(219, 112, 147), 350, 350, 80, 60);
|
||||
|
||||
canvas.DrawRectangle(global::System.Drawing.Color.FromArgb(50, 205, 50), 450, 450, 80, 60);
|
||||
|
||||
canvas.Display();
|
||||
|
||||
Console.WriteLine("Press any key to return to shell...");
|
||||
Console.ReadKey(true);
|
||||
var key = global::System.Console.ReadKey(true);
|
||||
if (key.Key == ConsoleKey.Escape)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Exception occurred: " + e.Message);
|
||||
global::System.Console.WriteLine("Exception occurred: " + e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
158
shell/Commands/Utility/AliasCommand.cs
Normal file
158
shell/Commands/Utility/AliasCommand.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace CMLeonOS.Commands
|
||||
{
|
||||
public static class AliasCommand
|
||||
{
|
||||
private static string aliasFilePath = @"0:\system\alias.dat";
|
||||
private static Dictionary<string, string> aliases = new Dictionary<string, string>();
|
||||
|
||||
public static void LoadAliases()
|
||||
{
|
||||
aliases.Clear();
|
||||
|
||||
try
|
||||
{
|
||||
if (File.Exists(aliasFilePath))
|
||||
{
|
||||
string[] lines = File.ReadAllLines(aliasFilePath);
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#"))
|
||||
{
|
||||
int separatorIndex = line.IndexOf('=');
|
||||
if (separatorIndex > 0)
|
||||
{
|
||||
string name = line.Substring(0, separatorIndex).Trim();
|
||||
string value = line.Substring(separatorIndex + 1).Trim();
|
||||
aliases[name] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Error loading aliases: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void SaveAliases()
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(aliasFilePath));
|
||||
|
||||
using (StreamWriter writer = new StreamWriter(aliasFilePath))
|
||||
{
|
||||
writer.WriteLine("# CMLeonOS Alias Configuration");
|
||||
writer.WriteLine("# Format: alias_name=command");
|
||||
writer.WriteLine();
|
||||
|
||||
var keys = new List<string>(aliases.Keys);
|
||||
|
||||
for (int i = 0; i < keys.Count - 1; i++)
|
||||
{
|
||||
for (int j = i + 1; j < keys.Count; j++)
|
||||
{
|
||||
if (string.Compare(keys[i], keys[j]) > 0)
|
||||
{
|
||||
string temp = keys[i];
|
||||
keys[i] = keys[j];
|
||||
keys[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (string key in keys)
|
||||
{
|
||||
writer.WriteLine($"{key}={aliases[key]}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Error saving aliases: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void AddAlias(string name, string command)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(command))
|
||||
{
|
||||
Console.WriteLine("Error: Alias name and command cannot be empty");
|
||||
return;
|
||||
}
|
||||
|
||||
aliases[name] = command;
|
||||
SaveAliases();
|
||||
Console.WriteLine($"Alias '{name}' added successfully");
|
||||
}
|
||||
|
||||
public static void RemoveAlias(string name)
|
||||
{
|
||||
if (aliases.Remove(name))
|
||||
{
|
||||
SaveAliases();
|
||||
Console.WriteLine($"Alias '{name}' removed successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Error: Alias '{name}' not found");
|
||||
}
|
||||
}
|
||||
|
||||
public static void ListAliases()
|
||||
{
|
||||
if (aliases.Count == 0)
|
||||
{
|
||||
Console.WriteLine("No aliases defined");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("Defined aliases:");
|
||||
var keys = new List<string>(aliases.Keys);
|
||||
|
||||
for (int i = 0; i < keys.Count - 1; i++)
|
||||
{
|
||||
for (int j = i + 1; j < keys.Count; j++)
|
||||
{
|
||||
if (string.Compare(keys[i], keys[j]) > 0)
|
||||
{
|
||||
string temp = keys[i];
|
||||
keys[i] = keys[j];
|
||||
keys[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (string key in keys)
|
||||
{
|
||||
Console.WriteLine($" {key} => {aliases[key]}");
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetAlias(string name)
|
||||
{
|
||||
if (aliases.TryGetValue(name, out string value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool AliasExists(string name)
|
||||
{
|
||||
return aliases.ContainsKey(name);
|
||||
}
|
||||
|
||||
public static void ClearAliases()
|
||||
{
|
||||
aliases.Clear();
|
||||
SaveAliases();
|
||||
Console.WriteLine("All aliases cleared");
|
||||
}
|
||||
}
|
||||
}
|
||||
103
shell/Shell.cs
103
shell/Shell.cs
@@ -57,6 +57,8 @@ namespace CMLeonOS
|
||||
fileSystem = new FileSystem();
|
||||
fixMode = Kernel.FixMode;
|
||||
envManager = EnvironmentVariableManager.Instance;
|
||||
|
||||
Commands.AliasCommand.LoadAliases();
|
||||
}
|
||||
|
||||
public void Run()
|
||||
@@ -110,7 +112,28 @@ namespace CMLeonOS
|
||||
|
||||
private void ProcessCommand(string command, string args)
|
||||
{
|
||||
shell.CommandList.ProcessCommand(this, command, args);
|
||||
string expandedCommand = command;
|
||||
string expandedArgs = args;
|
||||
|
||||
string aliasValue = Commands.AliasCommand.GetAlias(command);
|
||||
if (aliasValue != null)
|
||||
{
|
||||
var aliasParts = aliasValue.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (aliasParts.Length > 0)
|
||||
{
|
||||
expandedCommand = aliasParts[0];
|
||||
if (aliasParts.Length > 1)
|
||||
{
|
||||
expandedArgs = aliasParts[1] + (string.IsNullOrEmpty(args) ? "" : " " + args);
|
||||
}
|
||||
else
|
||||
{
|
||||
expandedArgs = args;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shell.CommandList.ProcessCommand(this, expandedCommand, expandedArgs);
|
||||
}
|
||||
|
||||
public void ProcessEcho(string args)
|
||||
@@ -1221,6 +1244,84 @@ namespace CMLeonOS
|
||||
Commands.TestGuiCommand.RunTestGui();
|
||||
}
|
||||
|
||||
public void ProcessAlias(string args)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(args))
|
||||
{
|
||||
Commands.AliasCommand.ListAliases();
|
||||
}
|
||||
else
|
||||
{
|
||||
string name = "";
|
||||
string command = "";
|
||||
|
||||
int i = 0;
|
||||
while (i < args.Length && char.IsWhiteSpace(args[i]))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
int nameStart = i;
|
||||
while (i < args.Length && !char.IsWhiteSpace(args[i]))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
name = args.Substring(nameStart, i - nameStart).Trim();
|
||||
|
||||
while (i < args.Length && char.IsWhiteSpace(args[i]))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i < args.Length && (args[i] == '\'' || args[i] == '"'))
|
||||
{
|
||||
char quoteChar = args[i];
|
||||
i++;
|
||||
int commandStart = i;
|
||||
while (i < args.Length && args[i] != quoteChar)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
if (i < args.Length)
|
||||
{
|
||||
command = args.Substring(commandStart, i - commandStart);
|
||||
}
|
||||
else
|
||||
{
|
||||
command = args.Substring(commandStart);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
command = args.Substring(i).Trim();
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(command))
|
||||
{
|
||||
ShowError("Usage: alias <name> <command>");
|
||||
ShowError("Example: alias ll 'ls -l'");
|
||||
ShowError("Example: alias home \"cd /home\"");
|
||||
ShowError("Example: alias cls clear");
|
||||
return;
|
||||
}
|
||||
|
||||
Commands.AliasCommand.AddAlias(name, command);
|
||||
}
|
||||
}
|
||||
|
||||
public void ProcessUnalias(string args)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(args))
|
||||
{
|
||||
ShowError("Usage: unalias <name>");
|
||||
ShowError("Example: unalias ll");
|
||||
return;
|
||||
}
|
||||
|
||||
string name = args.Trim();
|
||||
Commands.AliasCommand.RemoveAlias(name);
|
||||
}
|
||||
|
||||
public void SetDnsServer(string args)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(args))
|
||||
|
||||
Reference in New Issue
Block a user