From 4a3b262b5036a220caa195f5bc5992f79d8d9ab7 Mon Sep 17 00:00:00 2001 From: Jonas Lima de Amorim Date: Tue, 8 Sep 2026 14:44:46 -0300 Subject: [PATCH 1/5] feat(summary): default to current month when no period is given --- Timetracker/Options/SummaryOptions.cs | 10 ++++++++-- Timetracker/Program.cs | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Timetracker/Options/SummaryOptions.cs b/Timetracker/Options/SummaryOptions.cs index f7208fd..5a5afa4 100644 --- a/Timetracker/Options/SummaryOptions.cs +++ b/Timetracker/Options/SummaryOptions.cs @@ -5,10 +5,10 @@ namespace Timetracker.Options; [Verb("summary", HelpText = "Show a daily summary of logged hours for a given period.")] public class SummaryOptions : IPeriodOptions { - [Option('f', "from", Required = false, HelpText = "Start date in the format YYYY/MM/DD, 'today' or 'yesterday'. Defaults to today.")] + [Option('f', "from", Required = false, HelpText = "Start date in the format YYYY/MM/DD, 'today' or 'yesterday'. Defaults to the start of the current month.")] public string From { get; set; } - [Option('t', "to", Required = false, HelpText = "End date in the format YYYY/MM/DD, 'today' or 'yesterday'. Defaults to today.")] + [Option('t', "to", Required = false, HelpText = "End date in the format YYYY/MM/DD, 'today' or 'yesterday'. Defaults to the end of the current month.")] public string To { get; set; } [Option('p', "period", Required = false, HelpText = "Summarize a specific month in the format YYYY/MM (e.g., 2026/06). Cannot be used with --from, --to or week/month shortcuts.")] @@ -17,6 +17,9 @@ public class SummaryOptions : IPeriodOptions [Option("month", Required = false, HelpText = "Summarize the current month. Cannot be used with --from, --to, --period or other shortcuts.")] public bool Month { get; set; } + [Option("current-month", Required = false, HelpText = "Alias for --month. Summarize the current month.")] + public bool CurrentMonth { get; set; } + [Option("last-month", Required = false, HelpText = "Summarize the previous month. Cannot be used with --from, --to, --period or other shortcuts.")] public bool LastMonth { get; set; } @@ -29,6 +32,9 @@ public class SummaryOptions : IPeriodOptions [Option("week", Required = false, HelpText = "Summarize the current week (Monday to Sunday). Cannot be used with --from, --to or --period.")] public bool Week { get; set; } + [Option("current-week", Required = false, HelpText = "Alias for --week. Summarize the current week (Monday to Sunday).")] + public bool CurrentWeek { get; set; } + [Option("last-week", Required = false, HelpText = "Summarize the previous week (Monday to Sunday). Cannot be used with --from, --to or --period.")] public bool LastWeek { get; set; } diff --git a/Timetracker/Program.cs b/Timetracker/Program.cs index 1aa68fb..32c14a8 100644 --- a/Timetracker/Program.cs +++ b/Timetracker/Program.cs @@ -407,6 +407,16 @@ static async Task SummaryAction(SummaryOptions opts, CancellationToken canc return 1; } + var noPeriodSpecified = !opts.Today && !opts.Yesterday + && !opts.Week && !opts.CurrentWeek && !opts.LastWeek + && !opts.Month && !opts.CurrentMonth && !opts.LastMonth + && string.IsNullOrEmpty(opts.Period) + && string.IsNullOrEmpty(opts.From) + && string.IsNullOrEmpty(opts.To); + + if (noPeriodSpecified) + opts.Month = true; + if (!PeriodResolver.TryResolve(opts, out var from, out var to, out var periodError)) { ConsoleHelper.WriteError(periodError); From e899fa71d04679a4ac8cfa65a7df9daa42a55cd5 Mon Sep 17 00:00:00 2001 From: Jonas Lima de Amorim Date: Tue, 8 Sep 2026 14:45:09 -0300 Subject: [PATCH 2/5] feat(period): add --current-month and --current-week aliases Mirrors --month and --week for a more explicit naming style. Covers list, summary, and interactive commands. --- README.md | 14 ++--- Timetracker.Tests/FakePeriodOptions.cs | 2 + Timetracker.Tests/PeriodResolverTests.cs | 68 ++++++++++++++++++++++++ Timetracker/Options/IPeriodOptions.cs | 2 + Timetracker/Options/ListOptions.cs | 6 +++ Timetracker/Utils/PeriodResolver.cs | 11 ++-- 6 files changed, 92 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e372d87..a13a03b 100644 --- a/README.md +++ b/README.md @@ -162,13 +162,13 @@ List time entries for a period. | `--output` | `-o` | no | Output format: `json` (batch-upload compatible) or `csv` | | `--today` | | no | Shortcut for today's entries | | `--yesterday` | | no | Shortcut for yesterday's entries | -| `--week` | | no | Entries for the current week (Mon–Sun) | +| `--week` / `--current-week` | | no | Entries for the current week (Mon–Sun) | | `--last-week` | | no | Entries for the previous week (Mon–Sun) | -| `--month` | | no | Entries for the current month | +| `--month` / `--current-month` | | no | Entries for the current month | | `--last-month` | | no | Entries for the previous month | | `--ids` | | no | Show entry IDs in the last column, in place of comments | -All period shortcuts (`--today`, `--yesterday`, `--week`, `--last-week`, `--month`, `--last-month`) and `--period` are mutually exclusive and cannot be combined with `--from` or `--to`. +All period shortcuts (`--today`, `--yesterday`, `--week`, `--current-week`, `--last-week`, `--month`, `--current-month`, `--last-month`) and `--period` are mutually exclusive and cannot be combined with `--from` or `--to`. ```bash # Today's entries @@ -222,9 +222,9 @@ Accepts the same period options as `list`. | `--output` | `-o` | no | Output format: `json` or `csv`. Defaults to a table | | `--today` | | no | Today | | `--yesterday` | | no | Yesterday | -| `--week` | | no | Current week (Mon–Sun) | +| `--week` / `--current-week` | | no | Current week (Mon–Sun) | | `--last-week` | | no | Previous week (Mon–Sun) | -| `--month` | | no | Current month | +| `--month` / `--current-month` | | no | Current month (default when no period is given) | | `--last-month` | | no | Previous month | ```bash @@ -256,9 +256,9 @@ Browse, create, copy, edit and delete time entries in an interactive terminal UI | `--work-item` | `-w` | no | Filter by Work Item ID | | `--today` | | no | Entries for today (default) | | `--yesterday` | | no | Entries for yesterday | -| `--week` | | no | Entries for the current week | +| `--week` / `--current-week` | | no | Entries for the current week | | `--last-week` | | no | Entries for the previous week | -| `--month` | | no | Entries for the current month | +| `--month` / `--current-month` | | no | Entries for the current month | | `--last-month` | | no | Entries for the previous month | ```bash diff --git a/Timetracker.Tests/FakePeriodOptions.cs b/Timetracker.Tests/FakePeriodOptions.cs index f27faf7..297fd54 100644 --- a/Timetracker.Tests/FakePeriodOptions.cs +++ b/Timetracker.Tests/FakePeriodOptions.cs @@ -11,7 +11,9 @@ internal sealed class FakePeriodOptions : IPeriodOptions public bool Today { get; init; } public bool Yesterday { get; init; } public bool Week { get; init; } + public bool CurrentWeek { get; init; } public bool LastWeek { get; init; } public bool Month { get; init; } + public bool CurrentMonth { get; init; } public bool LastMonth { get; init; } } diff --git a/Timetracker.Tests/PeriodResolverTests.cs b/Timetracker.Tests/PeriodResolverTests.cs index 8389c7a..2cf7ee3 100644 --- a/Timetracker.Tests/PeriodResolverTests.cs +++ b/Timetracker.Tests/PeriodResolverTests.cs @@ -176,4 +176,72 @@ public void LastMonth_CoversFirstToLastDayOfPreviousMonth() Assert.Equal(from.AddMonths(1).AddDays(-1), to); Assert.Equal(DateTime.Today.Month, from.AddMonths(1).Month); } + + // --- current-* aliases ------------------------------------------------- + + [Fact] + public void CurrentMonth_ResolvesIdenticallyToMonth() + { + PeriodResolver.TryResolve(new FakePeriodOptions { Month = true }, out var mFrom, out var mTo, out _); + var ok = PeriodResolver.TryResolve(new FakePeriodOptions { CurrentMonth = true }, out var from, out var to, out _); + + Assert.True(ok); + Assert.Equal(mFrom, from); + Assert.Equal(mTo, to); + } + + [Fact] + public void CurrentWeek_ResolvesIdenticallyToWeek() + { + PeriodResolver.TryResolve(new FakePeriodOptions { Week = true }, out var wFrom, out var wTo, out _); + var ok = PeriodResolver.TryResolve(new FakePeriodOptions { CurrentWeek = true }, out var from, out var to, out _); + + Assert.True(ok); + Assert.Equal(wFrom, from); + Assert.Equal(wTo, to); + } + + [Fact] + public void CurrentMonth_AndMonth_AreMutuallyExclusive() + { + var opts = new FakePeriodOptions { Month = true, CurrentMonth = true }; + + var ok = PeriodResolver.TryResolve(opts, out _, out _, out var error); + + Assert.False(ok); + Assert.Contains("mutually exclusive", error); + } + + [Fact] + public void CurrentWeek_AndWeek_AreMutuallyExclusive() + { + var opts = new FakePeriodOptions { Week = true, CurrentWeek = true }; + + var ok = PeriodResolver.TryResolve(opts, out _, out _, out var error); + + Assert.False(ok); + Assert.Contains("mutually exclusive", error); + } + + [Fact] + public void CurrentMonth_WithFromOrTo_Fail() + { + var opts = new FakePeriodOptions { CurrentMonth = true, From = "2026/06/01" }; + + var ok = PeriodResolver.TryResolve(opts, out _, out _, out var error); + + Assert.False(ok); + Assert.Contains("shortcuts cannot be used", error); + } + + [Fact] + public void CurrentWeek_WithFromOrTo_Fail() + { + var opts = new FakePeriodOptions { CurrentWeek = true, To = "2026/06/30" }; + + var ok = PeriodResolver.TryResolve(opts, out _, out _, out var error); + + Assert.False(ok); + Assert.Contains("shortcuts cannot be used", error); + } } diff --git a/Timetracker/Options/IPeriodOptions.cs b/Timetracker/Options/IPeriodOptions.cs index 9ff7a66..06207a2 100644 --- a/Timetracker/Options/IPeriodOptions.cs +++ b/Timetracker/Options/IPeriodOptions.cs @@ -13,5 +13,7 @@ public interface IPeriodOptions bool Week { get; } bool LastWeek { get; } bool Month { get; } + bool CurrentMonth { get; } bool LastMonth { get; } + bool CurrentWeek { get; } } diff --git a/Timetracker/Options/ListOptions.cs b/Timetracker/Options/ListOptions.cs index 3c862f8..771979c 100644 --- a/Timetracker/Options/ListOptions.cs +++ b/Timetracker/Options/ListOptions.cs @@ -17,6 +17,9 @@ public class ListOptions : IPeriodOptions [Option("month", Required = false, HelpText = "Show entries for the current month. Cannot be used with --from, --to, --period or other shortcuts.")] public bool Month { get; set; } + [Option("current-month", Required = false, HelpText = "Alias for --month. Show entries for the current month.")] + public bool CurrentMonth { get; set; } + [Option("last-month", Required = false, HelpText = "Show entries for the previous month. Cannot be used with --from, --to, --period or other shortcuts.")] public bool LastMonth { get; set; } @@ -38,6 +41,9 @@ public class ListOptions : IPeriodOptions [Option("week", Required = false, HelpText = "Show entries for the current week (Monday to Sunday). Cannot be used with --from, --to or --period.")] public bool Week { get; set; } + [Option("current-week", Required = false, HelpText = "Alias for --week. Show entries for the current week (Monday to Sunday).")] + public bool CurrentWeek { get; set; } + [Option("last-week", Required = false, HelpText = "Show entries for the previous week (Monday to Sunday). Cannot be used with --from, --to or --period.")] public bool LastWeek { get; set; } } diff --git a/Timetracker/Utils/PeriodResolver.cs b/Timetracker/Utils/PeriodResolver.cs index 72aa15a..88959f0 100644 --- a/Timetracker/Utils/PeriodResolver.cs +++ b/Timetracker/Utils/PeriodResolver.cs @@ -14,7 +14,8 @@ public static bool TryResolve(IPeriodOptions opts, out DateTime from, out DateTi var periodFlags = new[] { - opts.Today, opts.Yesterday, opts.Week, opts.LastWeek, opts.Month, opts.LastMonth, + opts.Today, opts.Yesterday, opts.Week, opts.CurrentWeek, opts.LastWeek, + opts.Month, opts.CurrentMonth, opts.LastMonth, !string.IsNullOrEmpty(opts.Period) }.Count(x => x); @@ -24,7 +25,9 @@ public static bool TryResolve(IPeriodOptions opts, out DateTime from, out DateTi return false; } - var usesShortcut = opts.Today || opts.Yesterday || opts.Week || opts.LastWeek || opts.Month || opts.LastMonth; + var usesShortcut = opts.Today || opts.Yesterday + || opts.Week || opts.CurrentWeek || opts.LastWeek + || opts.Month || opts.CurrentMonth || opts.LastMonth; if (usesShortcut && (!string.IsNullOrEmpty(opts.From) || !string.IsNullOrEmpty(opts.To))) { @@ -44,7 +47,7 @@ public static bool TryResolve(IPeriodOptions opts, out DateTime from, out DateTi return true; } - if (opts.Week) + if (opts.Week || opts.CurrentWeek) { (from, to) = ValidationUtils.ResolveCurrentWeek(); return true; @@ -56,7 +59,7 @@ public static bool TryResolve(IPeriodOptions opts, out DateTime from, out DateTi return true; } - if (opts.Month) + if (opts.Month || opts.CurrentMonth) { (from, to) = ValidationUtils.ResolveCurrentMonth(); return true; From e540a5c408fd8839c7a695e3a6ea536440567b8a Mon Sep 17 00:00:00 2001 From: Jonas Lima de Amorim Date: Tue, 8 Sep 2026 14:45:14 -0300 Subject: [PATCH 3/5] refactor(interactive): implement IPeriodOptions and use PeriodResolver Eliminates duplicated period resolution logic in InteractiveAction. --- Timetracker/Options/InteractiveOptions.cs | 12 ++++++++++- Timetracker/Program.cs | 25 ++++------------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/Timetracker/Options/InteractiveOptions.cs b/Timetracker/Options/InteractiveOptions.cs index f665ab8..c648515 100644 --- a/Timetracker/Options/InteractiveOptions.cs +++ b/Timetracker/Options/InteractiveOptions.cs @@ -3,11 +3,15 @@ namespace Timetracker.Options; [Verb("interactive", HelpText = "Interactively browse, edit and delete time entries.")] -public class InteractiveOptions +public class InteractiveOptions : IPeriodOptions { [Option('p', "period", Required = false, HelpText = "Specific month in YYYY/MM format (e.g., 2026/06).")] public string Period { get; set; } + // Interactive mode does not support explicit date ranges. + string IPeriodOptions.From => null; + string IPeriodOptions.To => null; + [Option('w', "work-item", Required = false, HelpText = "Filter entries by Work Item ID.")] public int? WorkItemId { get; set; } @@ -20,12 +24,18 @@ public class InteractiveOptions [Option("week", Required = false, HelpText = "Show entries for the current week.")] public bool Week { get; set; } + [Option("current-week", Required = false, HelpText = "Alias for --week. Show entries for the current week.")] + public bool CurrentWeek { get; set; } + [Option("last-week", Required = false, HelpText = "Show entries for the previous week.")] public bool LastWeek { get; set; } [Option("month", Required = false, HelpText = "Show entries for the current month.")] public bool Month { get; set; } + [Option("current-month", Required = false, HelpText = "Alias for --month. Show entries for the current month.")] + public bool CurrentMonth { get; set; } + [Option("last-month", Required = false, HelpText = "Show entries for the previous month.")] public bool LastMonth { get; set; } } diff --git a/Timetracker/Program.cs b/Timetracker/Program.cs index 32c14a8..a2ebd46 100644 --- a/Timetracker/Program.cs +++ b/Timetracker/Program.cs @@ -650,28 +650,11 @@ static async Task InteractiveAction(InteractiveOptions opts, CancellationTo return 1; } - DateTime from, to; - - if (opts.Yesterday) - from = to = DateTime.Today.AddDays(-1); - else if (opts.Week) - (from, to) = ValidationUtils.ResolveCurrentWeek(); - else if (opts.LastWeek) - (from, to) = ValidationUtils.ResolveLastWeek(); - else if (opts.Month) - (from, to) = ValidationUtils.ResolveCurrentMonth(); - else if (opts.LastMonth) - (from, to) = ValidationUtils.ResolveLastMonth(); - else if (!string.IsNullOrEmpty(opts.Period)) - { - if (!ValidationUtils.TryResolveMonth(opts.Period, out from, out to)) - { - ConsoleHelper.WriteError("Invalid period format. Use YYYY/MM (e.g., 2026/06)."); - return 1; - } + if (!PeriodResolver.TryResolve(opts, out var from, out var to, out var periodError)) + { + ConsoleHelper.WriteError(periodError); + return 1; } - else - from = to = DateTime.Today; var config = ConfigService.LoadConfig(); var activities = ActivityService.GetActivities(); From 133506587a1866f15774425d950c271b73140f05 Mon Sep 17 00:00:00 2001 From: Jonas Lima de Amorim Date: Tue, 8 Sep 2026 15:03:51 -0300 Subject: [PATCH 4/5] feat(config): add --week-start option (sunday default, monday alt) --- README.md | 5 ++ Timetracker.Tests/PeriodResolverTests.cs | 7 ++- Timetracker.Tests/ResolveDateTests.cs | 66 +++++++++++++++++++++-- Timetracker/Options/ConfigOptions.cs | 3 ++ Timetracker/Program.cs | 13 +++-- Timetracker/Services/ConfigService.cs | 24 ++++++++- Timetracker/Utils/ValidationUtils.cs | 15 ++++-- Timetracker/Validators/ConfigValidator.cs | 12 ++++- 8 files changed, 130 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index a13a03b..4f1900a 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ Configure the connection to your Timetracker instance. | `--url` | `-u` | yes* | Base URL of your Timetracker instance | | `--token` | `-t` | yes* | Bearer token for authentication | | `--border` | | no | Table border style for list output: `minimal` (default), `square`, or `markdown` | +| `--week-start` | | no | First day of the week for `--week`/`--last-week`: `sunday` (default) or `monday` | | `--show` | | no | Display current config (token masked) | | `--reset` | | no | Delete all local config and activity cache | @@ -90,6 +91,10 @@ timetracker config -t eyJnew... # Change the list table border (no network call, credentials untouched) timetracker config --border square +# Set week start to Sunday (default) or Monday +timetracker config --week-start sunday +timetracker config --week-start monday + # Remove all local config timetracker config --reset ``` diff --git a/Timetracker.Tests/PeriodResolverTests.cs b/Timetracker.Tests/PeriodResolverTests.cs index 2cf7ee3..3236e7f 100644 --- a/Timetracker.Tests/PeriodResolverTests.cs +++ b/Timetracker.Tests/PeriodResolverTests.cs @@ -2,8 +2,9 @@ namespace Timetracker.Tests; -public class PeriodResolverTests +public class PeriodResolverTests : IDisposable { + public void Dispose() => ValidationUtils.WeekStartOverride = null; // --- Error paths ------------------------------------------------------- [Fact] @@ -137,6 +138,8 @@ public void Period_ResolvesToWholeMonth() [Fact] public void Week_IsMondayToSunday() { + ValidationUtils.WeekStartOverride = DayOfWeek.Monday; + var ok = PeriodResolver.TryResolve(new FakePeriodOptions { Week = true }, out var from, out var to, out _); Assert.True(ok); @@ -148,6 +151,8 @@ public void Week_IsMondayToSunday() [Fact] public void LastWeek_IsExactlySevenDaysBeforeThisWeek() { + ValidationUtils.WeekStartOverride = DayOfWeek.Monday; + PeriodResolver.TryResolve(new FakePeriodOptions { Week = true }, out var weekFrom, out var weekTo, out _); PeriodResolver.TryResolve(new FakePeriodOptions { LastWeek = true }, out var lastFrom, out var lastTo, out _); diff --git a/Timetracker.Tests/ResolveDateTests.cs b/Timetracker.Tests/ResolveDateTests.cs index 0e0c4ad..cce7245 100644 --- a/Timetracker.Tests/ResolveDateTests.cs +++ b/Timetracker.Tests/ResolveDateTests.cs @@ -8,7 +8,11 @@ namespace Timetracker.Tests; /// public sealed class ResolveDateTests : IDisposable { - public void Dispose() => ValidationUtils.Clock = TimeProvider.System; + public void Dispose() + { + ValidationUtils.Clock = TimeProvider.System; + ValidationUtils.WeekStartOverride = null; + } private static void PinTo(int year, int month, int day) => ValidationUtils.Clock = new FixedClock(year, month, day); @@ -31,10 +35,11 @@ public void ResolveDate_Yesterday_UsesTheClock() } [Fact] - public void ResolveCurrentWeek_MidWeek_ReturnsThatWeeksMondayToSunday() + public void ResolveCurrentWeek_MondayStart_MidWeek_ReturnsMondayToSunday() { // 2026-03-18 is a Wednesday. PinTo(2026, 3, 18); + ValidationUtils.WeekStartOverride = DayOfWeek.Monday; var (from, to) = ValidationUtils.ResolveCurrentWeek(); @@ -43,10 +48,11 @@ public void ResolveCurrentWeek_MidWeek_ReturnsThatWeeksMondayToSunday() } [Fact] - public void ResolveCurrentWeek_OnSunday_ReturnsTheEndingWeek() + public void ResolveCurrentWeek_MondayStart_OnSunday_ReturnsTheEndingWeek() { // 2026-03-22 is a Sunday — the week should still be 03-16..03-22. PinTo(2026, 3, 22); + ValidationUtils.WeekStartOverride = DayOfWeek.Monday; var (from, to) = ValidationUtils.ResolveCurrentWeek(); @@ -55,9 +61,10 @@ public void ResolveCurrentWeek_OnSunday_ReturnsTheEndingWeek() } [Fact] - public void ResolveLastWeek_ReturnsPreviousMondayToSunday() + public void ResolveLastWeek_MondayStart_ReturnsPreviousMondayToSunday() { PinTo(2026, 3, 18); + ValidationUtils.WeekStartOverride = DayOfWeek.Monday; var (from, to) = ValidationUtils.ResolveLastWeek(); @@ -65,6 +72,57 @@ public void ResolveLastWeek_ReturnsPreviousMondayToSunday() Assert.Equal(new DateTime(2026, 3, 15), to); } + [Fact] + public void ResolveCurrentWeek_SundayStart_MidWeek_ReturnsSundayToSaturday() + { + // 2026-03-18 is a Wednesday; week should be 03-15 (Sun) .. 03-21 (Sat). + PinTo(2026, 3, 18); + ValidationUtils.WeekStartOverride = DayOfWeek.Sunday; + + var (from, to) = ValidationUtils.ResolveCurrentWeek(); + + Assert.Equal(new DateTime(2026, 3, 15), from); // Sunday + Assert.Equal(new DateTime(2026, 3, 21), to); // Saturday + } + + [Fact] + public void ResolveCurrentWeek_SundayStart_OnSunday_StartsOnThatDay() + { + // 2026-03-15 is a Sunday — should be the start of the week. + PinTo(2026, 3, 15); + ValidationUtils.WeekStartOverride = DayOfWeek.Sunday; + + var (from, to) = ValidationUtils.ResolveCurrentWeek(); + + Assert.Equal(new DateTime(2026, 3, 15), from); + Assert.Equal(new DateTime(2026, 3, 21), to); + } + + [Fact] + public void ResolveCurrentWeek_SundayStart_OnSaturday_ReturnsCurrentWeek() + { + // 2026-03-21 is a Saturday — last day of the Sun-start week. + PinTo(2026, 3, 21); + ValidationUtils.WeekStartOverride = DayOfWeek.Sunday; + + var (from, to) = ValidationUtils.ResolveCurrentWeek(); + + Assert.Equal(new DateTime(2026, 3, 15), from); + Assert.Equal(new DateTime(2026, 3, 21), to); + } + + [Fact] + public void ResolveLastWeek_SundayStart_ReturnsPreviousSundayToSaturday() + { + PinTo(2026, 3, 18); + ValidationUtils.WeekStartOverride = DayOfWeek.Sunday; + + var (from, to) = ValidationUtils.ResolveLastWeek(); + + Assert.Equal(new DateTime(2026, 3, 8), from); // Sunday + Assert.Equal(new DateTime(2026, 3, 14), to); // Saturday + } + [Fact] public void ResolveCurrentMonth_ReturnsFirstToLastDay() { diff --git a/Timetracker/Options/ConfigOptions.cs b/Timetracker/Options/ConfigOptions.cs index 35b28aa..a2154c9 100644 --- a/Timetracker/Options/ConfigOptions.cs +++ b/Timetracker/Options/ConfigOptions.cs @@ -19,4 +19,7 @@ public class ConfigOptions [Option("border", Required = false, HelpText = "Table border style for list output: minimal (default), square, or markdown.")] public string Border { get; set; } + + [Option("week-start", Required = false, HelpText = "First day of the week for --week and --last-week shortcuts: sunday (default) or monday.")] + public string WeekStart { get; set; } } diff --git a/Timetracker/Program.cs b/Timetracker/Program.cs index a2ebd46..864af3d 100644 --- a/Timetracker/Program.cs +++ b/Timetracker/Program.cs @@ -91,7 +91,8 @@ async Task ConfigAction(ConfigOptions opts, CancellationToken cancellationT Console.WriteLine($"URL: {config.TimetrackerUrl}"); Console.WriteLine($"Token: {maskedToken}"); Console.WriteLine($"UserId: {config.TimetrackerUserId}"); - Console.WriteLine($"Border: {config.TableBorder ?? "minimal"}"); + Console.WriteLine($"Border: {config.TableBorder ?? "minimal"}"); + Console.WriteLine($"Week start: {config.WeekStart ?? "sunday"}"); return 0; } @@ -129,9 +130,13 @@ async Task ConfigAction(ConfigOptions opts, CancellationToken cancellationT // Partial update (e.g. only --border): merge into the existing config, no network call. ConfigService.SaveConfig(opts); - ConsoleHelper.WriteSuccess(!string.IsNullOrEmpty(opts.Border) - ? $"Table border set to '{opts.Border.ToLowerInvariant()}'." - : "Configuration updated."); + var message = (!string.IsNullOrEmpty(opts.Border), !string.IsNullOrEmpty(opts.WeekStart)) switch + { + (true, _) => $"Table border set to '{opts.Border.ToLowerInvariant()}'.", + (_, true) => $"Week start set to '{opts.WeekStart.ToLowerInvariant()}'.", + _ => "Configuration updated." + }; + ConsoleHelper.WriteSuccess(message); return 0; } diff --git a/Timetracker/Services/ConfigService.cs b/Timetracker/Services/ConfigService.cs index ae21c8a..cca661f 100644 --- a/Timetracker/Services/ConfigService.cs +++ b/Timetracker/Services/ConfigService.cs @@ -17,6 +17,7 @@ public record Config public string AccountName { get; set; } public bool TokenEncrypted { get; set; } public string TableBorder { get; set; } + public string WeekStart { get; set; } } public static class ConfigService @@ -83,7 +84,8 @@ public static void SaveConfig(ConfigOptions opts, string userId = null, string d DisplayName = displayName ?? existing.DisplayName, Email = email ?? existing.Email, AccountName = accountName ?? existing.AccountName, - TableBorder = opts.Border?.ToLowerInvariant() ?? existing.TableBorder + TableBorder = opts.Border?.ToLowerInvariant() ?? existing.TableBorder, + WeekStart = opts.WeekStart?.ToLowerInvariant() ?? existing.WeekStart }; WriteConfig(config); @@ -106,6 +108,26 @@ private static void WriteConfig(Config config) File.SetUnixFileMode(configPath, UnixFileMode.UserRead | UnixFileMode.UserWrite); } + /// Returns the configured week start day; defaults to when unset. + public static DayOfWeek GetWeekStart() + { + var configPath = GetConfigPath(); + if (!File.Exists(configPath)) + return DayOfWeek.Sunday; + + try + { + var value = JsonConvert.DeserializeObject(File.ReadAllText(configPath))?.WeekStart; + return string.Equals(value, "monday", StringComparison.OrdinalIgnoreCase) + ? DayOfWeek.Monday + : DayOfWeek.Sunday; + } + catch + { + return DayOfWeek.Sunday; + } + } + /// Reads the configured table border without decrypting the token; null if unset. public static string GetTableBorder() { diff --git a/Timetracker/Utils/ValidationUtils.cs b/Timetracker/Utils/ValidationUtils.cs index 90df923..07171b4 100644 --- a/Timetracker/Utils/ValidationUtils.cs +++ b/Timetracker/Utils/ValidationUtils.cs @@ -1,5 +1,6 @@ using System.Globalization; using System.Text.RegularExpressions; +using Timetracker.Services; namespace Timetracker.Utils; @@ -11,6 +12,13 @@ public static class ValidationUtils /// internal static TimeProvider Clock { get; set; } = TimeProvider.System; + /// + /// First day of the week. Defaults to reading from config; tests can override directly. + /// + internal static DayOfWeek? WeekStartOverride { get; set; } + + private static DayOfWeek WeekStart => WeekStartOverride ?? ConfigService.GetWeekStart(); + private static DateTime Today => Clock.GetLocalNow().Date; public static bool ValidDate(string date) => DateTime.TryParse(date, out _); @@ -45,9 +53,10 @@ public static DateTime ResolveDate(string input) public static (DateTime From, DateTime To) ResolveCurrentWeek() { var today = Today; - var diff = (7 + (today.DayOfWeek - DayOfWeek.Monday)) % 7; - var monday = today.AddDays(-diff); - return (monday, monday.AddDays(6)); + var start = WeekStart; + var diff = (7 + (today.DayOfWeek - start)) % 7; + var weekStart = today.AddDays(-diff); + return (weekStart, weekStart.AddDays(6)); } public static (DateTime From, DateTime To) ResolveLastWeek() diff --git a/Timetracker/Validators/ConfigValidator.cs b/Timetracker/Validators/ConfigValidator.cs index f96f4de..43baba3 100644 --- a/Timetracker/Validators/ConfigValidator.cs +++ b/Timetracker/Validators/ConfigValidator.cs @@ -13,8 +13,9 @@ public ConfigValidator() .Must(x => x.Show || x.Reset || !string.IsNullOrEmpty(x.TimetrackerUrl) || !string.IsNullOrEmpty(x.TimetrackerBearerToken) - || !string.IsNullOrEmpty(x.Border)) - .WithMessage("Provide at least one option: --url, --token, --border, --show, or --reset."); + || !string.IsNullOrEmpty(x.Border) + || !string.IsNullOrEmpty(x.WeekStart)) + .WithMessage("Provide at least one option: --url, --token, --border, --week-start, --show, or --reset."); // Credentials are only mandatory on first-time setup. Once configured, any // option can be updated on its own without re-entering the others. @@ -40,5 +41,12 @@ public ConfigValidator() .Must(b => b.ToLowerInvariant() is "minimal" or "square" or "markdown") .WithMessage("--border must be one of: minimal, square, markdown."); }); + + When(x => !string.IsNullOrEmpty(x.WeekStart), () => + { + RuleFor(x => x.WeekStart) + .Must(w => w.ToLowerInvariant() is "sunday" or "monday") + .WithMessage("--week-start must be one of: sunday, monday."); + }); } } From 7e88060ae95d731fc4a9bd22c487d3e4b2d1dab1 Mon Sep 17 00:00:00 2001 From: Jonas Lima de Amorim Date: Tue, 8 Sep 2026 15:08:57 -0300 Subject: [PATCH 5/5] feat(config): add ascii border style option --- README.md | 3 ++- Timetracker/Options/ConfigOptions.cs | 2 +- Timetracker/Utils/TableHelper.cs | 5 +++-- Timetracker/Validators/ConfigValidator.cs | 4 ++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4f1900a..380cc6d 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ Configure the connection to your Timetracker instance. |---|---|---|---| | `--url` | `-u` | yes* | Base URL of your Timetracker instance | | `--token` | `-t` | yes* | Bearer token for authentication | -| `--border` | | no | Table border style for list output: `minimal` (default), `square`, or `markdown` | +| `--border` | | no | Table border style for list output: `minimal` (default), `square`, `markdown`, or `ascii` | | `--week-start` | | no | First day of the week for `--week`/`--last-week`: `sunday` (default) or `monday` | | `--show` | | no | Display current config (token masked) | | `--reset` | | no | Delete all local config and activity cache | @@ -90,6 +90,7 @@ timetracker config -t eyJnew... # Change the list table border (no network call, credentials untouched) timetracker config --border square +timetracker config --border ascii # Set week start to Sunday (default) or Monday timetracker config --week-start sunday diff --git a/Timetracker/Options/ConfigOptions.cs b/Timetracker/Options/ConfigOptions.cs index a2154c9..41602b4 100644 --- a/Timetracker/Options/ConfigOptions.cs +++ b/Timetracker/Options/ConfigOptions.cs @@ -17,7 +17,7 @@ public class ConfigOptions [Option("reset", Required = false, HelpText = "Remove all local configuration and activity cache files.")] public bool Reset { get; set; } - [Option("border", Required = false, HelpText = "Table border style for list output: minimal (default), square, or markdown.")] + [Option("border", Required = false, HelpText = "Table border style for list output: minimal (default), square, markdown, or ascii.")] public string Border { get; set; } [Option("week-start", Required = false, HelpText = "First day of the week for --week and --last-week shortcuts: sunday (default) or monday.")] diff --git a/Timetracker/Utils/TableHelper.cs b/Timetracker/Utils/TableHelper.cs index 3b6ab6f..f3938ca 100644 --- a/Timetracker/Utils/TableHelper.cs +++ b/Timetracker/Utils/TableHelper.cs @@ -22,8 +22,9 @@ public static Table NewTable(params string[] columns) private static TableBorder ResolveBorder() => ConfigService.GetTableBorder()?.ToLowerInvariant() switch { - "square" => TableBorder.Square, + "square" => TableBorder.Square, "markdown" => TableBorder.Markdown, - _ => TableBorder.Minimal + "ascii" => TableBorder.Ascii, + _ => TableBorder.Minimal }; } diff --git a/Timetracker/Validators/ConfigValidator.cs b/Timetracker/Validators/ConfigValidator.cs index 43baba3..e4285f0 100644 --- a/Timetracker/Validators/ConfigValidator.cs +++ b/Timetracker/Validators/ConfigValidator.cs @@ -38,8 +38,8 @@ public ConfigValidator() When(x => !string.IsNullOrEmpty(x.Border), () => { RuleFor(x => x.Border) - .Must(b => b.ToLowerInvariant() is "minimal" or "square" or "markdown") - .WithMessage("--border must be one of: minimal, square, markdown."); + .Must(b => b.ToLowerInvariant() is "minimal" or "square" or "markdown" or "ascii") + .WithMessage("--border must be one of: minimal, square, markdown, ascii."); }); When(x => !string.IsNullOrEmpty(x.WeekStart), () =>