Skip to content

Commit

Permalink
Merge pull request #207 from rprouse/issue/206
Browse files Browse the repository at this point in the history
Month calendars
  • Loading branch information
rprouse authored Nov 29, 2024
2 parents 51ee1f1 + 5dd83cb commit 95e7d6c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 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.3.3</Version>
<Version>6.4.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": "weather daily"
"commandLineArgs": "calendar month"
}
}
}
77 changes: 77 additions & 0 deletions Guppi.Console/Skills/CalendarSkill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.Calendar.v3.Data;
using Guppi.Core.Exceptions;
using Guppi.Core.Extensions;
using Guppi.Core.Interfaces.Services;
Expand Down Expand Up @@ -43,6 +44,13 @@ public IEnumerable<Command> GetCommands()
await Agenda(midnight, "Tomorrow's agenda", markdown, table);
}, markdown, table);

var month = new Command("month", "Displays this month's calendar") { markdown };
month.SetHandler(async (bool markdown) =>
{
if (markdown) await MonthMarkdown();
else Month();
}, markdown);

var free = new Command("free", "Displays free time for a given day");
free.AddArgument(new Argument<DateTime>("date", "The date to check"));
free.Handler = CommandHandler.Create<DateTime>(FreeTime);
Expand All @@ -59,6 +67,7 @@ public IEnumerable<Command> GetCommands()
next,
today,
tomorrow,
month,
free,
logout,
configure
Expand Down Expand Up @@ -260,4 +269,72 @@ private static string JoinLink(Core.Entities.Calendar.Event eventItem) =>

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

private static void Month()
{
(DateOnly start, DateOnly end) = GetMonthRange();

AnsiConsoleHelper.TitleRule($":calendar: {start:MMMM yyyy}");

var table = new Table();
table.Border(TableBorder.Rounded);

table.AddColumn(new TableColumn(new Markup("[yellow]Sun[/]")).RightAligned());
table.AddColumn(new TableColumn(new Markup("[yellow]Mon[/]")).RightAligned());
table.AddColumn(new TableColumn(new Markup("[yellow]Tue[/]")).RightAligned());
table.AddColumn(new TableColumn(new Markup("[yellow]Wed[/]")).RightAligned());
table.AddColumn(new TableColumn(new Markup("[yellow]Thu[/]")).RightAligned());
table.AddColumn(new TableColumn(new Markup("[yellow]Fri[/]")).RightAligned());
table.AddColumn(new TableColumn(new Markup("[yellow]Sat[/]")).RightAligned());

// Add empty cells for the last days of the previous month
var row = Enumerable.Range(0, 7).Select(_ => "").ToArray();
for (var day = start; day <= end; day = day.AddDays(1))
{
row[(int)day.DayOfWeek] = day.Day.ToString();
if (day.DayOfWeek == DayOfWeek.Saturday)
{
table.AddRow(row);
row = Enumerable.Range(0, 7).Select(_ => "").ToArray();
}
}
if (end.DayOfWeek != DayOfWeek.Saturday)
table.AddRow(row);

AnsiConsole.Write(table);

AnsiConsole.WriteLine();
AnsiConsoleHelper.Rule("white");
}

private static async Task MonthMarkdown()
{
(DateOnly start, DateOnly end) = GetMonthRange();
StringBuilder cal = new();
cal.AppendLine("| Day | Date | Habits | Notes |");
cal.AppendLine("| --- | ---- | ------ | ----- |");
for (var day = start; day <= end; day = day.AddDays(1))
{
cal.AppendLine($"| **{day:ddd}** | [[{day:yyyy-MM-dd}]] | | |");
}

AnsiConsoleHelper.TitleRule($":calendar: {start:MMMM yyyy}");

AnsiConsole.WriteLine();
AnsiConsole.WriteLine(cal.ToString());
await TextCopy.ClipboardService.SetTextAsync(cal.ToString());
AnsiConsole.WriteLine();
AnsiConsole.MarkupLine("[green]:green_circle: Copied to clipboard[/]");

AnsiConsole.WriteLine();
AnsiConsoleHelper.Rule("white");
}

private static (DateOnly start, DateOnly end) GetMonthRange()
{
var now = DateTime.Now;
var start = new DateOnly(now.Year, now.Month, 1);
var end = new DateOnly(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));
return (start, end);
}
}

0 comments on commit 95e7d6c

Please sign in to comment.