Skip to content

Commit 0634d0f

Browse files
committed
refactor: create DemonstrateVFS.cs command
1 parent 8f270ed commit 0634d0f

File tree

7 files changed

+122
-109
lines changed

7 files changed

+122
-109
lines changed

Atypical.VirtualFileSystem.DemoCli/Atypical.VirtualFileSystem.DemoCli.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<ItemGroup>
88
<PackageReference Include="Spectre.Console" Version="0.48.0" />
9+
<PackageReference Include="Spectre.Console.Cli" Version="0.48.0" />
910
</ItemGroup>
1011

1112
<ItemGroup>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using Spectre.Console.Cli;
2+
3+
namespace Atypical.VirtualFileSystem.DemoCli.Commands;
4+
5+
public class DemonstrateVFS : Command
6+
{
7+
public override int Execute(CommandContext context)
8+
{
9+
// Create a virtual file system
10+
var factory = new VirtualFileSystemFactory();
11+
var vfs = factory.CreateFileSystem();
12+
13+
// Subscribe to VFS events
14+
SubscribeToWriteVFSEvents(vfs, OnChange);
15+
16+
// Display a banner
17+
AnsiConsole.Write(
18+
new FigletText("VFS Demo")
19+
.LeftJustified()
20+
.Color(Color.Gold1));
21+
22+
// Create a directory structure
23+
ProcessStep(vfs, "CREATE A FILE STRUCTURE", () =>
24+
{
25+
vfs.CreateDirectory(new VFSDirectoryPath("/heroes"));
26+
vfs.CreateFile(new VFSFilePath("/heroes/ironman.txt"), "Tony Stark");
27+
vfs.CreateFile(new VFSFilePath("/heroes/captain_america.txt"), "Steve Rogers");
28+
vfs.CreateFile(new VFSFilePath("/heroes/hulk.txt"), "Bruce Banner");
29+
vfs.CreateFile(new VFSFilePath("/heroes/thor.txt"), "Thor Odinson");
30+
vfs.CreateFile(new VFSFilePath("/heroes/black_widow.txt"), "Natasha Romanoff");
31+
32+
vfs.CreateDirectory(new VFSDirectoryPath("/villains"));
33+
vfs.CreateFile(new VFSFilePath("/villains/loki.txt"), "Loki Laufeyson");
34+
vfs.CreateFile(new VFSFilePath("/villains/ultron.txt"), "Ultron");
35+
vfs.CreateFile(new VFSFilePath("/villains/thanos.txt"), "Thanos");
36+
});
37+
38+
// Rename a file
39+
ProcessStep(vfs, "RENAME A FILE",
40+
() => vfs.RenameFile(new VFSFilePath("/heroes/ironman.txt"), "tommy_stark.txt"));
41+
42+
// UNDO
43+
ProcessStep(vfs, "UNDO", () => vfs.ChangeHistory.Undo());
44+
45+
// REDO
46+
ProcessStep(vfs, "REDO", () => vfs.ChangeHistory.Redo());
47+
48+
// Move a file
49+
ProcessStep(vfs, "MOVE A FILE",
50+
() => vfs.MoveFile(new VFSFilePath("/heroes/tony_stark.txt"), new VFSFilePath("/villains/tony_stark.txt")));
51+
52+
// Delete a file
53+
ProcessStep(vfs, "DELETE A FILE",
54+
() => vfs.DeleteFile(new VFSFilePath("/villains/tony_stark.txt")));
55+
56+
// Delete a directory
57+
ProcessStep(vfs, "DELETE DIRECTORY",
58+
() => vfs.DeleteDirectory(new VFSDirectoryPath("/villains")));
59+
60+
// Move a directory
61+
ProcessStep(vfs, "MOVE DIRECTORY",
62+
() => vfs.MoveDirectory(new VFSDirectoryPath("/heroes"), new VFSDirectoryPath("/avengers")));
63+
64+
// Rename a directory
65+
// TODO: fix rename directory
66+
// ProcessStep(vfs, "RENAME DIRECTORY",
67+
// () => vfs.RenameDirectory(new VFSDirectoryPath("/avengers"), new VFSDirectoryPath("/heroes")));
68+
69+
return 0;
70+
}
71+
72+
private static void SubscribeToWriteVFSEvents(
73+
IVirtualFileSystem virtualFileSystem,
74+
Action<VFSEventArgs> action)
75+
{
76+
virtualFileSystem.DirectoryCreated += action;
77+
virtualFileSystem.FileCreated += action;
78+
virtualFileSystem.DirectoryDeleted += action;
79+
virtualFileSystem.FileDeleted += action;
80+
virtualFileSystem.DirectoryMoved += action;
81+
virtualFileSystem.FileMoved += action;
82+
virtualFileSystem.DirectoryRenamed += action;
83+
virtualFileSystem.FileRenamed += action;
84+
}
85+
86+
private static void OnChange(VFSEventArgs args)
87+
{
88+
AnsiConsole.Write(new Markup($" - {args.MessageWithMarkup}"));
89+
AnsiConsole.WriteLine();
90+
}
91+
92+
private static void ProcessStep(IVirtualFileSystem virtualFileSystem, string sectionHeader, Action action)
93+
{
94+
WriteSectionHeader(sectionHeader);
95+
action();
96+
WriteTree(virtualFileSystem);
97+
}
98+
99+
private static void WriteSectionHeader(string header = "")
100+
{
101+
AnsiConsole.Write(new Markup($"[underline yellow]{header}[/]"));
102+
AnsiConsole.WriteLine();
103+
AnsiConsole.WriteLine();
104+
}
105+
106+
private static void WriteTree(IVirtualFileSystem virtualFileSystem)
107+
{
108+
AnsiConsole.WriteLine();
109+
AnsiConsole.Write(new Tree("Marvel Universe").FillTree(virtualFileSystem));
110+
AnsiConsole.WriteLine();
111+
}
112+
}

Atypical.VirtualFileSystem.DemoCli/Extensions/TreeExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// This source code is licensed under the BSD-style license found in the
55
// LICENSE file in the root directory of this source tree.
66

7-
using Atypical.VirtualFileSystem.Core.Contracts;
87
using Spectre.Console.Rendering;
98

109
namespace Atypical.VirtualFileSystem.DemoCli.Extensions;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
// Global using directives
22

3+
global using Atypical.VirtualFileSystem.Core;
4+
global using Atypical.VirtualFileSystem.Core.Contracts;
5+
global using Atypical.VirtualFileSystem.DemoCli.Extensions;
36
global using Spectre.Console;
Lines changed: 6 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,11 @@
1-
using Atypical.VirtualFileSystem.Core;
2-
using Atypical.VirtualFileSystem.Core.Contracts;
3-
using Atypical.VirtualFileSystem.DemoCli.Extensions;
4-
using Atypical.VirtualFileSystem.DemoCli.WIP;
1+
using Atypical.VirtualFileSystem.DemoCli.Commands;
2+
using Spectre.Console.Cli;
53

6-
// Create a virtual file system
7-
var factory = new VirtualFileSystemFactory();
8-
var vfs = factory.CreateFileSystem();
4+
var app = new CommandApp();
95

10-
// Add UNDO/REDO functionality
11-
var changeHistory = new ChangeHistory(vfs);
12-
13-
// Subscribe to VFS events
14-
SubscribeToWriteVFSEvents(vfs);
15-
16-
// Display a banner
17-
AnsiConsole.Write(
18-
new FigletText("VFS Demo")
19-
.LeftJustified()
20-
.Color(Color.Gold1));
21-
22-
// Create a directory structure
23-
ProcessStep(vfs, "CREATE A FILE STRUCTURE", () =>
6+
app.Configure(config =>
247
{
25-
vfs.CreateDirectory(new VFSDirectoryPath("/heroes"));
26-
vfs.CreateFile(new VFSFilePath("/heroes/ironman.txt"), "Tony Stark");
27-
vfs.CreateFile(new VFSFilePath("/heroes/captain_america.txt"), "Steve Rogers");
28-
vfs.CreateFile(new VFSFilePath("/heroes/hulk.txt"), "Bruce Banner");
29-
vfs.CreateFile(new VFSFilePath("/heroes/thor.txt"), "Thor Odinson");
30-
vfs.CreateFile(new VFSFilePath("/heroes/black_widow.txt"), "Natasha Romanoff");
31-
32-
vfs.CreateDirectory(new VFSDirectoryPath("/villains"));
33-
vfs.CreateFile(new VFSFilePath("/villains/loki.txt"), "Loki Laufeyson");
34-
vfs.CreateFile(new VFSFilePath("/villains/ultron.txt"), "Ultron");
35-
vfs.CreateFile(new VFSFilePath("/villains/thanos.txt"), "Thanos");
8+
config.AddCommand<DemonstrateVFS>("demo");
369
});
3710

38-
// Rename a file
39-
ProcessStep(vfs, "RENAME A FILE",
40-
() => vfs.RenameFile(new VFSFilePath("/heroes/ironman.txt"), "tommy_stark.txt"));
41-
42-
// UNDO
43-
ProcessStep(vfs, "UNDO", () => changeHistory.Undo());
44-
45-
// REDO
46-
ProcessStep(vfs, "REDO", () => changeHistory.Redo());
47-
48-
// Move a file
49-
ProcessStep(vfs, "MOVE A FILE",
50-
() => vfs.MoveFile(new VFSFilePath("/heroes/tony_stark.txt"), new VFSFilePath("/villains/tony_stark.txt")));
51-
52-
// Delete a file
53-
ProcessStep(vfs, "DELETE A FILE",
54-
() => vfs.DeleteFile(new VFSFilePath("/villains/tony_stark.txt")));
55-
56-
// Delete a directory
57-
ProcessStep(vfs, "DELETE DIRECTORY",
58-
() => vfs.DeleteDirectory(new VFSDirectoryPath("/villains")));
59-
60-
// Move a directory
61-
ProcessStep(vfs, "MOVE DIRECTORY",
62-
() => vfs.MoveDirectory(new VFSDirectoryPath("/heroes"), new VFSDirectoryPath("/avengers")));
63-
64-
// Rename a directory
65-
// TODO: fix rename directory
66-
// ProcessStep(vfs, "RENAME DIRECTORY",
67-
// () => vfs.RenameDirectory(new VFSDirectoryPath("/avengers"), new VFSDirectoryPath("/heroes")));
68-
return;
69-
70-
void SubscribeToWriteVFSEvents(IVirtualFileSystem virtualFileSystem)
71-
{
72-
virtualFileSystem.DirectoryCreated += OnChange;
73-
virtualFileSystem.FileCreated += OnChange;
74-
virtualFileSystem.DirectoryDeleted += OnChange;
75-
virtualFileSystem.FileDeleted += OnChange;
76-
virtualFileSystem.DirectoryMoved += OnChange;
77-
virtualFileSystem.FileMoved += OnChange;
78-
virtualFileSystem.DirectoryRenamed += OnChange;
79-
virtualFileSystem.FileRenamed += OnChange;
80-
return;
81-
82-
static void OnChange(VFSEventArgs args)
83-
{
84-
AnsiConsole.Write(new Markup($" - {args.MessageWithMarkup}"));
85-
AnsiConsole.WriteLine();
86-
}
87-
}
88-
89-
void ProcessStep(IVirtualFileSystem virtualFileSystem, string sectionHeader, Action action)
90-
{
91-
WriteSectionHeader(sectionHeader);
92-
action();
93-
WriteTree(virtualFileSystem);
94-
}
95-
96-
void WriteSectionHeader(string header = "")
97-
{
98-
AnsiConsole.Write(new Markup($"[underline yellow]{header}[/]"));
99-
AnsiConsole.WriteLine();
100-
AnsiConsole.WriteLine();
101-
}
102-
103-
void WriteTree(IVirtualFileSystem virtualFileSystem)
104-
{
105-
AnsiConsole.WriteLine();
106-
AnsiConsole.Write(new Tree("Marvel Universe").FillTree(virtualFileSystem));
107-
AnsiConsole.WriteLine();
108-
}
11+
app.Run(args);

Atypical.VirtualFileSystem.DemoCli/WIP/FileSystemWatcher.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
// This source code is licensed under the BSD-style license found in the
55
// LICENSE file in the root directory of this source tree.
66

7-
using Atypical.VirtualFileSystem.Core;
8-
using Atypical.VirtualFileSystem.Core.Contracts;
9-
107
namespace Atypical.VirtualFileSystem.DemoCli.WIP;
118

129
public class FileSystemWatcher : IFileSystemWatcher

Atypical.VirtualFileSystem.DemoCli/WIP/IFileSystemWatcher.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
// This source code is licensed under the BSD-style license found in the
55
// LICENSE file in the root directory of this source tree.
66

7-
using Atypical.VirtualFileSystem.Core;
8-
97
namespace Atypical.VirtualFileSystem.DemoCli.WIP;
108

119
public interface IFileSystemWatcher

0 commit comments

Comments
 (0)