Created
April 20, 2016 21:56
-
-
Save XoseLluis/48da5512cb1fb38ff84c9bb432684f72 to your computer and use it in GitHub Desktop.
A ConsoleSpinner in C# (you know the typical |/-\|/-\ kind of thing)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace HttpClientAsyncTest | |
{ | |
public class ConsoleSpinner | |
{ | |
int pos; | |
List<string> items; | |
public ConsoleSpinner(List<string> items) | |
{ | |
this.items = items; | |
this.pos = -1; | |
} | |
private void ClearAndReposition(int length) | |
{ | |
for(int i=0; i<length; i++) | |
{ | |
Console.Write("\b \b"); //move back, remove by writing ' ', move back again | |
} | |
} | |
public void WriteNext() | |
{ | |
if (this.pos != -1) | |
this.ClearAndReposition(this.items[this.pos].Length); | |
if (++this.pos >= this.items.Count) | |
this.pos = 0; | |
Console.Write(this.items[this.pos]); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//var consoleSpinner = new ConsoleSpinner(new List<string>(){"|", "/", "-", "\\", "|", "/", "-", "\\"}); | |
var consoleSpinner = new ConsoleSpinner(new List<string>(){"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}); | |
while(true) | |
{ | |
consoleSpinner.WriteNext(); | |
Thread.Sleep(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment