-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFileManager.cs
More file actions
335 lines (292 loc) · 14 KB
/
FileManager.cs
File metadata and controls
335 lines (292 loc) · 14 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using Spectre.Console;
using Spectre.Console.Rendering;
using termix.Handlers;
using termix.models;
using termix.Services;
using termix.UI;
using System.Collections.Concurrent;
namespace termix;
public class FileManager
{
public readonly FileManagerState State = new();
public readonly ActionHandler ActionHandler;
public readonly NavigationHandler NavigationHandler;
public readonly FilterHandler FilterHandler;
private readonly InputHandler _inputHandler;
private readonly DoubleBufferedRenderer _doubleBuffer = new();
private readonly FilePreviewService _filePreviewService;
private readonly FileManagerRenderer _renderer;
private readonly ConfigService _configService = new();
public readonly List<(string Text, string Command)> OpenWithOptions = OpenWithOptionsProvider.GetOptions();
private bool _needsRedraw = true;
private bool _shouldQuit;
private readonly ConcurrentQueue<Action> _uiActions = new();
private int _lastWindowWidth;
private int _lastWindowHeight;
public readonly List<(string Text, SortBy By, SortDirection Dir, bool Group)> SortOptions =
[
("Name: A to Z", SortBy.Name, SortDirection.Ascending, true),
("Name: Z to A", SortBy.Name, SortDirection.Descending, true),
("Date: Newest First", SortBy.Date, SortDirection.Descending, true),
("Date: Oldest First", SortBy.Date, SortDirection.Ascending, true),
("Size: Largest First", SortBy.Size, SortDirection.Descending, true),
("Size: Smallest First", SortBy.Size, SortDirection.Ascending, true),
("Mixed Sort: Name A to Z", SortBy.Name, SortDirection.Ascending, false),
("Mixed Sort: Name Z to A", SortBy.Name, SortDirection.Descending, false),
("Mixed Sort: Date Newest First", SortBy.Date, SortDirection.Descending, false),
("Mixed Sort: Date Oldest First", SortBy.Date, SortDirection.Ascending, false)
];
public FileManager(bool useIcons)
{
var config = _configService.Load();
State.ShowHiddenFiles = config.ShowHiddenFiles;
var iconProvider = new IconProvider(useIcons);
_renderer = new FileManagerRenderer(iconProvider);
_filePreviewService = new FilePreviewService(iconProvider);
var bookmarkService = new BookmarkService();
ActionHandler = new ActionHandler(this, bookmarkService);
NavigationHandler = new NavigationHandler(this);
FilterHandler = new FilterHandler(this);
_inputHandler = new InputHandler(this);
}
public void ScheduleUiAction(Action action)
{
_uiActions.Enqueue(action);
}
public void Run()
{
AnsiConsole.Clear();
_lastWindowWidth = Console.WindowWidth;
_lastWindowHeight = Console.WindowHeight;
RefreshDirectory(setInitialSelection: true);
while (!_shouldQuit)
{
while (_uiActions.TryDequeue(out var action))
{
action.Invoke();
}
if (_needsRedraw)
{
_needsRedraw = false;
var footerContent = CreateFooterRenderable();
var layout = _renderer.GetLayout(this, footerContent);
_doubleBuffer.Render(layout);
}
while (!Console.KeyAvailable && !_needsRedraw && !_shouldQuit && _uiActions.IsEmpty)
{
if (Console.WindowWidth != _lastWindowWidth || Console.WindowHeight != _lastWindowHeight)
{
_lastWindowWidth = Console.WindowWidth;
_lastWindowHeight = Console.WindowHeight;
AnsiConsole.Clear();
AdjustViewPort();
UpdatePreview();
SetNeedsRedraw();
}
Thread.Sleep(50);
}
if (_shouldQuit) break;
if (Console.KeyAvailable) _inputHandler.ProcessKey(Console.ReadKey(true));
}
}
public void SetNeedsRedraw() => _needsRedraw = true;
public void Quit(bool force = false)
{
if (force && State.IsOperationInProgress) State.OperationCts?.Cancel();
_shouldQuit = true;
}
public void ResetToNormalMode()
{
State.DebounceCts.Cancel();
State.IsDeepSearchRunning = false;
State.CurrentMode = InputMode.Normal;
State.InputText = "";
State.PromptText = "";
State.VisuallySelectedItems.Clear();
State.VisuallySelectedBookmarks.Clear();
SetNeedsRedraw();
}
public void RefreshDirectory(string? findAndSelect = null, bool preserveSelection = false,
bool setInitialSelection = false)
{
var oldSelectedIndex = State.SelectedIndex;
LoadCurrentDirectory();
if (findAndSelect != null)
{
State.SelectedIndex = State.CurrentItems.FindIndex(item =>
item.Name.Equals(findAndSelect, StringComparison.OrdinalIgnoreCase));
}
else if (preserveSelection)
{
State.SelectedIndex = Math.Clamp(oldSelectedIndex, 0, State.CurrentItems.Count - 1);
}
else if (setInitialSelection)
{
var firstSelectableIndex = State.CurrentItems.FindIndex(item => !item.IsParentDirectory);
State.SelectedIndex = firstSelectableIndex != -1 ? firstSelectableIndex : 0;
if (State.CurrentItems.Count == 0) State.SelectedIndex = -1;
}
if (State is { SelectedIndex: -1, CurrentItems.Count: > 0 }) State.SelectedIndex = 0;
AdjustViewPort();
UpdatePreview();
}
public void AdjustViewPort()
{
var pageSize = Console.WindowHeight - 12;
pageSize = Math.Max(5, pageSize);
State.ViewOffset = State.SelectedIndex < State.ViewOffset ? State.SelectedIndex :
State.SelectedIndex >= State.ViewOffset + pageSize ? State.SelectedIndex - pageSize + 1 :
State.ViewOffset;
State.ViewOffset = Math.Clamp(State.ViewOffset, 0, Math.Max(0, State.CurrentItems.Count - pageSize));
SetNeedsRedraw();
}
public void UpdatePreview(bool resetScroll = true)
{
if (resetScroll)
{
State.PreviewVerticalOffset = 0;
State.PreviewHorizontalOffset = 0;
}
var selectedItem = State.SelectedIndex >= 0 && State.SelectedIndex < State.CurrentItems.Count
? State.CurrentItems[State.SelectedIndex]
: null;
State.CurrentPreview = selectedItem == null
? _filePreviewService.GetPreview(null, 0, 0, State.ShowHiddenFiles)
: _filePreviewService.GetPreview(selectedItem.Path, State.PreviewVerticalOffset,
State.PreviewHorizontalOffset, State.ShowHiddenFiles);
SetNeedsRedraw();
}
private void LoadCurrentDirectory()
{
try
{
State.UnfilteredItems = FileSystemService.GetDirectoryContents(
State.CurrentPath,
State.SortBy,
State.SortDirection,
State.GroupDirectories,
State.ShowHiddenFiles);
State.GitStatuses = GitService.GetRepoStatuses(State.CurrentPath);
if (State.CurrentMode != InputMode.Filter) State.CurrentItems = [.. State.UnfilteredItems];
}
catch (Exception ex)
{
State.StatusMessage = $"[red]Error loading directory: {ex.Message.EscapeMarkup()}[/]";
State.CurrentItems = [];
State.UnfilteredItems = [];
State.SelectedIndex = -1;
}
SetNeedsRedraw();
}
public void ToggleHiddenFiles()
{
State.ShowHiddenFiles = !State.ShowHiddenFiles;
var config = _configService.Load();
config.ShowHiddenFiles = State.ShowHiddenFiles;
_configService.Save(config);
RefreshDirectory(preserveSelection: true);
State.StatusMessage =
State.ShowHiddenFiles ? "[yellow]Showing hidden files[/]" : "[grey]Hiding hidden files[/]";
}
private IRenderable CreateFooterRenderable()
{
if (State.IsOperationInProgress)
{
var grid = new Grid().AddColumns(new GridColumn().NoWrap(), new GridColumn().PadLeft(1),
new GridColumn().PadLeft(1));
grid.AddRow(
new Markup(State.ProgressTaskDescription ?? "Processing..."),
new CustomProgressBar { Value = State.ProgressValue, Width = 30 },
new Markup($"[bold]{State.ProgressValue:F0}%[/]")
);
return new Panel(grid)
{ Border = BoxBorder.Rounded, BorderStyle = new Style(Color.Yellow), Padding = new Padding(1, 1) };
}
if (State.StatusMessage != null)
{
var borderColor = Color.Fuchsia;
if (State.StatusMessage.Contains("[green]")) borderColor = Color.Green;
if (State.StatusMessage.Contains("[red]")) borderColor = Color.Red;
if (State.StatusMessage.Contains("[yellow]")) borderColor = Color.Yellow;
return new Panel(new Markup(State.StatusMessage))
{ Border = BoxBorder.Rounded, BorderStyle = new Style(borderColor) };
}
switch (State.CurrentMode)
{
case InputMode.DeleteConfirm or InputMode.QuitConfirm or InputMode.CreateDirConfirm
or InputMode.BookmarkDeleteConfirm or InputMode.PasteConflict:
return new Panel(new Markup(State.PromptText))
{
Border = BoxBorder.Rounded,
BorderStyle = new Style(Color.Yellow)
};
case InputMode.Add or InputMode.Rename or InputMode.AddBookmark or InputMode.RenameBookmark:
var simplePromptContent =
$"{State.PromptText.EscapeMarkup()}[yellow]{State.InputText.EscapeMarkup()}[/][grey]█[/]";
return new Panel(new Markup(simplePromptContent))
{
Border = BoxBorder.Rounded,
BorderStyle = new Style(Color.Cyan1)
};
case InputMode.Filter:
var searchIndicator = State.IsDeepSearchRunning ? "[grey](Searching...)[/]" : "";
var filterPromptContent =
$"{State.PromptText.EscapeMarkup()}{searchIndicator} [yellow]{State.InputText.EscapeMarkup()}[/][grey]█[/] | [grey]Press[/] [cyan]Esc[/] [grey]to navigate results[/]";
return new Panel(new Markup(filterPromptContent))
{
Border = BoxBorder.Rounded,
BorderStyle = new Style(Color.Cyan1)
};
case InputMode.BookmarkFilter:
var bookmarkFilterPromptContent =
$"{State.PromptText.EscapeMarkup()}[yellow]{State.InputText.EscapeMarkup()}[/][grey]█[/] | [grey]Press[/] [cyan]Esc[/] [grey]to return to bookmark list[/]";
return new Panel(new Markup(bookmarkFilterPromptContent))
{
Border = BoxBorder.Rounded,
BorderStyle = new Style(Color.Cyan1)
};
}
var helpText = new Markup(GetFooterText());
return new Panel(Align.Center(helpText)) { Border = BoxBorder.None };
}
private string GetFooterText()
{
switch (State.CurrentMode)
{
case InputMode.HelpScreen:
return "[grey]Use[/] [cyan]↑↓/JK[/] [grey]to scroll | Press[/] [cyan]Esc[/] [grey]to close[/]";
case InputMode.Visual:
return
$"[bold yellow]-- VISUAL --[/] [grey]Selected:[/][yellow] {State.VisuallySelectedItems.Count} [/] | [cyan]a[/] [grey]Select All[/] | [cyan]i[/] [grey]Inverse Selection[/] | [cyan]Space[/] [grey]Toggle[/] | [cyan]y[/] [grey]Yank[/] | [cyan]x[/] [grey]Move[/] | [cyan]d[/] [grey]Del[/] | [cyan]Esc[/] [grey]Cancel[/]";
case InputMode.Filter:
var searchIndicator = State.IsDeepSearchRunning ? "[grey](Searching...)[/]" : "[grey](Fuzzy)[/]";
var filterPromptContent =
$"{State.PromptText.EscapeMarkup()}{searchIndicator} [yellow]{State.InputText.EscapeMarkup()}[/][grey]█[/]";
return filterPromptContent;
case InputMode.SortMenu:
return
"[grey]Use[/] [cyan]↓↑/JK[/] [grey]to select[/] | [cyan]Enter[/] [grey]Apply[/] | [cyan]Esc[/] [grey]Cancel[/]";
case InputMode.FilteredNavigation:
return
"[grey]Use[/] [cyan]B[/] [grey]to return to search results[/] | [grey]Currently browsing from a search result.[/]";
case InputMode.BookmarkMenu:
return
"[grey]Use[/] [cyan]↑↓/JK[/] [grey]Move[/] | [cyan]Enter[/] [grey]Jump[/] | [cyan]s[/] [grey]Filter[/] | [cyan]r[/] [grey]Rename[/] | [cyan]d[/] [grey]Delete[/] | [cyan]v[/] [grey]Visual[/] | [cyan]Esc[/] [grey]Close[/]";
case InputMode.BookmarkVisual:
return
$"[bold yellow]-- VISUAL BOOKMARK --[/] [grey]Selected:[/][yellow] {State.VisuallySelectedBookmarks.Count} [/] | [cyan]Space[/] [grey]Toggle[/] | [cyan]d[/] [grey]Del[/] | [cyan]Esc[/] [grey]Cancel[/]";
case InputMode.Normal when !string.IsNullOrEmpty(State.InputText):
return
$"[grey]Results for '[yellow]{State.InputText.EscapeMarkup()}[/]'. Press [cyan]Esc[/] to clear, or [cyan]S[/] for new search.[/]";
default:
if (State.Clipboard == null)
return
"[grey]Use[/] [cyan]↓↑/JK[/] [grey]to move[/] | [cyan]Enter[/] [grey]to open[/] | [cyan]q[/] [grey]to quit[/] | [cyan]b[/] [grey]bookmarks[/] | [cyan]?[/] [grey]for help[/]";
var mode = State.Clipboard.Mode == ClipboardMode.Copy ? "Yank" : "Move";
var items = State.Clipboard.Items.Count == 1
? State.Clipboard.Items[0].Name.EscapeMarkup()
: $"{State.Clipboard.Items.Count} items";
return $"[grey]Clipboard ({mode}):[/] [yellow]{items}[/] | [cyan]p[/] Paste, [cyan]Esc[/] Clear";
}
}
}