PromptPlus icon indicating copy to clipboard operation
PromptPlus copied to clipboard

PromptPlus output ignores moved cursor position by asynchronous operations

Open ividyon opened this issue 2 years ago • 0 comments

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:

2024-01-16_08-01-42__rider64

When one of the event handlers responds, new text is printed:

2024-01-16_08-02-10__rider64

But if I then "press any key", the following output overwrites those lines from the original position of the "Press any key" prompt.

2024-01-16_08-02-50__rider64

Do you have advice on how to handle this?

ividyon avatar Jan 16 '24 07:01 ividyon