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