
This is a clock that tells you the time by highlighting text in a block of text. In this case using a C# Console App. Not exactly original, but I thought I'd give it a try.
The progress bar counts the time until the next 5 minute mark.
Download Code
Download Executable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace TimeTextSquare
{
class Program
{
static void Main(string[] args)
{
TextClock clock = new TextClock()
{
Colour1 = ConsoleColor.Green,
Colour2 = ConsoleColor.DarkGray
};
Console.CursorVisible = false;
Console.WindowWidth = 15;
Console.WindowHeight = 8;
clock.Time = DateTime.Now;
while (true)
{
Console.Clear();
clock.Time = DateTime.Now;
//clock.Time = clock.Time.AddMinutes(5);
Console.Title = clock.Time.ToLongTimeString();
clock.WriteTimeSquare();
System.Threading.Thread.Sleep(new TimeSpan(0, 0, 5));
}
}
}
class TextClock
{
readonly string[] clockText = new string[]
{
"ITSMATENQUARTER",
"TWENTYHALFIVEVE",
"PASTOMELEVENINE",
"EIGHTWELVESEVEN",
"TENTHREEFIVESIX",
"FOURTWONEOCLOCK",
">>|>>|>>|>>|>>|"
};
public DateTime Time { get; set; }
public ConsoleColor Colour1 { get; set; }
public ConsoleColor Colour2 { get; set; }
List<string> minuteHighlightStrings = new List<string>();
List<string> highlightStrings = new List<string>();
string hourHighlightString;
bool highlightFirstA = false;
void SetTimeStrings()
{
minuteHighlightStrings.Clear();
highlightStrings.Clear();
hourHighlightString = "";
highlightStrings.Add("ITS");
if (Time.Minute < 5)
highlightStrings.Add("OCLOCK");
else if (Time.Minute < 10)
minuteHighlightStrings.Add("FIVE");
else if (Time.Minute < 15)
minuteHighlightStrings.Add("TEN");
else if (Time.Minute < 20)
minuteHighlightStrings.Add("QUARTER");
else if (Time.Minute < 25)
minuteHighlightStrings.Add("TWENTY");
else if (Time.Minute < 30)
{
minuteHighlightStrings.Add("TWENTY");
minuteHighlightStrings.Add("FIVE");
}
else if (Time.Minute < 35)
highlightStrings.Add("HALF");
else if (Time.Minute < 40)
{
minuteHighlightStrings.Add("TWENTY");
minuteHighlightStrings.Add("FIVE");
}
else if (Time.Minute < 45)
minuteHighlightStrings.Add("TWENTY");
else if (Time.Minute < 50)
minuteHighlightStrings.Add("QUARTER");
else if (Time.Minute < 55)
minuteHighlightStrings.Add("TEN");
else if (Time.Minute < 60)
minuteHighlightStrings.Add("FIVE");
if (minuteHighlightStrings.Contains("QUARTER"))
highlightFirstA = true;
if (Time.Minute < 35 && Time.Minute >= 5)
highlightStrings.Add("PAST");
else if (Time.Minute >= 35)
highlightStrings.Add("TO");
int hour = Time.Hour;
if (Time.Minute >= 35)
hour++;
if (hour > 12)
hour -= 12;
string[] hourStrings = new string[] { "TWELVE", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE" };
hourHighlightString = hourStrings[hour];
}
public void WriteTimeSquare()
{
SetTimeStrings();
Console.ForegroundColor = Colour2;
for (int lineCounter = 0; lineCounter < clockText.Length - 1; lineCounter++)
{
string lineText = clockText[lineCounter];
for (int charCounter = 0; charCounter < lineText.Length; charCounter++)
{
string remainingText = lineText.Substring(charCounter);
bool found = false;
foreach (string highlightStr in highlightStrings)
if (remainingText.StartsWith(highlightStr))
{
found = true;
Console.ForegroundColor = Colour1;
Console.Write(highlightStr);
Console.ForegroundColor = Colour2;
charCounter += highlightStr.Length - 1;
}
if (lineCounter <= 1)
{
foreach (string highlightStr in minuteHighlightStrings)
if (remainingText.StartsWith(highlightStr))
{
found = true;
Console.ForegroundColor = Colour1;
Console.Write(highlightStr);
Console.ForegroundColor = Colour2;
charCounter += highlightStr.Length - 1;
}
}
else
{
if (remainingText.StartsWith(hourHighlightString))
{
found = true;
Console.ForegroundColor = Colour1;
Console.Write(hourHighlightString);
Console.ForegroundColor = Colour2;
charCounter += hourHighlightString.Length - 1;
}
}
if (!found)
{
if (highlightFirstA && remainingText[0] == 'A')
{
Console.ForegroundColor = Colour1;
Console.Write('A');
Console.ForegroundColor = Colour2;
highlightFirstA = false;
}
else
Console.Write(remainingText[0]);
}
}
Console.Write('\n');
}
string progressText = clockText[clockText.Length - 1];
int intervalLength = 60 * 5;
int current = Time.Minute - ((int)Math.Floor((double)Time.Minute / 5) * 5);
current *= 60;
current += Time.Second;
int numHighlighted = (int)Math.Round(current / (double)(intervalLength / progressText.Length), MidpointRounding.AwayFromZero);
//if (numHighlighted < 0)
// numHighlighted = 0;
Console.ForegroundColor = Colour1;
Console.Write(progressText.Substring(0, numHighlighted));
Console.ForegroundColor = Colour2;
Console.Write(progressText.Substring(numHighlighted));
Console.ResetColor();
}
}
}
