Skip to content

Commit e55bbd9

Browse files
authored
Merge pull request #32 from onihilist/fix/plugin-loader
2 parents 653dd47 + f4ca4ee commit e55bbd9

25 files changed

Lines changed: 1317 additions & 81 deletions

Commands/AboutCommand.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using NShell.Shell;
2+
using NShell.Shell.Commands;
3+
using Spectre.Console;
4+
5+
namespace NShell.Commands;
6+
7+
public class AboutCommand : ICustomCommand, IMetadataCommand
8+
{
9+
public string Name => "about";
10+
public string Description => "Display information about NShell.";
11+
12+
public void Execute(ShellContext context, string[] args)
13+
{
14+
AnsiConsole.Clear();
15+
16+
var panel = new Panel(new Markup(
17+
$"[bold cyan]NShell[/] - A Custom C# Interactive Shell\n\n" +
18+
$"[grey]Version:[/] [yellow]{Program.VERSION}[/]\n" +
19+
$"[grey]GitHub:[/] [blue]{Program.GITHUB}[/]\n\n" +
20+
$"[grey]Runtime:[/] [green].NET {Environment.Version}[/]\n" +
21+
$"[grey]Platform:[/] [green]{Environment.OSVersion}[/]\n\n" +
22+
$"[dim]Type [yellow]help[/] to see available commands.[/]"
23+
))
24+
{
25+
Header = new PanelHeader("[bold green] About NShell [/]"),
26+
Border = BoxBorder.Rounded
27+
};
28+
29+
AnsiConsole.Write(panel);
30+
Console.WriteLine();
31+
}
32+
}

Commands/AliasCommand.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using NShell.Shell;
2+
using NShell.Shell.Commands;
3+
using NShell.Shell.Config;
4+
using Spectre.Console;
5+
6+
namespace NShell.Commands;
7+
8+
public class AliasCommand : ICustomCommand, IMetadataCommand
9+
{
10+
public string Name => "alias";
11+
public string Description => "Create command aliases (e.g., alias ll='ls -la').";
12+
13+
// Static dictionary to store aliases
14+
public static Dictionary<string, string> Aliases { get; } = new Dictionary<string, string>();
15+
private static readonly ConfigManager _configManager = new ConfigManager();
16+
17+
static AliasCommand()
18+
{
19+
// Load saved aliases on first use
20+
var savedAliases = _configManager.LoadAliases();
21+
foreach (var alias in savedAliases)
22+
{
23+
Aliases[alias.Key] = alias.Value;
24+
}
25+
}
26+
27+
public void Execute(ShellContext context, string[] args)
28+
{
29+
if (args.Length == 0)
30+
{
31+
// List all aliases
32+
if (Aliases.Count == 0)
33+
{
34+
AnsiConsole.MarkupLine("[[[yellow]*[/]]] - No aliases defined.");
35+
return;
36+
}
37+
38+
AnsiConsole.MarkupLine("[bold cyan]Current Aliases:[/]\n");
39+
foreach (var alias in Aliases.OrderBy(a => a.Key))
40+
{
41+
AnsiConsole.MarkupLine($"[yellow]{alias.Key}[/]=[green]'{alias.Value}'[/]");
42+
}
43+
return;
44+
}
45+
46+
// Join all args to handle aliases with spaces
47+
var fullArg = string.Join(' ', args);
48+
var parts = fullArg.Split('=', 2);
49+
50+
if (parts.Length != 2)
51+
{
52+
AnsiConsole.MarkupLine("[[[yellow]*[/]]] - Usage: alias name='command'");
53+
return;
54+
}
55+
56+
string aliasName = parts[0].Trim();
57+
string aliasValue = parts[1].Trim();
58+
59+
// Remove quotes if present
60+
if ((aliasValue.StartsWith("\"") && aliasValue.EndsWith("\"")) ||
61+
(aliasValue.StartsWith("'") && aliasValue.EndsWith("'")))
62+
{
63+
aliasValue = aliasValue.Substring(1, aliasValue.Length - 2);
64+
}
65+
66+
Aliases[aliasName] = aliasValue;
67+
_configManager.SaveAliases(Aliases);
68+
AnsiConsole.MarkupLine($"[[[green]+[/]]] - Alias created: [yellow]{aliasName}[/]=[green]'{aliasValue}'[/]");
69+
}
70+
}

Commands/CdCommand.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

Commands/ClearCommand.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using NShell.Shell;
2+
using NShell.Shell.Commands;
3+
using Spectre.Console;
4+
5+
namespace NShell.Commands;
6+
7+
public class ClearCommand : ICustomCommand, IMetadataCommand
8+
{
9+
public string Name => "clear";
10+
public string Description => "Clear the terminal screen.";
11+
12+
public void Execute(ShellContext context, string[] args)
13+
{
14+
AnsiConsole.Clear();
15+
}
16+
}

Commands/ExitCommand.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using NShell.Shell;
2+
using NShell.Shell.Commands;
3+
using Spectre.Console;
4+
5+
namespace NShell.Commands;
6+
7+
public class ExitCommand : ICustomCommand, IMetadataCommand
8+
{
9+
public string Name => "exit";
10+
public string Description => "Exit the shell.";
11+
12+
public void Execute(ShellContext context, string[] args)
13+
{
14+
// Save history before exiting
15+
Shell.Readline.ReadLine.History.Save();
16+
17+
AnsiConsole.MarkupLine("[bold green]Goodbye![/]");
18+
Environment.Exit(0);
19+
}
20+
}

Commands/ExportCommand.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using NShell.Shell;
2+
using NShell.Shell.Commands;
3+
using Spectre.Console;
4+
5+
namespace NShell.Commands;
6+
7+
public class ExportCommand : ICustomCommand, IMetadataCommand
8+
{
9+
public string Name => "export";
10+
public string Description => "Set environment variables (e.g., export VAR=value).";
11+
12+
public void Execute(ShellContext context, string[] args)
13+
{
14+
if (args.Length == 0)
15+
{
16+
// Display all environment variables
17+
var envVars = Environment.GetEnvironmentVariables();
18+
var sortedKeys = envVars.Keys.Cast<string>().OrderBy(k => k);
19+
20+
foreach (var key in sortedKeys)
21+
{
22+
AnsiConsole.MarkupLine($"[cyan]{key}[/]=[yellow]{envVars[key]}[/]");
23+
}
24+
return;
25+
}
26+
27+
foreach (var arg in args)
28+
{
29+
var parts = arg.Split('=', 2);
30+
if (parts.Length != 2)
31+
{
32+
AnsiConsole.MarkupLine($"[[[yellow]*[/]]] - Invalid format: {arg}. Use: export VAR=value");
33+
continue;
34+
}
35+
36+
string varName = parts[0].Trim();
37+
string varValue = parts[1].Trim();
38+
39+
// Remove quotes if present
40+
if ((varValue.StartsWith("\"") && varValue.EndsWith("\"")) ||
41+
(varValue.StartsWith("'") && varValue.EndsWith("'")))
42+
{
43+
varValue = varValue.Substring(1, varValue.Length - 2);
44+
}
45+
46+
Environment.SetEnvironmentVariable(varName, varValue);
47+
AnsiConsole.MarkupLine($"[[[green]+[/]]] - Set [cyan]{varName}[/]=[yellow]{varValue}[/]");
48+
}
49+
}
50+
}

Commands/HelpCommand.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using NShell.Shell;
2+
using NShell.Shell.Commands;
3+
using Spectre.Console;
4+
5+
namespace NShell.Commands;
6+
7+
public class HelpCommand : ICustomCommand, IMetadataCommand
8+
{
9+
public string Name => "help";
10+
public string Description => "Display help information about available commands.";
11+
12+
public void Execute(ShellContext context, string[] args)
13+
{
14+
AnsiConsole.MarkupLine("[bold cyan]NShell - Available Commands[/]\n");
15+
16+
var table = new Table();
17+
table.AddColumn("[bold]Command[/]");
18+
table.AddColumn("[bold]Description[/]");
19+
20+
// Add custom commands with descriptions
21+
foreach (var cmd in CommandParser.CustomCommands.Values.OrderBy(c => c.Name))
22+
{
23+
string description = "No description available";
24+
if (cmd is IMetadataCommand metaCmd)
25+
{
26+
description = metaCmd.Description;
27+
}
28+
29+
table.AddRow($"[yellow]{cmd.Name}[/]", description);
30+
}
31+
32+
AnsiConsole.Write(table);
33+
34+
AnsiConsole.MarkupLine($"\n[grey]Total custom commands: {CommandParser.CustomCommands.Count}[/]");
35+
AnsiConsole.MarkupLine($"[grey]Total system commands: {CommandParser.SystemCommands.Count}[/]");
36+
AnsiConsole.MarkupLine("\n[grey]Type a command name to execute it, or use Tab for auto-completion.[/]");
37+
}
38+
}

Commands/HistoryCommand.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using NShell.Shell;
2+
using NShell.Shell.Commands;
3+
using NShell.Shell.Readline;
4+
using Spectre.Console;
5+
6+
namespace NShell.Commands;
7+
8+
public class HistoryCommand : ICustomCommand, IMetadataCommand
9+
{
10+
public string Name => "history";
11+
public string Description => "Display command history.";
12+
13+
public void Execute(ShellContext context, string[] args)
14+
{
15+
int displayCount = 20; // Default to last 20 commands
16+
int historyCount = ReadLine.History.Count;
17+
18+
if (args.Length > 0)
19+
{
20+
if (args[0] == "-c" || args[0] == "--clear")
21+
{
22+
// Clear history - not implemented as it would require HistoryManager changes
23+
AnsiConsole.MarkupLine("[[[yellow]*[/]]] - History clearing not yet implemented.");
24+
return;
25+
}
26+
else if (int.TryParse(args[0], out int count))
27+
{
28+
displayCount = count;
29+
}
30+
else
31+
{
32+
AnsiConsole.MarkupLine("[[[yellow]*[/]]] - Usage: history [number] or history -c");
33+
return;
34+
}
35+
}
36+
37+
// Display the last N commands
38+
int startIndex = Math.Max(0, historyCount - displayCount);
39+
40+
if (historyCount == 0)
41+
{
42+
AnsiConsole.MarkupLine("[[[yellow]*[/]]] - No commands in history.");
43+
return;
44+
}
45+
46+
AnsiConsole.MarkupLine($"[bold cyan]Command History (last {Math.Min(displayCount, historyCount)} commands):[/]\n");
47+
48+
for (int i = startIndex; i < historyCount; i++)
49+
{
50+
var command = ReadLine.History.GetAt(i);
51+
if (command != null)
52+
{
53+
AnsiConsole.MarkupLine($" [grey]{i + 1,4}[/] {command}");
54+
}
55+
}
56+
}
57+
}

Commands/PrintEnvCommand.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using NShell.Shell;
2+
using NShell.Shell.Commands;
3+
using Spectre.Console;
4+
5+
namespace NShell.Commands;
6+
7+
public class PrintEnvCommand : ICustomCommand, IMetadataCommand
8+
{
9+
public string Name => "printenv";
10+
public string Description => "Print environment variables.";
11+
12+
public void Execute(ShellContext context, string[] args)
13+
{
14+
if (args.Length == 0)
15+
{
16+
// Display all environment variables
17+
var envVars = Environment.GetEnvironmentVariables();
18+
var sortedKeys = envVars.Keys.Cast<string>().OrderBy(k => k);
19+
20+
foreach (var key in sortedKeys)
21+
{
22+
AnsiConsole.MarkupLine($"[cyan]{key}[/]=[yellow]{envVars[key]}[/]");
23+
}
24+
}
25+
else
26+
{
27+
// Display specific environment variables
28+
foreach (var varName in args)
29+
{
30+
var value = Environment.GetEnvironmentVariable(varName);
31+
if (value != null)
32+
{
33+
AnsiConsole.MarkupLine($"[cyan]{varName}[/]=[yellow]{value}[/]");
34+
}
35+
else
36+
{
37+
AnsiConsole.MarkupLine($"[[[yellow]*[/]]] - Variable [cyan]{varName}[/] is not set.");
38+
}
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)