-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
287 lines (250 loc) · 10.3 KB
/
Copy pathProgram.cs
File metadata and controls
287 lines (250 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
/// <summary>
/// QuickSheet Countdown Timer Extension.
/// Prefix: "cntdn". Usage: "cntdn: 2026-12-25 Christmas" or "cntdn: 2026-06-01 Project Launch"
/// Multiple events: "cntdn: 2026-12-25 Christmas, 2026-07-04 Independence Day, 2026-10-31 Halloween"
/// No params = built-in upcoming holidays for current year.
/// </summary>
class Program
{
private static readonly JsonSerializerOptions JsonOpts = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = false
};
static void Main()
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
string? line;
while ((line = Console.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line)) continue;
try
{
using var doc = JsonDocument.Parse(line);
string? type = doc.RootElement.TryGetProperty("type", out var tp) ? tp.GetString() : null;
switch (type)
{
case "init":
HandleInit();
break;
case "activate":
HandleActivate(doc.RootElement);
break;
case "deactivate":
break;
}
}
catch (Exception ex)
{
SendJson(new { type = "error", id = "", message = $"Parse error: {ex.Message}" });
}
}
}
static void HandleInit()
{
SendJson(new
{
type = "register",
prefix = "cntdn",
name = "Countdown Timer",
version = "1.0.0"
});
SendLog("Countdown Timer registered. Usage: cntdn: YYYY-MM-DD Label, ...");
}
static void HandleActivate(JsonElement root)
{
string id = root.TryGetProperty("id", out var idProp) ? idProp.GetString() ?? "" : "";
string[] extParams = [];
if (root.TryGetProperty("params", out var paramsProp) && paramsProp.ValueKind == JsonValueKind.Array)
{
extParams = paramsProp.EnumerateArray()
.Select(p => p.GetString()?.Trim() ?? "")
.Where(p => p.Length > 0)
.ToArray();
}
try
{
// Join all params back and split by comma for multiple events
string rawInput = string.Join(" ", extParams);
var events = ParseEvents(rawInput);
if (events.Count == 0)
events = GetDefaultHolidays();
// Sort by date
events.Sort((a, b) => a.Date.CompareTo(b.Date));
var cells = new List<(int r, int c, string v)>();
// Header
cells.Add((0, 0, "⏳ Countdown"));
cells.Add((0, 1, "Date"));
cells.Add((0, 2, "Remaining"));
cells.Add((0, 3, "Progress"));
var now = DateTime.Now;
for (int i = 0; i < events.Count; i++)
{
var ev = events[i];
int row = i + 1;
var diff = ev.Date - now;
string remaining;
string icon;
string progress;
if (diff.TotalSeconds <= 0)
{
// Past event
var ago = now - ev.Date;
remaining = FormatTimeSpan(ago) + " ago";
icon = "✅";
progress = "████████████████ 100%";
}
else if (diff.TotalHours < 1)
{
remaining = $"{(int)diff.TotalMinutes}m {diff.Seconds}s";
icon = "🔴";
progress = MakeProgressBar(ev.Date, now, 16);
}
else if (diff.TotalDays < 1)
{
remaining = $"{(int)diff.TotalHours}h {diff.Minutes}m";
icon = "🟡";
progress = MakeProgressBar(ev.Date, now, 16);
}
else if (diff.TotalDays < 7)
{
remaining = $"{(int)diff.TotalDays}d {diff.Hours}h";
icon = "🟡";
progress = MakeProgressBar(ev.Date, now, 16);
}
else if (diff.TotalDays < 30)
{
int weeks = (int)(diff.TotalDays / 7);
int days = (int)(diff.TotalDays % 7);
remaining = $"{weeks}w {days}d";
icon = "🟢";
progress = MakeProgressBar(ev.Date, now, 16);
}
else
{
int months = (int)(diff.TotalDays / 30.44);
int days = (int)(diff.TotalDays % 30.44);
remaining = $"{months}mo {days}d";
icon = "🔵";
progress = MakeProgressBar(ev.Date, now, 16);
}
cells.Add((row, 0, $"{icon} {ev.Label}"));
cells.Add((row, 1, ev.Date.ToString("yyyy-MM-dd")));
cells.Add((row, 2, remaining));
cells.Add((row, 3, progress));
}
// Footer
cells.Add((events.Count + 1, 0, $"Updated {now:HH:mm} · {events.Count} events"));
SendCells(id, cells);
}
catch (Exception ex)
{
SendCells(id, new List<(int r, int c, string v)>
{
(0, 0, $"⚠️ Error: {ex.Message}"),
(1, 0, "Usage: cntdn: 2026-12-25 Christmas, 2026-06-01 Launch")
});
}
}
static List<CountdownEvent> ParseEvents(string input)
{
var events = new List<CountdownEvent>();
if (string.IsNullOrWhiteSpace(input)) return events;
// Split by comma for multiple events
var parts = input.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
foreach (var part in parts)
{
// Expected: "YYYY-MM-DD Label" or "YYYY-MM-DD HH:mm Label"
var tokens = part.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (tokens.Length < 1) continue;
string dateStr = tokens[0];
string label = tokens.Length > 1 ? tokens[1] : "Event";
// Try datetime with time component
if (DateTime.TryParseExact(dateStr + (tokens.Length > 1 && tokens[1].Contains(':') ? " " + tokens[1].Split(' ', 2)[0] : ""),
new[] { "yyyy-MM-dd HH:mm", "yyyy-MM-dd" },
CultureInfo.InvariantCulture, DateTimeStyles.None, out var dt))
{
// If we consumed a time from label, remove it
if (tokens.Length > 1 && tokens[1].Contains(':'))
{
var labelParts = tokens[1].Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
label = labelParts.Length > 1 ? labelParts[1] : "Event";
}
events.Add(new CountdownEvent { Date = dt, Label = label });
}
else if (DateTime.TryParse(dateStr, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
events.Add(new CountdownEvent { Date = dt, Label = label });
}
}
return events;
}
static List<CountdownEvent> GetDefaultHolidays()
{
int year = DateTime.Now.Year;
var now = DateTime.Now;
var holidays = new List<CountdownEvent>
{
new() { Date = new DateTime(year, 1, 1), Label = "New Year's Day" },
new() { Date = new DateTime(year, 2, 14), Label = "Valentine's Day" },
new() { Date = new DateTime(year, 3, 17), Label = "St. Patrick's Day" },
new() { Date = new DateTime(year, 7, 4), Label = "Independence Day" },
new() { Date = new DateTime(year, 10, 31), Label = "Halloween" },
new() { Date = new DateTime(year, 11, 27), Label = "Thanksgiving" },
new() { Date = new DateTime(year, 12, 25), Label = "Christmas" },
new() { Date = new DateTime(year, 12, 31, 23, 59, 59), Label = "New Year's Eve" },
new() { Date = new DateTime(year + 1, 1, 1), Label = $"New Year {year + 1}" },
};
// Show upcoming + recently passed (last 7 days)
return holidays
.Where(h => h.Date > now.AddDays(-7))
.Take(8)
.ToList();
}
static string FormatTimeSpan(TimeSpan ts)
{
if (ts.TotalDays >= 1) return $"{(int)ts.TotalDays}d {ts.Hours}h";
if (ts.TotalHours >= 1) return $"{(int)ts.TotalHours}h {ts.Minutes}m";
return $"{(int)ts.TotalMinutes}m";
}
static string MakeProgressBar(DateTime target, DateTime now, int width)
{
// Progress from "1 year before target" to target
var totalSpan = TimeSpan.FromDays(365);
var elapsed = totalSpan - (target - now);
if (elapsed < TimeSpan.Zero) elapsed = TimeSpan.Zero;
double ratio = Math.Clamp(elapsed / totalSpan, 0, 1);
int filled = (int)(ratio * width);
int empty = width - filled;
string bar = new string('█', filled) + new string('░', empty);
return $"{bar} {ratio * 100:F0}%";
}
static void SendCells(string id, List<(int r, int c, string v)> cells)
{
SendJson(new
{
type = "write",
id,
cells = cells.Select(c => new { r = c.r, c = c.c, v = c.v }).ToArray()
});
}
static void SendJson(object obj)
{
string json = JsonSerializer.Serialize(obj, JsonOpts);
Console.WriteLine(json);
Console.Out.Flush();
}
static void SendLog(string message)
{
SendJson(new { type = "log", message });
}
}
class CountdownEvent
{
public DateTime Date { get; set; }
public string Label { get; set; } = "Event";
}