Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PromptPlus output ignores moved cursor position by asynchronous operations #122

Open
ividyon opened this issue Jan 16, 2024 · 0 comments
Open
Assignees

Comments

@ividyon
Copy link
Contributor

ividyon commented Jan 16, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants