-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.go
More file actions
596 lines (487 loc) · 15.4 KB
/
Copy pathui.go
File metadata and controls
596 lines (487 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package main
import (
"fmt"
"strings"
"time"
"github.com/charmbracelet/lipgloss"
)
// =============================================================================
// Design Tokens
// =============================================================================
const appWidth = 78
var (
// Colors
cPrimary = lipgloss.Color("#7D56F4")
cPrimaryHi = lipgloss.Color("#9B7BF7")
cSuccess = lipgloss.Color("#04B575")
cSuccessHi = lipgloss.Color("#06D68E")
cDanger = lipgloss.Color("#FF4757")
cDangerHi = lipgloss.Color("#FF6B81")
cWarning = lipgloss.Color("#F4D03F")
cText = lipgloss.Color("#E8E8E8")
cTextDim = lipgloss.Color("#A0A0B0")
cMuted = lipgloss.Color("#6C6C80")
cBorder = lipgloss.Color("#3A3A50")
cBgPanel = lipgloss.Color("#1E1E2E")
cBgHeader = lipgloss.Color("#16161E")
)
// =============================================================================
// Reusable Styles
// =============================================================================
var (
// Header bar (always visible)
headerBar = lipgloss.NewStyle().
Background(cBgHeader).
Foreground(cText).
Width(appWidth).
Padding(0, 2).
Render
headerTitle = lipgloss.NewStyle().
Bold(true).
Foreground(cPrimaryHi).
Render
headerMeta = lipgloss.NewStyle().
Foreground(cMuted).
Render
// License pill
pillActive = lipgloss.NewStyle().
Background(cPrimary).
Foreground(lipgloss.Color("#FFFFFF")).
Padding(0, 1).
Bold(true).
Render
pillTrial = lipgloss.NewStyle().
Background(cWarning).
Foreground(lipgloss.Color("#000000")).
Padding(0, 1).
Bold(true).
Render
pillExpired = lipgloss.NewStyle().
Background(cDanger).
Foreground(lipgloss.Color("#FFFFFF")).
Padding(0, 1).
Bold(true).
Render
// Breadcrumb
crumbActive = lipgloss.NewStyle().
Foreground(cPrimaryHi).
Bold(true).
Render
crumbDone = lipgloss.NewStyle().
Foreground(cSuccess).
Render
crumbPending = lipgloss.NewStyle().
Foreground(cMuted).
Render
crumbArrow = lipgloss.NewStyle().
Foreground(cBorder).
Render
// Panels
panel = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(cBorder).
Background(cBgPanel).
Padding(1, 2).
Width(appWidth - 4)
panelSuccess = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(cSuccess).
Background(cBgPanel).
Padding(1, 2).
Width(appWidth - 4)
panelError = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(cDanger).
Background(cBgPanel).
Padding(1, 2).
Width(appWidth - 4)
// Typography
title = lipgloss.NewStyle().
Bold(true).
Foreground(cPrimaryHi).
MarginBottom(1)
subtitle = lipgloss.NewStyle().
Foreground(cTextDim).
MarginBottom(1)
body = lipgloss.NewStyle().
Foreground(cText)
hint = lipgloss.NewStyle().
Foreground(cMuted).
Italic(true)
label = lipgloss.NewStyle().
Foreground(cTextDim).
Width(16).
Align(lipgloss.Right).
PaddingRight(1)
value = lipgloss.NewStyle().
Foreground(cText).
Bold(true)
valueHighlight = lipgloss.NewStyle().
Foreground(cWarning).
Bold(true)
valueSuccess = lipgloss.NewStyle().
Foreground(cSuccessHi).
Bold(true)
// Interactive elements
choiceSelected = lipgloss.NewStyle().
Foreground(cSuccessHi).
Bold(true).
PaddingLeft(2)
choiceNormal = lipgloss.NewStyle().
Foreground(cText).
PaddingLeft(2)
choiceDesc = lipgloss.NewStyle().
Foreground(cMuted).
PaddingLeft(4)
// Footer
footer = lipgloss.NewStyle().
Foreground(cMuted).
Width(appWidth).
Align(lipgloss.Center).
MarginTop(1)
// Progress
progressLabel = lipgloss.NewStyle().
Foreground(cTextDim).
Width(12).
Render
progressValue = lipgloss.NewStyle().
Foreground(cPrimaryHi).
Bold(true).
Width(10).
Align(lipgloss.Right).
Render
// Big numbers
bigNumber = lipgloss.NewStyle().
Foreground(cPrimaryHi).
Bold(true).
Width(10).
Align(lipgloss.Right).
Render
)
// =============================================================================
// Main View Router
// =============================================================================
func renderView(m model) string {
var sections []string
// Persistent header
sections = append(sections, renderHeader())
// Breadcrumb (hidden on error/license screen)
if m.step != stepError {
sections = append(sections, renderBreadcrumb(m.step))
}
// Main content
switch m.step {
case stepSelectDB:
sections = append(sections, renderDBSelection(m))
case stepInputRows:
sections = append(sections, renderRowInput(m))
case stepRunning:
sections = append(sections, renderRunning(m))
case stepSuccess:
sections = append(sections, renderSuccess(m))
case stepError:
sections = append(sections, renderError(m))
}
// Footer
sections = append(sections, footer.Render(renderFooter(m)))
return lipgloss.JoinVertical(lipgloss.Center, sections...)
}
// =============================================================================
// Header (Persistent)
// =============================================================================
func renderHeader() string {
lic := LoadAndVerifyLicense()
left := headerTitle("⚡ DATA-MOCKER")
right := ""
if lic.Valid {
if lic.IsTrial() {
right = pillTrial(fmt.Sprintf("TRIAL %dd", lic.DaysRemaining()))
} else {
right = pillActive(strings.ToUpper(string(lic.Claims.Tier)))
}
} else {
right = pillExpired("EXPIRED")
}
// Pad between left and right
padWidth := appWidth - lipgloss.Width(left) - lipgloss.Width(right) - 4
if padWidth < 1 {
padWidth = 1
}
padding := strings.Repeat(" ", padWidth)
return headerBar(lipgloss.JoinHorizontal(lipgloss.Left, left, padding, right))
}
// =============================================================================
// Breadcrumb
// =============================================================================
func renderBreadcrumb(step int) string {
steps := []string{"Database", "Amount", "Generate", "Done"}
var parts []string
for i, label := range steps {
var s string
switch {
case i == step:
s = crumbActive(label)
case i < step:
s = crumbDone("✓ " + label)
default:
s = crumbPending(label)
}
parts = append(parts, s)
if i < len(steps)-1 {
parts = append(parts, crumbArrow("›"))
}
}
return lipgloss.NewStyle().
Width(appWidth).
Align(lipgloss.Center).
MarginTop(1).
MarginBottom(1).
Render(lipgloss.JoinHorizontal(lipgloss.Center, parts...))
}
// =============================================================================
// Step 0: Database Selection
// =============================================================================
func renderDBSelection(m model) string {
var b strings.Builder
b.WriteString(title.Render("Choose Your Database Engine") + "\n")
b.WriteString(subtitle.Render("Pick the engine that fits your workflow") + "\n\n")
engines := []struct {
name string
desc string
badge string
badgeC lipgloss.Color
}{
{"PostgreSQL", "Fastest. Binary COPY protocol for millions of rows.", "RECOMMENDED", cSuccess},
{"MySQL", "Widely compatible. Optimized batch INSERT.", "", cMuted},
{"SQLite", "Zero setup. Instant file-based database.", "QUICK", cWarning},
}
for i, eng := range engines {
icon := " "
style := choiceNormal
if m.cursor == i {
icon = "▸ "
style = choiceSelected
}
b.WriteString(style.Render(fmt.Sprintf("%s%s", icon, eng.name)) + "\n")
b.WriteString(choiceDesc.Render(eng.desc) + "\n")
if eng.badge != "" {
badge := lipgloss.NewStyle().
Foreground(eng.badgeC).
Bold(true).
PaddingLeft(4).
Render("[" + eng.badge + "]")
b.WriteString(badge + "\n")
}
b.WriteString("\n")
}
b.WriteString(hint.Render("Tip: SQLite needs no Docker — perfect for first-time users.") + "\n")
return panel.Render(b.String())
}
// =============================================================================
// Step 1: Row Count Input
// =============================================================================
func renderRowInput(m model) string {
var b strings.Builder
b.WriteString(title.Render("How Many Rows?") + "\n")
// Selected DB badge
dbBadge := lipgloss.NewStyle().
Background(cPrimary).
Foreground(lipgloss.Color("#FFFFFF")).
Padding(0, 1).
Bold(true).
Render(" " + m.selectedDB + " ")
b.WriteString(subtitle.Render("Target: "+dbBadge) + "\n\n")
b.WriteString(body.Render("Enter the number of mock rows to generate.") + "\n")
b.WriteString(hint.Render("Tip: 1,000 for testing • 100,000 for benchmarks • 1,000,000+ for stress tests") + "\n\n")
// Input box
inputBox := lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(cPrimary).
Padding(0, 1).
Width(30).
Render(m.rowInput.View())
b.WriteString(inputBox + "\n")
return panel.Render(b.String())
}
// =============================================================================
// Step 2: Running / Progress Dashboard
// =============================================================================
func renderRunning(m model) string {
var b strings.Builder
b.WriteString(title.Render("Generating Data") + "\n\n")
if m.connStr == "" {
b.WriteString(m.spinner.View() + " ")
b.WriteString(body.Render("Provisioning container...") + "\n")
b.WriteString(hint.Render(" Pulling image on first run. This may take 30–60 seconds.") + "\n")
} else {
b.WriteString(m.spinner.View() + " ")
b.WriteString(body.Render(fmt.Sprintf("Streaming into %s", m.selectedDB)) + "\n\n")
// Progress bar
b.WriteString(m.progress.View() + "\n\n")
// Stats dashboard
b.WriteString(renderDivider() + "\n")
b.WriteString(renderStatRow("Progress", fmt.Sprintf("%.1f%%", m.percent*100), false))
b.WriteString(renderStatRow("Rows", fmt.Sprintf("%s / %s", formatNumber(m.rowsWritten), formatNumber(m.targetRows)), true))
b.WriteString(renderStatRow("Speed", fmt.Sprintf("%s rows/sec", formatNumber(m.speed)), false))
b.WriteString(renderStatRow("Elapsed", m.elapsed.Round(time.Second).String(), false))
b.WriteString(renderDivider() + "\n")
// ETA estimate
if m.speed > 0 && m.percent < 1.0 {
remaining := m.targetRows - m.rowsWritten
etaSec := remaining / m.speed
if etaSec > 0 {
eta := time.Duration(etaSec) * time.Second
b.WriteString("\n" + hint.Render(fmt.Sprintf("⏱ Estimated time remaining: %s", eta.Round(time.Second))))
}
}
}
return panel.Render(b.String())
}
// =============================================================================
// Step 3: Success Celebration
// =============================================================================
func renderSuccess(m model) string {
var b strings.Builder
// Celebration banner
b.WriteString(lipgloss.NewStyle().
Width(appWidth).
Align(lipgloss.Center).
Foreground(cSuccessHi).
Bold(true).
Render("✨ GENERATION COMPLETE ✨") + "\n\n")
// Stats box
var stats strings.Builder
stats.WriteString(renderStatRow("Total Rows", formatNumber(m.rowsWritten), true))
stats.WriteString(renderStatRow("Time", m.elapsed.Round(time.Millisecond).String(), false))
stats.WriteString(renderStatRow("Database", m.selectedDB, false))
// Connection string in a copy-friendly box
connBox := lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(cPrimary).
Background(lipgloss.Color("#252535")).
Padding(1, 2).
Width(appWidth - 12).
Render(lipgloss.NewStyle().
Foreground(cPrimaryHi).
Bold(true).
Render(m.connStr))
stats.WriteString(renderStatRow("Connection", "", false))
stats.WriteString(" " + connBox + "\n")
if m.containerID != "" {
stats.WriteString(renderStatRow("Container", m.containerID[:12], false))
}
b.WriteString(panelSuccess.Render(stats.String()) + "\n\n")
// Next steps
b.WriteString(body.Render("What would you like to do next?") + "\n")
b.WriteString(hint.Render("• Connect: Use the connection string above with DBeaver, TablePlus, or psql") + "\n")
b.WriteString(hint.Render("• Export: Run with --format csv to generate files without a database") + "\n")
b.WriteString(hint.Render("• Schema: Use --schema to define custom tables and foreign keys") + "\n")
return b.String()
}
// =============================================================================
// Step -1: Error (Helpful, Not Punishing)
// =============================================================================
func renderError(m model) string {
var b strings.Builder
b.WriteString(lipgloss.NewStyle().
Width(appWidth).
Align(lipgloss.Center).
Foreground(cDangerHi).
Bold(true).
Render("⚠ SOMETHING WENT WRONG") + "\n\n")
// Error detail box
errBox := lipgloss.NewStyle().
Foreground(cDanger).
Bold(true).
Render(fmt.Sprintf("%v", m.err))
b.WriteString(panelError.Render(errBox) + "\n\n")
// Suggested fixes
b.WriteString(body.Render("Try these fixes:") + "\n")
suggestions := []string{
"Ensure Docker Desktop is running (for PostgreSQL / MySQL)",
"Pick SQLite for zero-setup testing",
"Reduce row count if memory is limited",
"Check your license with: ./datamocker --license",
}
for _, s := range suggestions {
b.WriteString(hint.Render(" • "+s) + "\n")
}
b.WriteString("\n" + body.Render("Press Enter to go back • Q to quit") + "\n")
return b.String()
}
// =============================================================================
// License Screen (Fallback)
// =============================================================================
func renderLicenseScreen() string {
lic := LoadAndVerifyLicense()
var b strings.Builder
b.WriteString(lipgloss.NewStyle().
Width(appWidth).
Align(lipgloss.Center).
Foreground(cDangerHi).
Bold(true).
Render("🔒 LICENSE REQUIRED") + "\n\n")
if lic.Error != "" {
b.WriteString(lipgloss.NewStyle().
Foreground(cDanger).
Render(lic.Error) + "\n\n")
}
b.WriteString(body.Render("Purchase a license at:") + "\n")
b.WriteString(lipgloss.NewStyle().
Foreground(cPrimaryHi).
Bold(true).
Underline(true).
Render("https://datamocker.dev") + "\n\n")
b.WriteString(hint.Render("Or place a valid .datamocker_license file in your home directory.") + "\n\n")
if lic.IsTrial() && lic.DaysRemaining() > 0 {
b.WriteString(lipgloss.NewStyle().
Foreground(cWarning).
Bold(true).
Render(fmt.Sprintf("⏱ Trial expires in %d days", lic.DaysRemaining())) + "\n")
}
b.WriteString("\n" + footer.Render("Press Q to exit"))
return b.String()
}
// =============================================================================
// Helpers
// =============================================================================
func renderDivider() string {
return lipgloss.NewStyle().
Foreground(cBorder).
Render(strings.Repeat("─", appWidth-8))
}
func renderStatRow(name, val string, highlight bool) string {
valStyle := value
if highlight {
valStyle = valueHighlight
}
return lipgloss.JoinHorizontal(lipgloss.Left,
label.Render(name),
valStyle.Render(val),
) + "\n"
}
func renderFooter(m model) string {
switch m.step {
case stepSelectDB:
return "↑/↓ select • Enter confirm • Q quit"
case stepInputRows:
return "Enter confirm • Esc back • Q quit"
case stepRunning:
return "Please wait... • Ctrl+C to cancel"
case stepSuccess:
return "Enter new session • Q quit"
case stepError:
return "Enter retry • Q quit"
default:
return "↑/↓ navigate • Enter confirm • Q quit"
}
}
func formatNumber(n int) string {
if n < 1000 {
return fmt.Sprintf("%d", n)
}
if n < 1000000 {
return fmt.Sprintf("%.1fK", float64(n)/1000)
}
return fmt.Sprintf("%.2fM", float64(n)/1000000)
}