From 3aa9dbe60d5e21c6d40ecdc6e022b38f8a7f84f4 Mon Sep 17 00:00:00 2001 From: Tr0sT Date: Tue, 7 Jan 2020 04:55:58 +0300 Subject: [PATCH] Added an asynchronous method ReadLineAsync for reading with CancellationToken --- .gitignore | 1 + src/ReadLine.Demo/Program.cs | 9 +++++++ src/ReadLine/ReadLine.cs | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/.gitignore b/.gitignore index c304b65..27107da 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *.user project.lock.json .vs/ +.idea/ diff --git a/src/ReadLine.Demo/Program.cs b/src/ReadLine.Demo/Program.cs index 29b484e..77ee6cf 100755 --- a/src/ReadLine.Demo/Program.cs +++ b/src/ReadLine.Demo/Program.cs @@ -1,4 +1,6 @@ using System; +using System.Threading; +using System.Threading.Tasks; namespace ConsoleApplication { @@ -20,6 +22,13 @@ public static void Main(string[] args) input = ReadLine.ReadPassword("Enter Password> "); Console.WriteLine(input); + + var src = new CancellationTokenSource(); + src.CancelAfter(3000); + input = ReadLine.ReadAsync("You have 3 sec to prompt: ", cancellationToken: src.Token).Result; + if (!src.IsCancellationRequested) + Console.WriteLine(input); + src.Dispose(); } } diff --git a/src/ReadLine/ReadLine.cs b/src/ReadLine/ReadLine.cs index 157cf66..b8ad4e3 100755 --- a/src/ReadLine/ReadLine.cs +++ b/src/ReadLine/ReadLine.cs @@ -2,6 +2,8 @@ using Internal.ReadLine.Abstractions; using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; namespace System { @@ -39,6 +41,25 @@ public static string Read(string prompt = "", string @default = "") return text; } + public static async Task ReadAsync(string prompt = "", string @default = "", CancellationToken cancellationToken = default) + { + Console.Write(prompt); + KeyHandler keyHandler = new KeyHandler(new Console2(), _history, AutoCompletionHandler); + string text = await GetTextAsync(keyHandler, cancellationToken); + if (String.IsNullOrWhiteSpace(text) && !String.IsNullOrWhiteSpace(@default)) + { + text = @default; + } + else + { + if (HistoryEnabled) + _history.Add(text); + } + if (cancellationToken.IsCancellationRequested) + Console.WriteLine(""); + return text; + } + public static string ReadPassword(string prompt = "") { Console.Write(prompt); @@ -58,5 +79,33 @@ private static string GetText(KeyHandler keyHandler) Console.WriteLine(); return keyHandler.Text; } + + private static async Task GetTextAsync(KeyHandler keyHandler, CancellationToken cancellationToken = default) + { + Task.Run(() => + { + while (!Console.KeyAvailable) + { + if (cancellationToken.IsCancellationRequested) + return; + Thread.Sleep(50); + } + ConsoleKeyInfo keyInfo = Console.ReadKey(true); + while (keyInfo.Key != ConsoleKey.Enter) + { + keyHandler.Handle(keyInfo); + while (!Console.KeyAvailable) + { + if (cancellationToken.IsCancellationRequested) + return; + Thread.Sleep(50); + } + keyInfo = Console.ReadKey(true); + } + + Console.WriteLine(); + }).Wait(); + return keyHandler.Text; + } } }