Skip to content

Commit

Permalink
Display agenda as a markdown table
Browse files Browse the repository at this point in the history
  • Loading branch information
rprouse committed Oct 18, 2024
1 parent e548018 commit 3b7bb23
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Guppi.Console/Guppi.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://github.com/rprouse/guppi</PackageProjectUrl>
<RepositoryUrl>https://github.com/rprouse/guppi</RepositoryUrl>
<PackageId>dotnet-guppi</PackageId>
<Version>6.1.2</Version>
<Version>6.2.0</Version>
<PackAsTool>true</PackAsTool>
<ToolCommandName>guppi</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
Expand Down
2 changes: 1 addition & 1 deletion Guppi.Console/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Guppi.Console": {
"commandName": "Project",
"commandLineArgs": "cal agenda"
"commandLineArgs": "cal agenda -t"
}
}
}
50 changes: 34 additions & 16 deletions Guppi.Console/Skills/CalendarSkill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ internal class CalendarSkill(ICalendarService service) : ISkill

public IEnumerable<Command> GetCommands()
{
var markdown = new Option<bool>(new string[] { "--markdown", "-m" }, "Display as Markdown to be copied into Notes");
var markdown = new Option<bool>(["--markdown", "-m"], "Display as Markdown to be copied into Notes");
var table = new Option<bool>(["--table", "-t"], "Display as a markdown table");

var next = new Command("next", "Views next calendar event") { markdown };
next.AddAlias("view");
next.SetHandler(async (bool markdown) => await Next(markdown), markdown);

var today = new Command("today", "Displays today's agenda") { markdown };
var today = new Command("today", "Displays today's agenda") { markdown, table };
today.AddAlias("agenda");
today.SetHandler(async (bool markdown) =>
today.SetHandler(async (bool markdown, bool table) =>
{
var now = DateTime.Now;
var midnight = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Local);
await Agenda(midnight, "Today's agenda", markdown);
}, markdown);
await Agenda(midnight, "Today's agenda", markdown, table);
}, markdown, table);

var tomorrow = new Command("tomorrow", "Displays tomorrow's agenda") { markdown };
tomorrow.SetHandler(async (bool markdown) =>
var tomorrow = new Command("tomorrow", "Displays tomorrow's agenda") { markdown, table };
tomorrow.SetHandler(async (bool markdown, bool table) =>
{
var now = DateTime.Now.AddDays(1);
var midnight = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Local);
await Agenda(midnight, "Tomorrow's agenda", markdown);
}, markdown);
await Agenda(midnight, "Tomorrow's agenda", markdown, table);
}, markdown, table);

var free = new Command("free", "Displays free time for a given day");
free.AddArgument(new Argument<DateTime>("date", "The date to check"));
Expand Down Expand Up @@ -99,7 +101,7 @@ private async Task Next(bool markdown)
var line = $"- **{start}{end}** {eventItem.Summary}{JoinLink(eventItem)}";
sb.AppendLine(line);
AnsiConsole.WriteLine(line);
TextCopy.ClipboardService.SetText(sb.ToString());
await TextCopy.ClipboardService.SetTextAsync(sb.ToString());
AnsiConsole.MarkupLine("[green]:green_circle: Copied to clipboard[/]");
AnsiConsole.WriteLine();
}
Expand All @@ -126,7 +128,7 @@ private async Task Next(bool markdown)
}
}

private async Task Agenda(DateTime now, string title, bool markdown)
private async Task Agenda(DateTime now, string title, bool markdown, bool table)
{
try
{
Expand All @@ -135,21 +137,27 @@ private async Task Agenda(DateTime now, string title, bool markdown)
AnsiConsoleHelper.TitleRule($":calendar: {title}");
StringBuilder sb = new();

if (table)
{
sb.AppendLine("| Time | Meeting | Participants | Objective |");
sb.AppendLine("| ---- | ------- | ------------ | --------- |");
}

try
{
if (events.Any())
{
bool found = false;
foreach (var _ in events.Where(eventItem => DisplayEvent(eventItem, markdown, sb)).Select(eventItem => new { }))
foreach (var _ in events.Where(eventItem => DisplayEvent(eventItem, markdown, table, sb)).Select(eventItem => new { }))
{
found = true;
}

if (found)
{
if (markdown)
if (markdown || table)
{
TextCopy.ClipboardService.SetText(sb.ToString());
await TextCopy.ClipboardService.SetTextAsync(sb.ToString());
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[green]:green_circle: Copied to clipboard[/]");
}
Expand All @@ -175,15 +183,22 @@ private async Task Agenda(DateTime now, string title, bool markdown)
}
}

private static bool DisplayEvent(Core.Entities.Calendar.Event eventItem, bool markdown, StringBuilder markdownBuffer)
private static bool DisplayEvent(Core.Entities.Calendar.Event eventItem, bool markdown, bool table, StringBuilder markdownBuffer)
{
string start = eventItem.Start?.ToString("HH:mm");
if (string.IsNullOrEmpty(start))
{
return false;
}
string end = eventItem.End?.ToString("-HH:mm") ?? "";
if (markdown)

if (table)
{
var line = $"| {eventItem.Start.GetEmoji()} **{start}{end}** | {TableLinkedSummary(eventItem)} | | |";
markdownBuffer.AppendLine(line);
AnsiConsole.WriteLine(line);
}
else if (markdown)
{
var line = $"- {eventItem.Start.GetEmoji()} **{start}{end}** {eventItem.Summary}{JoinLink(eventItem)}";
markdownBuffer.AppendLine(line);
Expand Down Expand Up @@ -241,4 +256,7 @@ private async Task FreeTime(DateTime date)

private static string JoinLink(Core.Entities.Calendar.Event eventItem) =>
string.IsNullOrEmpty(eventItem.MeetingUrl) ? "" : $" [Join]({eventItem.MeetingUrl})";

private static string TableLinkedSummary(Core.Entities.Calendar.Event eventItem) =>
string.IsNullOrEmpty(eventItem.MeetingUrl) ? eventItem.Summary : $"[{eventItem.Summary}]({eventItem.MeetingUrl})";
}

0 comments on commit 3b7bb23

Please sign in to comment.