Search code examples
c#consoleoperating-systemconsole-applicationcosmos

How to allow user to erase text that was already read by Console.ReadLine();


I am wondering how one can write a program to do something like:

var text = Console.ReadLine();

and allow the user to later erase the text that got read. However, there is no clear way to do this. I know it can be done, since there are some text editors written in c#. Can someone point me in the right direction?


Solution

  • It all depends on how you want to delete the line.

    You can check Can Console.Clear be used to only clear a line instead of whole console? that is basically replacing a specific line with the empty string:

    SetCursorPosition documentation

    // Parameters left, top
    Console.SetCursorPosition(0, Console.CursorTop - 1);
    Console.Write(new string(' ', Console.WindowWidth)); 
    Console.SetCursorPosition(0, Console.CursorTop);
    

    The question here, is how the user will interact with the console to let the console know which row to delete, as the line will change as new rows will be inserted, so you need to find a way to count lines after the first one was inserted.

    So whenever you Writeline or Readline you need to increase the counter to maintain the history. If you want to do it for more than one line, then it gets even more complicated.