| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Text; |
| | | 3 | | using Spectre.Console; |
| | | 4 | | using Spectre.Console.Rendering; |
| | | 5 | | |
| | | 6 | | namespace Punch.CLI; |
| | | 7 | | |
| | | 8 | | // Renders the TUI panes from a PunchSession into the Spectre layout. Pure |
| | | 9 | | // presentation: it reads session state and never mutates it. |
| | | 10 | | internal sealed class PunchView |
| | | 11 | | { |
| | | 12 | | private readonly Layout _layout; |
| | | 13 | | |
| | 72 | 14 | | public PunchView(Layout layout) |
| | 72 | 15 | | { |
| | 72 | 16 | | _layout = layout; |
| | 72 | 17 | | } |
| | | 18 | | |
| | | 19 | | public void Render(PunchSession session, bool confirming = false, bool confirmingDelete = false) |
| | 0 | 20 | | { |
| | 0 | 21 | | RenderTimeline(session); |
| | 0 | 22 | | RenderMessages(session); |
| | 0 | 23 | | RenderInput(session, confirming, confirmingDelete); |
| | 0 | 24 | | RenderStatusBar(session); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | private void RenderTimeline(PunchSession session) |
| | 0 | 28 | | { |
| | 0 | 29 | | var cursorSlot = session.CursorSlot; |
| | 0 | 30 | | var selectionLength = session.SelectionLength; |
| | 0 | 31 | | var selectedBlock = session.SelectedBlock; |
| | | 32 | | |
| | 0 | 33 | | var consoleWidth = System.Console.WindowWidth; |
| | 0 | 34 | | var barWidth = Math.Max(1, consoleWidth - 4); // account for panel border + padding |
| | 0 | 35 | | var endSlot = cursorSlot + selectionLength; |
| | 0 | 36 | | var timeLabel = SlotTime.FormatRange(cursorSlot, endSlot); |
| | | 37 | | |
| | 0 | 38 | | var sorted = session.Blocks.OrderBy(b => b.StartSlot).ToList(); |
| | | 39 | | |
| | | 40 | | // Use a fixed pixels-per-slot so every 15-min segment is the same width. |
| | | 41 | | // On terminals narrower than 96 columns barWidth/96 rounds to 0, so fall |
| | | 42 | | // back to proportional mapping to prevent bar overflow. |
| | 0 | 43 | | var pixelsPerSlot = barWidth / 96; |
| | 0 | 44 | | int SlotToPixel(int slot) => pixelsPerSlot > 0 |
| | 0 | 45 | | ? slot * pixelsPerSlot |
| | 0 | 46 | | : (int)((long)slot * barWidth / 96); |
| | 0 | 47 | | var totalBarWidth = pixelsPerSlot > 0 ? pixelsPerSlot * 96 : barWidth; |
| | 0 | 48 | | var pixelState = new int[totalBarWidth]; // 0=free, 1=booked, 2=selected, 3=selected-existing |
| | 0 | 49 | | var pixelBlockIndex = new int[totalBarWidth]; |
| | 0 | 50 | | Array.Fill(pixelBlockIndex, -1); |
| | 0 | 51 | | for (var blockIdx = 0; blockIdx < sorted.Count; blockIdx++) |
| | 0 | 52 | | { |
| | 0 | 53 | | var block = sorted[blockIdx]; |
| | 0 | 54 | | var bStart = SlotToPixel(block.StartSlot); |
| | 0 | 55 | | var bEndExcl = SlotToPixel(block.StartSlot + block.Length); |
| | 0 | 56 | | bStart = Math.Clamp(bStart, 0, totalBarWidth); |
| | 0 | 57 | | bEndExcl = Math.Clamp(bEndExcl, bStart, totalBarWidth); |
| | 0 | 58 | | for (var px = bStart; px < bEndExcl; px++) |
| | 0 | 59 | | { |
| | 0 | 60 | | pixelState[px] = 1; |
| | 0 | 61 | | pixelBlockIndex[px] = blockIdx; |
| | 0 | 62 | | } |
| | 0 | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | var selStartPos = SlotToPixel(cursorSlot); |
| | 0 | 66 | | var selEndExcl = SlotToPixel(endSlot); |
| | 0 | 67 | | if (selEndExcl == selStartPos) selEndExcl = selStartPos + 1; |
| | 0 | 68 | | selStartPos = Math.Clamp(selStartPos, 0, totalBarWidth - 1); |
| | 0 | 69 | | var selEndPos = Math.Clamp(selEndExcl - 1, selStartPos, totalBarWidth - 1); |
| | | 70 | | // State 3 = selected existing block (cyan), State 2 = free selection (yellow) |
| | 0 | 71 | | var selPixelState = selectedBlock != null ? 3 : 2; |
| | 0 | 72 | | for (var i = selStartPos; i <= selEndPos; i++) |
| | 0 | 73 | | pixelState[i] = selPixelState; |
| | | 74 | | |
| | | 75 | | // Build hour labels line |
| | 0 | 76 | | var labelChars = new char[totalBarWidth]; |
| | 0 | 77 | | Array.Fill(labelChars, ' '); |
| | | 78 | | // Hour ticks turn the label line into a ruler. Skip them on narrow |
| | | 79 | | // terminals (proportional fallback) where they'd land on top of each other. |
| | 0 | 80 | | if (pixelsPerSlot >= 1) |
| | 0 | 81 | | { |
| | 0 | 82 | | for (var h = 1; h < 24; h++) |
| | 0 | 83 | | labelChars[SlotToPixel(h * 4)] = '╵'; |
| | 0 | 84 | | } |
| | 0 | 85 | | var hourMarkers = new[] { 0, 6, 12, 18, 24 }; |
| | 0 | 86 | | foreach (var h in hourMarkers) |
| | 0 | 87 | | { |
| | 0 | 88 | | var pos = SlotToPixel(h * 4); |
| | 0 | 89 | | var label = h == 12 ? "12pm" : h < 12 ? $"{h}am" : $"{h - 12}pm"; |
| | 0 | 90 | | if (h == 0 || h == 24) label = "12am"; |
| | | 91 | | // Right-align the end marker so it doesn't overflow |
| | 0 | 92 | | if (h == 24) pos = totalBarWidth - label.Length; |
| | 0 | 93 | | for (var i = 0; i < label.Length && pos + i < totalBarWidth; i++) |
| | 0 | 94 | | labelChars[pos + i] = label[i]; |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | // "Now" marker: when viewing today, point at the current time under the |
| | | 98 | | // bar so the day has an anchor. Nudge off hour-label text if it collides. |
| | 0 | 99 | | var nowMarkerPos = -1; |
| | 0 | 100 | | if (session.WorkingDate == DateOnly.FromDateTime(DateTime.Now)) |
| | 0 | 101 | | { |
| | 0 | 102 | | var minutesOfDay = (int)DateTime.Now.TimeOfDay.TotalMinutes; |
| | 0 | 103 | | var pos = Math.Clamp((int)((long)minutesOfDay * totalBarWidth / 1440), 0, totalBarWidth - 1); |
| | 0 | 104 | | foreach (var offset in new[] { 0, 1, -1, 2, -2, 3, -3, 4, -4 }) |
| | 0 | 105 | | { |
| | 0 | 106 | | var candidate = pos + offset; |
| | 0 | 107 | | if (candidate < 0 || candidate >= totalBarWidth) continue; |
| | 0 | 108 | | if (labelChars[candidate] == ' ' || labelChars[candidate] == '╵') |
| | 0 | 109 | | { |
| | 0 | 110 | | pos = candidate; |
| | 0 | 111 | | break; |
| | | 112 | | } |
| | 0 | 113 | | } |
| | 0 | 114 | | labelChars[pos] = '▲'; |
| | 0 | 115 | | nowMarkerPos = pos; |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | // Position the time label above the selection midpoint |
| | 0 | 119 | | var midPos = (selStartPos + selEndPos) / 2; |
| | 0 | 120 | | var timeLabelStart = Math.Max(0, Math.Min(midPos - timeLabel.Length / 2, totalBarWidth - timeLabel.Length)); |
| | 0 | 121 | | var topLine = new string(' ', timeLabelStart) + timeLabel; |
| | | 122 | | |
| | | 123 | | // Build bar markup with colored segments (alternating colors for adjacent blocks) |
| | 0 | 124 | | var barMarkup = new StringBuilder(); |
| | 0 | 125 | | var currentState = -1; |
| | 0 | 126 | | var currentBlockIndex = -1; |
| | 0 | 127 | | for (var i = 0; i < totalBarWidth; i++) |
| | 0 | 128 | | { |
| | 0 | 129 | | var needNewTag = pixelState[i] != currentState || |
| | 0 | 130 | | (pixelState[i] == 1 && pixelBlockIndex[i] != currentBlockIndex); |
| | 0 | 131 | | if (needNewTag) |
| | 0 | 132 | | { |
| | 0 | 133 | | if (currentState >= 0) barMarkup.Append("[/]"); |
| | 0 | 134 | | currentState = pixelState[i]; |
| | 0 | 135 | | currentBlockIndex = pixelBlockIndex[i]; |
| | 0 | 136 | | barMarkup.Append(currentState switch |
| | 0 | 137 | | { |
| | 0 | 138 | | 1 => currentBlockIndex >= 0 && currentBlockIndex < sorted.Count && sorted[currentBlockIndex].IsUnpai |
| | 0 | 139 | | ? "[grey50]" |
| | 0 | 140 | | : currentBlockIndex % 2 == 0 ? "[orangered1]" : "[orange3]", |
| | 0 | 141 | | 2 => "[bold yellow]", |
| | 0 | 142 | | 3 => "[bold white]", |
| | 0 | 143 | | _ => "[dim]" |
| | 0 | 144 | | }); |
| | 0 | 145 | | } |
| | 0 | 146 | | barMarkup.Append(currentState switch |
| | 0 | 147 | | { |
| | 0 | 148 | | 0 => '─', |
| | 0 | 149 | | 3 => '▒', |
| | 0 | 150 | | _ => '█' |
| | 0 | 151 | | }); |
| | 0 | 152 | | } |
| | 0 | 153 | | if (currentState >= 0) barMarkup.Append("[/]"); |
| | | 154 | | |
| | | 155 | | // Center the bar within the panel width |
| | 0 | 156 | | var pad = Math.Max(0, (barWidth - totalBarWidth) / 2); |
| | 0 | 157 | | var padStr = new string(' ', pad); |
| | | 158 | | |
| | | 159 | | // Render the labels line, lifting the now marker out of the dim span so |
| | | 160 | | // it stands out in the accent color. |
| | 0 | 161 | | var labelsLine = new string(labelChars); |
| | | 162 | | string labelsMarkup; |
| | 0 | 163 | | if (nowMarkerPos >= 0) |
| | 0 | 164 | | { |
| | 0 | 165 | | var left = Markup.Escape(padStr + labelsLine[..nowMarkerPos]); |
| | 0 | 166 | | var right = Markup.Escape(labelsLine[(nowMarkerPos + 1)..]); |
| | 0 | 167 | | labelsMarkup = $"[dim]{left}[/][bold orangered1]▲[/][dim]{right}[/]"; |
| | 0 | 168 | | } |
| | | 169 | | else |
| | 0 | 170 | | { |
| | 0 | 171 | | labelsMarkup = $"[dim]{Markup.Escape(padStr + labelsLine)}[/]"; |
| | 0 | 172 | | } |
| | | 173 | | |
| | 0 | 174 | | var timelineContent = new Rows( |
| | 0 | 175 | | new Markup($"[bold]{Markup.Escape(padStr + topLine)}[/]"), |
| | 0 | 176 | | new Markup(padStr + barMarkup.ToString()), |
| | 0 | 177 | | new Markup(labelsMarkup)); |
| | | 178 | | |
| | 0 | 179 | | _layout["Timeline"].Update( |
| | 0 | 180 | | new Panel(timelineContent) |
| | 0 | 181 | | .Header($"Timeline · {session.WorkingDate:dddd, MMM d}") |
| | 0 | 182 | | .Expand() |
| | 0 | 183 | | .Border(BoxBorder.Rounded)); |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | private void RenderMessages(PunchSession session) |
| | 0 | 187 | | { |
| | 0 | 188 | | if (session.ShowHelp) |
| | | 189 | | // Split like the picker/summary so the time log stays visible on the |
| | | 190 | | // left while help occupies the right half. |
| | 0 | 191 | | _layout["Messages"].Update(new Layout("MessagesSplit") |
| | 0 | 192 | | .SplitColumns( |
| | 0 | 193 | | new Layout("Log").Update(BuildLogPanel(session)), |
| | 0 | 194 | | new Layout("Help").Update(BuildHelpPanel()))); |
| | 0 | 195 | | else if (session.ShowTicketPicker) |
| | | 196 | | // Split the content area so the time log stays visible on the left |
| | | 197 | | // while the picker occupies the right half. |
| | 0 | 198 | | _layout["Messages"].Update(new Layout("MessagesSplit") |
| | 0 | 199 | | .SplitColumns( |
| | 0 | 200 | | new Layout("Log").Update(BuildLogPanel(session)), |
| | 0 | 201 | | new Layout("Picker").Update(BuildTicketPickerPanel(session)))); |
| | 0 | 202 | | else if (session.ShowTicketSummary) |
| | | 203 | | // Split like the picker so the time log stays visible on the left |
| | | 204 | | // while the summary occupies the right half. |
| | 0 | 205 | | _layout["Messages"].Update(new Layout("MessagesSplit") |
| | 0 | 206 | | .SplitColumns( |
| | 0 | 207 | | new Layout("Log").Update(BuildLogPanel(session)), |
| | 0 | 208 | | new Layout("Summary").Update(BuildTicketSummaryPanel(session)))); |
| | | 209 | | else |
| | 0 | 210 | | _layout["Messages"].Update(BuildLogPanel(session)); |
| | 0 | 211 | | } |
| | | 212 | | |
| | | 213 | | private static IRenderable BuildLogPanel(PunchSession session) |
| | 0 | 214 | | { |
| | 0 | 215 | | var selectedBlock = session.SelectedBlock; |
| | 0 | 216 | | var sorted = session.Blocks.OrderBy(b => b.StartSlot).ToList(); |
| | | 217 | | |
| | | 218 | | // Messages pane: show booked blocks sorted chronologically with scrolling |
| | 0 | 219 | | var consoleHeight = System.Console.WindowHeight; |
| | 0 | 220 | | var messagesHeight = Math.Max(1, consoleHeight - 10 - 2); // 10 = fixed panes (5+4+1), 2 = panel border |
| | | 221 | | |
| | | 222 | | // Clamp scroll offset to valid range and reserve lines for scroll indicators |
| | 0 | 223 | | var availableLines = messagesHeight; |
| | 0 | 224 | | var clampedOffset = Math.Clamp(session.LogScrollOffset, 0, Math.Max(0, sorted.Count - 1)); |
| | | 225 | | |
| | 0 | 226 | | var hasMoreAbove = clampedOffset > 0; |
| | 0 | 227 | | if (hasMoreAbove) availableLines--; |
| | | 228 | | |
| | 0 | 229 | | var hasMoreBelow = clampedOffset + availableLines < sorted.Count; |
| | 0 | 230 | | if (hasMoreBelow) availableLines--; |
| | | 231 | | |
| | 0 | 232 | | availableLines = Math.Max(1, availableLines); |
| | | 233 | | // Re-clamp offset so we don't scroll past the end |
| | 0 | 234 | | var maxScrollOffset = Math.Max(0, sorted.Count - availableLines); |
| | 0 | 235 | | clampedOffset = Math.Min(clampedOffset, maxScrollOffset); |
| | | 236 | | // Recalculate indicators after clamping |
| | 0 | 237 | | hasMoreAbove = clampedOffset > 0; |
| | 0 | 238 | | hasMoreBelow = clampedOffset + availableLines < sorted.Count; |
| | | 239 | | |
| | 0 | 240 | | var visibleBlocks = sorted.Skip(clampedOffset).Take(availableLines).ToList(); |
| | | 241 | | |
| | | 242 | | IRenderable messagesContent; |
| | 0 | 243 | | if (visibleBlocks.Count == 0) |
| | 0 | 244 | | { |
| | 0 | 245 | | messagesContent = new Markup("[dim]No entries yet. Select a time range and press Enter.[/]"); |
| | 0 | 246 | | } |
| | | 247 | | else |
| | 0 | 248 | | { |
| | 0 | 249 | | var renderables = new List<IRenderable>(); |
| | 0 | 250 | | if (hasMoreAbove) |
| | 0 | 251 | | renderables.Add(new Markup($"[dim] ▲ {clampedOffset} more above (PgUp)[/]")); |
| | 0 | 252 | | foreach (var b in visibleBlocks) |
| | 0 | 253 | | { |
| | 0 | 254 | | var timeRange = SlotTime.FormatRange(b.StartSlot, b.StartSlot + b.Length); |
| | 0 | 255 | | var escaped = Markup.Escape(b.Label); |
| | 0 | 256 | | var isSelected = selectedBlock != null && b.StartSlot == selectedBlock.StartSlot && b.Length == selected |
| | 0 | 257 | | var blockIdx = sorted.IndexOf(b); |
| | 0 | 258 | | var squareColor = isSelected |
| | 0 | 259 | | ? "white" |
| | 0 | 260 | | : b.IsUnpaid |
| | 0 | 261 | | ? "grey50" |
| | 0 | 262 | | : blockIdx % 2 == 0 ? "orangered1" : "orange3"; |
| | 0 | 263 | | var durationText = Duration.Humanize(b.Length * 15); |
| | 0 | 264 | | var ticketDisplay = string.IsNullOrEmpty(b.Ticket) ? "" : $"[cyan]{Markup.Escape(b.Ticket)}[/] "; |
| | 0 | 265 | | renderables.Add(new Markup($"[{squareColor}]■[/] [bold]{timeRange}[/] {ticketDisplay}{escaped} [dim grey |
| | 0 | 266 | | } |
| | 0 | 267 | | if (hasMoreBelow) |
| | 0 | 268 | | { |
| | 0 | 269 | | var belowCount = sorted.Count - clampedOffset - availableLines; |
| | 0 | 270 | | renderables.Add(new Markup($"[dim] ▼ {belowCount} more below (PgDn)[/]")); |
| | 0 | 271 | | } |
| | 0 | 272 | | messagesContent = new Rows(renderables); |
| | 0 | 273 | | } |
| | | 274 | | |
| | 0 | 275 | | var header = sorted.Count == 0 |
| | 0 | 276 | | ? "Time Logged" |
| | 0 | 277 | | : $"Time Logged · {sorted.Count} {(sorted.Count == 1 ? "entry" : "entries")}"; |
| | 0 | 278 | | return new Panel(messagesContent) |
| | 0 | 279 | | .Header(header) |
| | 0 | 280 | | .Expand() |
| | 0 | 281 | | .Border(BoxBorder.Rounded); |
| | 0 | 282 | | } |
| | | 283 | | |
| | | 284 | | private static IRenderable BuildHelpPanel() |
| | 0 | 285 | | { |
| | 0 | 286 | | var version = Assembly.GetExecutingAssembly() |
| | 0 | 287 | | .GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "0.0.0"; |
| | 0 | 288 | | var titleLine = new Markup($"[bold][red]p[/][orangered1]u[/][darkorange]n[/][orange3]c[/][orange1]h[/][/] [dim]v |
| | 0 | 289 | | var helpText = new Markup( |
| | 0 | 290 | | "[bold]Left/Right[/] Move cursor / Jump between blocks\n" + |
| | 0 | 291 | | "[bold]Up/Down[/] Resize selection\n" + |
| | 0 | 292 | | "[bold]PgUp/PgDn[/] Scroll time log\n" + |
| | 0 | 293 | | "[bold]Enter[/] Log time entry\n" + |
| | 0 | 294 | | "[bold]Tab[/] Switch input field\n" + |
| | 0 | 295 | | "[bold]Ctrl+E[/] Edit selected entry\n" + |
| | 0 | 296 | | "[bold]Ctrl+D[/] Delete selected entry\n" + |
| | 0 | 297 | | "[bold]Ctrl+Q, Q[/] Quit\n" + |
| | 0 | 298 | | "[bold]?[/] Toggle this help\n" + |
| | 0 | 299 | | "[bold]F3, Ctrl+T[/] Ticket summary\n" + |
| | 0 | 300 | | "[bold]F4, Ctrl+P[/] Pick ticket for entry"); |
| | 0 | 301 | | var helpContent = new Rows( |
| | 0 | 302 | | titleLine, |
| | 0 | 303 | | new Text(" "), |
| | 0 | 304 | | helpText, |
| | 0 | 305 | | new Text(" "), |
| | 0 | 306 | | new Markup("[dim]Esc/? cancel[/]")); |
| | | 307 | | // A single Expand-ed panel that fills the region height exactly, matching |
| | | 308 | | // the log panel beside it. |
| | 0 | 309 | | return new Panel(helpContent) |
| | 0 | 310 | | .Header("Help") |
| | 0 | 311 | | .Border(BoxBorder.Rounded) |
| | 0 | 312 | | .Expand(); |
| | 0 | 313 | | } |
| | | 314 | | |
| | | 315 | | private static IRenderable BuildTicketSummaryPanel(PunchSession session) |
| | 0 | 316 | | { |
| | 0 | 317 | | var blocks = session.Blocks; |
| | 0 | 318 | | var ticketGroups = blocks |
| | 0 | 319 | | .GroupBy(b => string.IsNullOrEmpty(b.Ticket) ? "" : b.Ticket) |
| | 0 | 320 | | .Select(g => new { Ticket = g.Key, TotalMinutes = g.Sum(b => b.Length * 15) }) |
| | 0 | 321 | | .OrderBy(g => g.Ticket == "" ? 1 : 0) |
| | 0 | 322 | | .ThenBy(g => g.Ticket) |
| | 0 | 323 | | .ToList(); |
| | | 324 | | |
| | 0 | 325 | | var summaryLines = new List<IRenderable>(); |
| | 0 | 326 | | foreach (var g in ticketGroups) |
| | 0 | 327 | | { |
| | 0 | 328 | | var dur = Duration.Humanize(g.TotalMinutes); |
| | 0 | 329 | | var visibleName = g.Ticket == "" ? "Other" : g.Ticket; |
| | 0 | 330 | | var paddedName = visibleName.PadRight(20); |
| | 0 | 331 | | var ticketLabel = g.Ticket == "" ? $"[dim]{paddedName}[/]" : $"[cyan]{Markup.Escape(paddedName)}[/]"; |
| | 0 | 332 | | summaryLines.Add(new Markup($" {ticketLabel} {dur}")); |
| | 0 | 333 | | } |
| | | 334 | | |
| | 0 | 335 | | var billableMinutes = blocks.Where(b => !b.IsUnpaid).Sum(b => b.Length * 15); |
| | 0 | 336 | | var unbillableMinutes = blocks.Where(b => b.IsUnpaid).Sum(b => b.Length * 15); |
| | 0 | 337 | | var totalDur = Duration.HumanizeTotal(blocks.Sum(b => b.Length * 15)); |
| | 0 | 338 | | summaryLines.Add(new Markup($" [dim]{new string('─', 28)}[/]")); |
| | 0 | 339 | | summaryLines.Add(new Markup($" {"Billable".PadRight(20)} {Duration.Humanize(billableMinutes)}")); |
| | 0 | 340 | | summaryLines.Add(new Markup($" [grey50]{"Unbillable".PadRight(20)} {Duration.Humanize(unbillableMinutes)}[/]")) |
| | 0 | 341 | | summaryLines.Add(new Markup($" [bold]{"Total".PadRight(20)} {totalDur}[/]")); |
| | | 342 | | |
| | 0 | 343 | | summaryLines.Add(new Text(" ")); |
| | 0 | 344 | | summaryLines.Add(new Markup("[dim]Esc/F3 close[/]")); |
| | | 345 | | |
| | | 346 | | // A single Expand-ed panel that fills the region height exactly, matching |
| | | 347 | | // the log panel beside it. |
| | 0 | 348 | | return new Panel(new Rows(summaryLines)) |
| | 0 | 349 | | .Header("Ticket Summary") |
| | 0 | 350 | | .Border(BoxBorder.Rounded) |
| | 0 | 351 | | .Expand(); |
| | 0 | 352 | | } |
| | | 353 | | |
| | | 354 | | // Clamps text to a maximum display width, appending an ellipsis when cut. |
| | | 355 | | private static string Truncate(string text, int maxWidth) |
| | 0 | 356 | | { |
| | 0 | 357 | | if (maxWidth <= 0 || text.Length <= maxWidth) |
| | 0 | 358 | | return text; |
| | 0 | 359 | | return maxWidth == 1 ? "…" : text[..(maxWidth - 1)] + "…"; |
| | 0 | 360 | | } |
| | | 361 | | |
| | | 362 | | private static IRenderable BuildTicketPickerPanel(PunchSession session) |
| | 0 | 363 | | { |
| | 0 | 364 | | var lines = new List<IRenderable>(); |
| | 0 | 365 | | if (session.Tickets.Count == 0) |
| | 0 | 366 | | { |
| | 0 | 367 | | lines.Add(new Markup("[dim]No tickets found.[/]")); |
| | 0 | 368 | | lines.Add(new Text(" ")); |
| | 0 | 369 | | lines.Add(new Markup("[dim]Create [/][cyan]~/.punch/tickets.txt[/][dim] with one ticket per line,[/]")); |
| | 0 | 370 | | lines.Add(new Markup("[dim]tab- or comma-delimited as [/][cyan]TICKET<tab|,>Title[/][dim].[/]")); |
| | 0 | 371 | | } |
| | | 372 | | else |
| | 0 | 373 | | { |
| | | 374 | | // Window the list around the cursor so long lists scroll instead of |
| | | 375 | | // overflowing the pane. Budget: panel interior (messages region minus |
| | | 376 | | // its border) minus the footer block (blank + hint = 2). Rows are |
| | | 377 | | // truncated to one physical line below so the budget holds exactly. |
| | 0 | 378 | | var consoleHeight = System.Console.WindowHeight; |
| | 0 | 379 | | var interior = Math.Max(3, consoleHeight - 10 - 2); |
| | 0 | 380 | | var maxRows = Math.Max(1, interior - 2); |
| | | 381 | | |
| | | 382 | | // The picker occupies the right half of the content width; keep each |
| | | 383 | | // row to a single line so wrapping never eats into the row budget. |
| | 0 | 384 | | var textWidth = Math.Max(8, System.Console.WindowWidth / 2 - 4); |
| | | 385 | | |
| | 0 | 386 | | var count = session.Tickets.Count; |
| | 0 | 387 | | var cursor = session.TicketPickerCursor; |
| | | 388 | | |
| | | 389 | | // The ▲/▼ indicators each consume a row, but whether they appear |
| | | 390 | | // depends on the window position — which depends on how many rows fit. |
| | | 391 | | // Iterate to a fixed point so the indicator lines never overshoot the |
| | | 392 | | // budget (an overshoot of one would clip the footer). |
| | 0 | 393 | | var visibleRows = Math.Min(count, maxRows); |
| | 0 | 394 | | var offset = 0; |
| | 0 | 395 | | var hasMoreAbove = false; |
| | 0 | 396 | | var hasMoreBelow = false; |
| | 0 | 397 | | for (var iter = 0; iter < 4; iter++) |
| | 0 | 398 | | { |
| | 0 | 399 | | offset = count <= visibleRows |
| | 0 | 400 | | ? 0 |
| | 0 | 401 | | : Math.Clamp(cursor - visibleRows / 2, 0, count - visibleRows); |
| | 0 | 402 | | hasMoreAbove = offset > 0; |
| | 0 | 403 | | hasMoreBelow = offset + visibleRows < count; |
| | 0 | 404 | | var fit = Math.Max(1, Math.Min(count, |
| | 0 | 405 | | maxRows - (hasMoreAbove ? 1 : 0) - (hasMoreBelow ? 1 : 0))); |
| | 0 | 406 | | if (fit == visibleRows) |
| | 0 | 407 | | break; |
| | 0 | 408 | | visibleRows = fit; |
| | 0 | 409 | | } |
| | | 410 | | |
| | 0 | 411 | | if (hasMoreAbove) |
| | 0 | 412 | | lines.Add(new Markup($" [dim]▲ {offset} more[/]")); |
| | 0 | 413 | | for (var i = offset; i < offset + visibleRows && i < count; i++) |
| | 0 | 414 | | { |
| | 0 | 415 | | var t = session.Tickets[i]; |
| | | 416 | | // Prefix is 4 chars (" > " / " "); reserve the rest for the |
| | | 417 | | // ticket + two-space gap + title, truncating the title to fit. |
| | 0 | 418 | | var title = Truncate(t.Title, Math.Max(1, textWidth - 4 - t.Ticket.Length - 2)); |
| | 0 | 419 | | var ticket = Markup.Escape(t.Ticket); |
| | 0 | 420 | | var titleEsc = Markup.Escape(title); |
| | 0 | 421 | | if (i == cursor) |
| | 0 | 422 | | lines.Add(new Markup($" [bold yellow]> {ticket}[/] {titleEsc}")); |
| | | 423 | | else |
| | 0 | 424 | | lines.Add(new Markup($" [cyan]{ticket}[/] [dim]{titleEsc}[/]")); |
| | 0 | 425 | | } |
| | 0 | 426 | | if (hasMoreBelow) |
| | 0 | 427 | | lines.Add(new Markup($" [dim]▼ {count - offset - visibleRows} more[/]")); |
| | 0 | 428 | | } |
| | 0 | 429 | | lines.Add(new Text(" ")); |
| | 0 | 430 | | lines.Add(new Markup("[dim]↑/↓ select · Enter assign · Esc/F4 cancel[/]")); |
| | | 431 | | |
| | | 432 | | // A single Expand-ed panel that fills the region height exactly, matching |
| | | 433 | | // the log panel beside it so the footer never gets clipped. |
| | 0 | 434 | | return new Panel(new Rows(lines)) |
| | 0 | 435 | | .Header("Pick Ticket") |
| | 0 | 436 | | .Border(BoxBorder.Rounded) |
| | 0 | 437 | | .Expand(); |
| | 0 | 438 | | } |
| | | 439 | | |
| | | 440 | | private void RenderInput(PunchSession session, bool confirming, bool confirmingDelete) |
| | 0 | 441 | | { |
| | 0 | 442 | | var selectedBlock = session.SelectedBlock; |
| | 0 | 443 | | var editing = session.Editing; |
| | | 444 | | |
| | 0 | 445 | | if (confirming) |
| | 0 | 446 | | { |
| | 0 | 447 | | _layout["Input"].Update( |
| | 0 | 448 | | new Panel(new Markup("[bold yellow]Press Q again to quit[/]")) |
| | 0 | 449 | | .Expand() |
| | 0 | 450 | | .Border(BoxBorder.Rounded)); |
| | 0 | 451 | | } |
| | 0 | 452 | | else if (confirmingDelete) |
| | 0 | 453 | | { |
| | 0 | 454 | | _layout["Input"].Update( |
| | 0 | 455 | | new Panel(new Markup("[bold yellow]Press D again to delete[/]")) |
| | 0 | 456 | | .Expand() |
| | 0 | 457 | | .Border(BoxBorder.Rounded)); |
| | 0 | 458 | | } |
| | 0 | 459 | | else if (selectedBlock != null && editing) |
| | 0 | 460 | | { |
| | 0 | 461 | | var descLine = RenderFieldLine("Description", session.InputBuffer, session.InputCursor, session.ActiveField |
| | 0 | 462 | | var tickLine = RenderFieldLine("Ticket", session.TicketBuffer, session.TicketCursor, session.ActiveField == |
| | 0 | 463 | | _layout["Input"].Update( |
| | 0 | 464 | | new Panel(new Rows(new Markup(descLine), new Markup(tickLine))) |
| | 0 | 465 | | .Header("Input [cyan](editing)[/]") |
| | 0 | 466 | | .Expand() |
| | 0 | 467 | | .Border(BoxBorder.Rounded)); |
| | 0 | 468 | | } |
| | 0 | 469 | | else if (selectedBlock != null) |
| | 0 | 470 | | { |
| | 0 | 471 | | var labelText = Markup.Escape(selectedBlock.Label); |
| | 0 | 472 | | var ticketText = Markup.Escape(selectedBlock.Ticket); |
| | 0 | 473 | | var descLine = $"[bold]Description:[/] {labelText}"; |
| | 0 | 474 | | var tickLine = $"[bold]Ticket:[/] {(string.IsNullOrEmpty(ticketText) ? "[dim]none[/]" : ticketText)}"; |
| | 0 | 475 | | _layout["Input"].Update( |
| | 0 | 476 | | new Panel(new Rows(new Markup(descLine), new Markup(tickLine))) |
| | 0 | 477 | | .Header("Input") |
| | 0 | 478 | | .Expand() |
| | 0 | 479 | | .Border(BoxBorder.Rounded)); |
| | 0 | 480 | | } |
| | | 481 | | else |
| | 0 | 482 | | { |
| | 0 | 483 | | var descLine = RenderFieldLine("Description", session.InputBuffer, session.InputCursor, session.ActiveField |
| | 0 | 484 | | var tickLine = RenderFieldLine("Ticket", session.TicketBuffer, session.TicketCursor, session.ActiveField == |
| | 0 | 485 | | _layout["Input"].Update( |
| | 0 | 486 | | new Panel(new Rows(new Markup(descLine), new Markup(tickLine))) |
| | 0 | 487 | | .Header("Input") |
| | 0 | 488 | | .Expand() |
| | 0 | 489 | | .Border(BoxBorder.Rounded)); |
| | 0 | 490 | | } |
| | 0 | 491 | | } |
| | | 492 | | |
| | | 493 | | private void RenderStatusBar(PunchSession session) |
| | 0 | 494 | | { |
| | 0 | 495 | | var consoleWidth = System.Console.WindowWidth; |
| | 0 | 496 | | var filePath = session.FilePath; |
| | 0 | 497 | | var totalMinutesAll = session.Blocks.Where(b => !b.IsUnpaid).Sum(b => b.Length * 15); |
| | 0 | 498 | | var totalFormatted = Duration.HumanizeTotal(totalMinutesAll); |
| | 0 | 499 | | var statusLeftPlain = $" {filePath} ?=help F3=summary F4=tickets"; |
| | | 500 | | string statusRightPlain; |
| | | 501 | | string statusRightMarkup; |
| | 0 | 502 | | if (session.TargetHours > 0) |
| | 0 | 503 | | { |
| | 0 | 504 | | var targetMinutes = session.TargetHours * 60; |
| | 0 | 505 | | var percent = totalMinutesAll * 100 / targetMinutes; |
| | | 506 | | // 10-cell gauge toward the daily target; the percent keeps counting |
| | | 507 | | // past 100 but the gauge pegs at full. |
| | 0 | 508 | | var filled = Math.Clamp(totalMinutesAll * 10 / targetMinutes, 0, 10); |
| | 0 | 509 | | var gaugeFilled = new string('▰', filled); |
| | 0 | 510 | | var gaugeEmpty = new string('▱', 10 - filled); |
| | 0 | 511 | | statusRightPlain = $"{totalFormatted} {gaugeFilled}{gaugeEmpty} {percent}% of {session.TargetHours}h "; |
| | 0 | 512 | | statusRightMarkup = $"[bold white]{Markup.Escape(totalFormatted)} [/][bold yellow]{gaugeFilled}[/][dim]{gau |
| | | 513 | | // On narrow terminals the gauge is the first thing to go. |
| | 0 | 514 | | if (statusLeftPlain.Length + statusRightPlain.Length > consoleWidth) |
| | 0 | 515 | | { |
| | 0 | 516 | | statusRightPlain = $"{totalFormatted} {percent}% of {session.TargetHours}h "; |
| | 0 | 517 | | statusRightMarkup = $"[bold white]{Markup.Escape(statusRightPlain)}[/]"; |
| | 0 | 518 | | } |
| | 0 | 519 | | } |
| | | 520 | | else |
| | 0 | 521 | | { |
| | 0 | 522 | | statusRightPlain = $"{totalFormatted} "; |
| | 0 | 523 | | statusRightMarkup = $"[bold white]{Markup.Escape(statusRightPlain)}[/]"; |
| | 0 | 524 | | } |
| | 0 | 525 | | var padding = Math.Max(0, consoleWidth - statusLeftPlain.Length - statusRightPlain.Length); |
| | 0 | 526 | | var statusBar = $"[white on orangered1] {Markup.Escape(filePath)} [bold yellow]?=help F3=summary F4=tickets[/] |
| | 0 | 527 | | _layout["StatusBar"].Update(new Markup(statusBar)); |
| | 0 | 528 | | } |
| | | 529 | | |
| | | 530 | | private static string RenderFieldLine(string fieldName, StringBuilder buffer, int cursor, bool isActive) |
| | 0 | 531 | | { |
| | 0 | 532 | | var paddedName = fieldName.PadRight(11); |
| | 0 | 533 | | if (isActive) |
| | 0 | 534 | | { |
| | 0 | 535 | | var text = buffer.ToString(); |
| | 0 | 536 | | var beforeCursor = Markup.Escape(text[..cursor]); |
| | 0 | 537 | | var cursorChar = cursor < text.Length ? Markup.Escape(text[cursor].ToString()) : " "; |
| | 0 | 538 | | var afterCursor = cursor < text.Length ? Markup.Escape(text[(cursor + 1)..]) : ""; |
| | 0 | 539 | | return $"[bold]{Markup.Escape(paddedName)}:[/] {beforeCursor}[invert]{cursorChar}[/]{afterCursor}"; |
| | | 540 | | } |
| | | 541 | | else |
| | 0 | 542 | | { |
| | 0 | 543 | | var escaped = Markup.Escape(buffer.ToString()); |
| | 0 | 544 | | var display = string.IsNullOrEmpty(escaped) ? "[dim]empty[/]" : $"[dim]{escaped}[/]"; |
| | 0 | 545 | | return $"[dim]{Markup.Escape(paddedName)}:[/] {display}"; |
| | | 546 | | } |
| | 0 | 547 | | } |
| | | 548 | | } |