| | | 1 | | using System.Reflection; |
| | | 2 | | using Spectre.Console; |
| | | 3 | | using Spectre.Console.Cli; |
| | | 4 | | |
| | | 5 | | namespace Punch.CLI; |
| | | 6 | | |
| | | 7 | | internal sealed class PunchCommand : Command<PunchCommandSettings> |
| | | 8 | | { |
| | | 9 | | private readonly IAnsiConsole _console; |
| | | 10 | | |
| | 7 | 11 | | public PunchCommand(IAnsiConsole console) |
| | 7 | 12 | | { |
| | 7 | 13 | | _console = console; |
| | 7 | 14 | | } |
| | | 15 | | |
| | | 16 | | public override int Execute(CommandContext context, PunchCommandSettings settings) |
| | 7 | 17 | | { |
| | 7 | 18 | | if (settings.Version) |
| | 2 | 19 | | { |
| | 2 | 20 | | var version = Assembly.GetExecutingAssembly() |
| | 2 | 21 | | .GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "0.0.0"; |
| | 2 | 22 | | Console.WriteLine(version); |
| | 2 | 23 | | return 0; |
| | | 24 | | } |
| | | 25 | | |
| | 5 | 26 | | if (settings.Date != null && settings.Yesterday) |
| | 1 | 27 | | { |
| | 1 | 28 | | Console.Error.WriteLine("Cannot combine --date and --yesterday."); |
| | 1 | 29 | | return 1; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | DateOnly workingDate; |
| | 4 | 33 | | if (settings.Date != null) |
| | 2 | 34 | | { |
| | 2 | 35 | | if (!DateOnly.TryParseExact(settings.Date, "yyyy-MM-dd", out workingDate)) |
| | 1 | 36 | | { |
| | 1 | 37 | | Console.Error.WriteLine($"Invalid date format: '{settings.Date}'. Expected yyyy-MM-dd."); |
| | 1 | 38 | | return 1; |
| | | 39 | | } |
| | 1 | 40 | | } |
| | 2 | 41 | | else if (settings.Yesterday) |
| | 2 | 42 | | { |
| | 2 | 43 | | workingDate = DateOnly.FromDateTime(DateTime.Now).AddDays(-1); |
| | 2 | 44 | | } |
| | | 45 | | else |
| | 0 | 46 | | { |
| | 0 | 47 | | workingDate = DateOnly.FromDateTime(DateTime.Now); |
| | 0 | 48 | | } |
| | | 49 | | |
| | 3 | 50 | | var filePath = PunchStorage.GetDisplayPath(workingDate); |
| | | 51 | | DaySchedule schedule; |
| | | 52 | | try |
| | 3 | 53 | | { |
| | 3 | 54 | | schedule = new DaySchedule(PunchStorage.Load(workingDate)); |
| | 0 | 55 | | } |
| | 3 | 56 | | catch (Exception ex) |
| | 3 | 57 | | { |
| | 3 | 58 | | _console.MarkupLine($"[bold red]Error:[/] Failed to load time log from [bold cyan]{Markup.Escape(filePath)}[ |
| | 3 | 59 | | _console.MarkupLine($"[bold yellow]Reason:[/] {Markup.Escape(ex.Message)}"); |
| | 3 | 60 | | return 1; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | var cursorSlot = 34; // default to 8:30am |
| | 0 | 64 | | if (schedule.Count > 0) |
| | 0 | 65 | | { |
| | 0 | 66 | | var lastEnd = schedule.Blocks.Max(b => b.StartSlot + b.Length); |
| | 0 | 67 | | if (lastEnd < 96) |
| | 0 | 68 | | cursorSlot = lastEnd; |
| | 0 | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | var appSettings = PunchStorage.LoadSettings(); |
| | 0 | 72 | | var session = new PunchSession(schedule, workingDate, filePath, cursorSlot, appSettings.GetTargetHours(workingDa |
| | | 73 | | |
| | 0 | 74 | | AnsiConsole.AlternateScreen(() => |
| | 0 | 75 | | { |
| | 0 | 76 | | try |
| | 0 | 77 | | { |
| | 0 | 78 | | AnsiConsole.Cursor.Hide(); |
| | 0 | 79 | | |
| | 0 | 80 | | var layout = new Layout("Root") |
| | 0 | 81 | | .SplitRows( |
| | 0 | 82 | | new Layout("Timeline").Size(5), |
| | 0 | 83 | | new Layout("Messages").Ratio(4), |
| | 0 | 84 | | new Layout("Input").Size(4), |
| | 0 | 85 | | new Layout("StatusBar").Size(1)); |
| | 0 | 86 | | var view = new PunchView(layout); |
| | 0 | 87 | | var controller = new PunchController(session, view); |
| | 0 | 88 | | |
| | 0 | 89 | | AnsiConsole.Live(layout).Start(controller.Run); |
| | 0 | 90 | | } |
| | 0 | 91 | | finally |
| | 0 | 92 | | { |
| | 0 | 93 | | AnsiConsole.Cursor.Show(); |
| | 0 | 94 | | } |
| | 0 | 95 | | }); |
| | | 96 | | |
| | 0 | 97 | | return 0; |
| | 7 | 98 | | } |
| | | 99 | | } |