/Console/ConsoleExtensions.cs |
@@ -0,0 +1,112 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.Collections.Generic; |
using System.Linq; |
|
namespace wasSharpNET.Console |
{ |
public static class ConsoleExtensions |
{ |
public enum ConsoleTextAlignment |
{ |
TOP_LEFT, |
TOP_CENTER, |
TOP_RIGHT |
} |
|
|
public static void WriteLine(this object data, ConsoleColor foreground, |
ConsoleColor background) |
{ |
var cFG = System.Console.ForegroundColor; |
var cBG = System.Console.BackgroundColor; |
System.Console.ForegroundColor = foreground; |
System.Console.BackgroundColor = background; |
System.Console.WriteLine(data); |
System.Console.ForegroundColor = cFG; |
System.Console.BackgroundColor = cBG; |
} |
|
public static void WriteLine(this object data, ConsoleColor foreground) |
{ |
var cFG = System.Console.ForegroundColor; |
System.Console.ForegroundColor = foreground; |
System.Console.WriteLine(data); |
System.Console.ForegroundColor = cFG; |
} |
|
public static void Write(this object data, ConsoleColor foreground, |
ConsoleColor background) |
{ |
var cFG = System.Console.ForegroundColor; |
var cBG = System.Console.BackgroundColor; |
System.Console.ForegroundColor = foreground; |
System.Console.BackgroundColor = background; |
System.Console.Write(data); |
System.Console.ForegroundColor = cFG; |
System.Console.BackgroundColor = cBG; |
} |
|
public static void WriteLine(this object data, ConsoleTextAlignment alignment) |
{ |
switch (alignment) |
{ |
case ConsoleTextAlignment.TOP_CENTER: |
System.Console.CursorLeft = Math.Max(System.Console.WindowWidth/2 - data.ToString().Length/2, 0); |
WriteLine(data, System.Console.ForegroundColor, System.Console.BackgroundColor); |
break; |
default: |
throw new NotImplementedException(); |
} |
} |
|
public static void WriteLine(this object data, ConsoleTextAlignment alignment, ConsoleColor foregroundColor) |
{ |
switch (alignment) |
{ |
case ConsoleTextAlignment.TOP_CENTER: |
System.Console.CursorLeft = Math.Max(System.Console.WindowWidth / 2 - data.ToString().Length / 2, 0); |
WriteLine(data, foregroundColor, System.Console.BackgroundColor); |
break; |
default: |
throw new NotImplementedException(); |
} |
} |
|
public static void Write(this object data, ConsoleTextAlignment alignment) |
{ |
switch (alignment) |
{ |
case ConsoleTextAlignment.TOP_CENTER: |
System.Console.CursorLeft = Math.Max(System.Console.WindowWidth/2 - data.ToString().Length/2, 0); |
Write(data, System.Console.ForegroundColor, System.Console.BackgroundColor); |
break; |
default: |
throw new NotImplementedException(); |
} |
} |
|
public static void WriteLine(this IEnumerable<object> data, ConsoleTextAlignment alignment) |
{ |
switch (alignment) |
{ |
case ConsoleTextAlignment.TOP_CENTER: |
var textBlock = data.Select(o => o.ToString()).ToArray(); |
var padding = Math.Max(System.Console.WindowWidth/2 - textBlock.Select(o => o.Length).Max()/2, 0); |
foreach (var line in data) |
{ |
System.Console.CursorLeft = padding; |
WriteLine(line, System.Console.ForegroundColor, System.Console.BackgroundColor); |
} |
break; |
default: |
throw new NotImplementedException(); |
} |
} |
} |
} |
/Console/ConsoleSpin.cs |
@@ -5,12 +5,17 @@ |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.Text; |
using System.Threading; |
using wasSharp.Collections.Generic; |
|
namespace wasSharpNET.Console |
{ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// A command-line spinner indicating activity. |
/// </summary> |
public class ConsoleSpin : IDisposable |
{ |
private static readonly CircularQueue<string> spinArt = |
@@ -20,21 +25,24 @@ |
private static Thread spinThread; |
private static bool run = true; |
|
public ConsoleSpin() |
public ConsoleSpin() : this(ConsoleExtensions.ConsoleTextAlignment.TOP_LEFT) |
{ |
} |
|
public ConsoleSpin(ConsoleExtensions.ConsoleTextAlignment alignment) |
{ |
spinThread = new Thread(() => |
{ |
do |
while (run) |
{ |
spinEvent.WaitOne(); |
Thread.Sleep(100); |
|
var deco = spinArt.Dequeue(); |
System.Console.Write(deco); |
deco.Write(alignment); |
foreach (var c in deco) |
System.Console.Write("\b"); |
"\b".Write(alignment); |
|
} while (run); |
Thread.Sleep(100); |
} |
}) |
{ |
IsBackground = true |
@@ -47,12 +55,16 @@ |
// Stop the callback thread. |
try |
{ |
run = false; |
spinEvent.Set(); |
if ((!spinThread.ThreadState.Equals(ThreadState.Running) && |
!spinThread.ThreadState.Equals(ThreadState.WaitSleepJoin)) || spinThread.Join(1000)) return; |
spinThread.Abort(); |
spinThread.Join(); |
if (spinThread != null) |
{ |
run = false; |
spinEvent.Reset(); |
if ((!spinThread.ThreadState.Equals(ThreadState.Running) && |
!spinThread.ThreadState.Equals(ThreadState.WaitSleepJoin)) || spinThread.Join(1000)) |
return; |
spinThread.Abort(); |
spinThread.Join(); |
} |
} |
catch (Exception) |
{ |