Skip to content

Instantly share code, notes, and snippets.

@Polyterative
Last active December 7, 2022 16:20
Show Gist options
  • Save Polyterative/f0c704a2b489b48b5cd5fdffb8257007 to your computer and use it in GitHub Desktop.
Save Polyterative/f0c704a2b489b48b5cd5fdffb8257007 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
namespace KeystrokeRedo
{
class Program
{
static void Main(string[] args)
{
// Create an observable sequence of keystrokes from the Console.KeyPress event
IObservable<string> keystrokes = Observable.FromEvent<ConsoleKeyInfo>(
handler => Console.KeyPress += handler,
handler => Console.KeyPress -= handler
).Select(key => key.KeyChar.ToString());
// Create a subject that holds the last 10 keystrokes pressed
Subject<string> keystrokeBuffer = new Subject<string>();
// Add keystrokes to the buffer when the button to redo keystrokes is pressed
keystrokes.Buffer(keystrokes.Where(key => key == "R"))
.Subscribe(keystrokeList => keystrokeBuffer.OnNext(keystrokeList));
// Redo the last keystrokes and clear the buffer when the button to redo keystrokes is pressed
keystrokeBuffer.Do(keystrokeList =>
{
foreach (string keystroke in keystrokeList)
{
Console.Write(keystroke);
}
Console.WriteLine();
keystrokeBuffer.OnNext(new List<string>());
}).Subscribe();
// Clear the buffer when the button to clear the buffer is pressed
keystrokes.Where(key => key == "C")
.Subscribe(_ => keystrokeBuffer.OnNext(new List<string>()));
}
}
}
@Polyterative
Copy link
Author

by chatgpt

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