PromptPlus
PromptPlus copied to clipboard
PromptPlus output ignores moved cursor position by asynchronous operations
I am currently implementing some asynchronous event handling:
foreach ((string path, WFileParser parser) in files)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Path.GetDirectoryName(Path.GetFullPath(path))!;
watcher.Filter = Path.GetFileName(path);
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Changed += WatchChanges;
watcher.Deleted += WatchRemoval;
watcher.Renamed += WatchRemoval;
string fullPath = Path.Combine(watcher.Path, watcher.Filter);
PromptPlus.WriteLine($"Watching path \"{fullPath}\"...");
_activeWatchers.TryAdd(fullPath, (watcher, parser));
watcher.EnableRaisingEvents = true;
}
// PromptPlus.WriteLine($"Watching {dict.Count} path(s) for changes.");
PromptPlus.WriteLine($"Watching {files.Count} path(s) for changes.");
PromptPlus.KeyPress(@"Press any key to stop watching files...
").Run();
private static void WatchRemoval(object sender, FileSystemEventArgs e)
{
PromptPlus.WriteLine($"Path {Path.GetFileName(e.Name)} was renamed or deleted, aborting watch.");
_activeWatchers[e.FullPath].Item1.EnableRaisingEvents = false;
_activeWatchers.Remove(e.FullPath);
}
private static void WatchChanges(object sender, FileSystemEventArgs e)
{
PromptPlus.WriteLine($"Detected change in {e.Name}.");
Catcher.Catch(() => ParseMode.Repack(e.FullPath, _activeWatchers[e.FullPath].Item2, false, out _), out _, e.FullPath);
}
This results in the following output:
When one of the event handlers responds, new text is printed:
But if I then "press any key", the following output overwrites those lines from the original position of the "Press any key" prompt.
Do you have advice on how to handle this?