| @@ -9,11 +9,13 @@ import ( |
| 9 | 9 | "os" |
| 10 | 10 | "os/exec" |
| 11 | 11 | "path/filepath" |
| 12 | "regexp" |
| 12 | 13 | "runtime" |
| 13 | 14 | "strings" |
| 14 | 15 | "sync" |
| 15 | 16 | "time" |
| 16 | 17 | |
| 18 | "github.com/charmbracelet/lipgloss" |
| 17 | 19 | "golang.org/x/term" |
| 18 | 20 | ) |
| 19 | 21 | |
| @@ -272,33 +274,199 @@ func formatLocalTime(t time.Time) string { |
| 272 | 274 | return t.In(time.Local).Format("Mon Jan 2 2006, 3:04 PM MST") |
| 273 | 275 | } |
| 274 | 276 | |
| 277 | func escapePango(s string) string { |
| 278 | s = strings.ReplaceAll(s, "&", "&") |
| 279 | s = strings.ReplaceAll(s, "<", "<") |
| 280 | s = strings.ReplaceAll(s, ">", ">") |
| 281 | return s |
| 282 | } |
| 283 | |
| 284 | var ( |
| 285 | mdBoldRe = regexp.MustCompile(`\*\*(.+?)\*\*`) |
| 286 | mdCodeRe = regexp.MustCompile("`([^`]+)`") |
| 287 | mdLinkRe = regexp.MustCompile(`\[([^\]]+)\]\(([^)]+)\)`) |
| 288 | mdURLRe = regexp.MustCompile(`https?://[^\s<>\]]+`) |
| 289 | ansiStrip = regexp.MustCompile(`\x1b\[[0-9;]*m`) |
| 290 | ) |
| 291 | |
| 292 | // markdownToPango turns common GitHub release markdown into Rofi-friendly Pango. |
| 293 | func markdownToPango(md string) string { |
| 294 | lines := strings.Split(strings.ReplaceAll(md, "\r\n", "\n"), "\n") |
| 295 | out := make([]string, 0, len(lines)) |
| 296 | for _, line := range lines { |
| 297 | trimmed := strings.TrimSpace(line) |
| 298 | switch { |
| 299 | case trimmed == "": |
| 300 | out = append(out, "") |
| 301 | continue |
| 302 | case strings.HasPrefix(trimmed, "### "): |
| 303 | text := escapePango(strings.TrimPrefix(trimmed, "### ")) |
| 304 | out = append(out, `<span foreground="#FFD166"><b>`+text+`</b></span>`) |
| 305 | case strings.HasPrefix(trimmed, "## "): |
| 306 | text := escapePango(strings.TrimPrefix(trimmed, "## ")) |
| 307 | out = append(out, `<span foreground="#7CB9E8" size="large"><b>`+text+`</b></span>`) |
| 308 | case strings.HasPrefix(trimmed, "# "): |
| 309 | text := escapePango(strings.TrimPrefix(trimmed, "# ")) |
| 310 | out = append(out, `<span foreground="#7CFC98" size="large"><b>`+text+`</b></span>`) |
| 311 | case strings.HasPrefix(trimmed, "- ") || strings.HasPrefix(trimmed, "* "): |
| 312 | body := strings.TrimPrefix(strings.TrimPrefix(trimmed, "- "), "* ") |
| 313 | out = append(out, `<span foreground="#98FB98">•</span> `+inlineMarkdownToPango(body)) |
| 314 | case strings.HasPrefix(trimmed, "**Full Changelog**") || strings.HasPrefix(strings.ToLower(trimmed), "**full changelog**"): |
| 315 | out = append(out, `<span foreground="#B0B0B0">`+inlineMarkdownToPango(trimmed)+`</span>`) |
| 316 | default: |
| 317 | out = append(out, inlineMarkdownToPango(trimmed)) |
| 318 | } |
| 319 | } |
| 320 | return strings.Join(out, "\n") |
| 321 | } |
| 322 | |
| 323 | func inlineMarkdownToPango(s string) string { |
| 324 | // Links first (before escaping full string piece by piece) |
| 325 | s = mdLinkRe.ReplaceAllStringFunc(s, func(m string) string { |
| 326 | parts := mdLinkRe.FindStringSubmatch(m) |
| 327 | if len(parts) != 3 { |
| 328 | return escapePango(m) |
| 329 | } |
| 330 | return `<span foreground="#6EC6FF" underline="single">` + escapePango(parts[1]) + `</span>` |
| 331 | }) |
| 332 | // Escape remaining raw text while preserving spans we inserted — do a simple pass: |
| 333 | // split on existing span tags is hard; re-process from original for bold/code on non-link text. |
| 334 | // Safer path: escape whole line then re-apply patterns on escaped text where ** still present. |
| 335 | if !strings.Contains(s, "<span") { |
| 336 | s = escapePango(s) |
| 337 | s = mdBoldRe.ReplaceAllString(s, `<span foreground="#FFFFFF"><b>$1</b></span>`) |
| 338 | s = mdCodeRe.ReplaceAllString(s, `<span foreground="#E0B0FF" face="monospace">$1</span>`) |
| 339 | s = mdURLRe.ReplaceAllStringFunc(s, func(u string) string { |
| 340 | return `<span foreground="#6EC6FF" underline="single">` + u + `</span>` |
| 341 | }) |
| 342 | return s |
| 343 | } |
| 344 | // Already has link spans — only lightly touch remaining ** if any outside tags |
| 345 | s = mdBoldRe.ReplaceAllString(s, `<b>$1</b>`) |
| 346 | return s |
| 347 | } |
| 348 | |
| 349 | // markdownToTerminal colors release notes for CLI (lipgloss), easy on the eyes. |
| 350 | func markdownToTerminal(md string) string { |
| 351 | heading := lipgloss.NewStyle().Foreground(lipgloss.Color("#7CB9E8")).Bold(true) |
| 352 | subhead := lipgloss.NewStyle().Foreground(lipgloss.Color("#FFD166")).Bold(true) |
| 353 | bullet := lipgloss.NewStyle().Foreground(lipgloss.Color("#98FB98")) |
| 354 | body := lipgloss.NewStyle().Foreground(lipgloss.Color("#E6E6FA")) |
| 355 | muted := lipgloss.NewStyle().Foreground(lipgloss.Color("#9A9A9A")) |
| 356 | link := lipgloss.NewStyle().Foreground(lipgloss.Color("#6EC6FF")).Underline(true) |
| 357 | |
| 358 | lines := strings.Split(strings.ReplaceAll(md, "\r\n", "\n"), "\n") |
| 359 | out := make([]string, 0, len(lines)) |
| 360 | for _, line := range lines { |
| 361 | trimmed := strings.TrimSpace(line) |
| 362 | switch { |
| 363 | case trimmed == "": |
| 364 | out = append(out, "") |
| 365 | case strings.HasPrefix(trimmed, "### "): |
| 366 | out = append(out, subhead.Render(strings.TrimPrefix(trimmed, "### "))) |
| 367 | case strings.HasPrefix(trimmed, "## "): |
| 368 | out = append(out, heading.Render(strings.TrimPrefix(trimmed, "## "))) |
| 369 | case strings.HasPrefix(trimmed, "# "): |
| 370 | out = append(out, heading.Render(strings.TrimPrefix(trimmed, "# "))) |
| 371 | case strings.HasPrefix(trimmed, "- ") || strings.HasPrefix(trimmed, "* "): |
| 372 | item := strings.TrimPrefix(strings.TrimPrefix(trimmed, "- "), "* ") |
| 373 | item = mdBoldRe.ReplaceAllString(item, "$1") |
| 374 | item = mdLinkRe.ReplaceAllString(item, "$1") |
| 375 | out = append(out, bullet.Render("• ")+body.Render(item)) |
| 376 | case strings.HasPrefix(trimmed, "http://") || strings.HasPrefix(trimmed, "https://"): |
| 377 | out = append(out, link.Render(trimmed)) |
| 378 | case strings.Contains(strings.ToLower(trimmed), "full changelog"): |
| 379 | out = append(out, muted.Render(mdLinkRe.ReplaceAllString(trimmed, "$1 ($2)"))) |
| 380 | default: |
| 381 | t := mdBoldRe.ReplaceAllString(trimmed, "$1") |
| 382 | t = mdLinkRe.ReplaceAllString(t, "$1") |
| 383 | out = append(out, body.Render(t)) |
| 384 | } |
| 385 | } |
| 386 | return strings.Join(out, "\n") |
| 387 | } |
| 388 | |
| 275 | 389 | func buildUpdatePromptMessage(currentVersion string, state updatePendingState) (prompt, message string) { |
| 390 | return buildUpdatePromptMessageMode(currentVersion, state, false) |
| 391 | } |
| 392 | |
| 393 | // buildUpdatePromptMessageMode formats the update banner. |
| 394 | // forRofi=true emits Pango markup for Rofi -mesg; false emits lipgloss ANSI for the terminal. |
| 395 | func buildUpdatePromptMessageMode(currentVersion string, state updatePendingState, forRofi bool) (prompt, message string) { |
| 276 | 396 | from := normalizeReleaseVersion(currentVersion) |
| 277 | 397 | to := normalizeReleaseVersion(state.LatestVersion) |
| 278 | | prompt = fmt.Sprintf("Update %s → %s", from, to) |
| 398 | prompt = fmt.Sprintf("✨ Update %s → %s", from, to) |
| 399 | |
| 400 | notes := strings.TrimSpace(state.ReleaseNotes) |
| 401 | if notes == "" { |
| 402 | notes = "(No release notes on GitHub for this release.)" |
| 403 | } |
| 404 | |
| 405 | if forRofi { |
| 406 | var b strings.Builder |
| 407 | title := state.ReleaseName |
| 408 | if title == "" { |
| 409 | title = "Curd " + to |
| 410 | } |
| 411 | b.WriteString(`<span foreground="#7CFC98" size="large"><b>🚀 ` + escapePango(title) + `</b></span>` + "\n") |
| 412 | b.WriteString(`<span foreground="#E6E6FA">Current </span>`) |
| 413 | b.WriteString(`<span foreground="#FF8A80"><b>` + escapePango(from) + `</b></span>`) |
| 414 | b.WriteString(`<span foreground="#E6E6FA"> → Latest </span>`) |
| 415 | b.WriteString(`<span foreground="#7CFC98"><b>` + escapePango(to) + `</b></span>` + "\n") |
| 416 | if state.HTMLURL != "" { |
| 417 | b.WriteString(`<span foreground="#6EC6FF" underline="single">` + escapePango(state.HTMLURL) + `</span>` + "\n") |
| 418 | } |
| 419 | b.WriteString("\n") |
| 420 | b.WriteString(markdownToPango(notes)) |
| 421 | message = strings.TrimSpace(b.String()) |
| 422 | // Pango is verbose; soft-cap markup length. |
| 423 | if len([]rune(message)) > maxReleaseNotesRunes*3 { |
| 424 | r := []rune(message) |
| 425 | message = string(r[:maxReleaseNotesRunes*3]) + "\n<span foreground=\"#9A9A9A\">… (truncated — full notes on GitHub)</span>" |
| 426 | } |
| 427 | return prompt, message |
| 428 | } |
| 429 | |
| 430 | // CLI / terminal |
| 431 | title := lipgloss.NewStyle().Foreground(lipgloss.Color("#7CFC98")).Bold(true) |
| 432 | label := lipgloss.NewStyle().Foreground(lipgloss.Color("#E6E6FA")) |
| 433 | oldV := lipgloss.NewStyle().Foreground(lipgloss.Color("#FF8A80")).Bold(true) |
| 434 | newV := lipgloss.NewStyle().Foreground(lipgloss.Color("#7CFC98")).Bold(true) |
| 435 | link := lipgloss.NewStyle().Foreground(lipgloss.Color("#6EC6FF")).Underline(true) |
| 279 | 436 | |
| 280 | 437 | var b strings.Builder |
| 281 | | if state.ReleaseName != "" { |
| 282 | | fmt.Fprintf(&b, "%s\n", state.ReleaseName) |
| 438 | name := state.ReleaseName |
| 439 | if name == "" { |
| 440 | name = "Curd " + to |
| 283 | 441 | } |
| 284 | | fmt.Fprintf(&b, "Current: %s Latest: %s\n", from, to) |
| 442 | b.WriteString(title.Render("🚀 "+name) + "\n") |
| 443 | b.WriteString(label.Render("Current ") + oldV.Render(from) + label.Render(" → Latest ") + newV.Render(to) + "\n") |
| 285 | 444 | if state.HTMLURL != "" { |
| 286 | | fmt.Fprintf(&b, "%s\n", state.HTMLURL) |
| 445 | b.WriteString(link.Render(state.HTMLURL) + "\n") |
| 287 | 446 | } |
| 288 | 447 | b.WriteString("\n") |
| 289 | | notes := strings.TrimSpace(state.ReleaseNotes) |
| 290 | | if notes == "" { |
| 291 | | notes = "(No release notes on GitHub for this release.)" |
| 292 | | } |
| 293 | | b.WriteString(notes) |
| 448 | b.WriteString(markdownToTerminal(notes)) |
| 294 | 449 | message = strings.TrimSpace(b.String()) |
| 295 | | // Rofi -mesg stays readable; keep a hard cap. |
| 296 | | if runes := []rune(message); len(runes) > maxReleaseNotesRunes { |
| 297 | | message = string(runes[:maxReleaseNotesRunes]) + "\n… (truncated — full notes on GitHub)" |
| 450 | // Cap plain-ish length for terminal |
| 451 | plain := ansiStrip.ReplaceAllString(message, "") |
| 452 | if len([]rune(plain)) > maxReleaseNotesRunes { |
| 453 | // Keep header + truncated notes roughly |
| 454 | message = message + "\n" + label.Render("… (truncated — full notes on GitHub)") |
| 298 | 455 | } |
| 299 | 456 | return prompt, message |
| 300 | 457 | } |
| 301 | 458 | |
| 459 | func updateActionOptions() []SelectionOption { |
| 460 | // Emoji prefixes: lively, ordered, no numeric indices; preserveOrder keeps this order. |
| 461 | return []SelectionOption{ |
| 462 | {Key: "update", Label: "🚀 Update now"}, |
| 463 | {Key: "later", Label: "⏰ Remind me later"}, |
| 464 | {Key: "skip", Label: "⏭️ Skip this version"}, |
| 465 | {Key: "disable", Label: "🔕 Turn off automatic update checks"}, |
| 466 | {Key: "continue", Label: "▶️ Continue without updating"}, |
| 467 | } |
| 468 | } |
| 469 | |
| 302 | 470 | // refreshUpdateStateFromGitHub reloads tag/name/body/url from the live release API |
| 303 | 471 | // so the prompt shows real markdown notes instead of a stale/test seed. |
| 304 | 472 | func refreshUpdateStateFromGitHub(state *updatePendingState) { |
| @@ -371,24 +539,18 @@ func HandlePendingUpdatePrompt(config *CurdConfig, currentVersion string) bool { |
| 371 | 539 | } |
| 372 | 540 | _ = saveUpdatePendingState(config.StoragePath, state) |
| 373 | 541 | |
| 374 | | // Fixed order (Update now first). No index prefixes — Rofi keeps order; |
| 375 | | // CLI uses DynamicSelectPreserveOrder so labels aren't alpha-sorted. |
| 376 | | options := []SelectionOption{ |
| 377 | | {Key: "update", Label: "Update now"}, |
| 378 | | {Key: "later", Label: "Remind me later"}, |
| 379 | | {Key: "skip", Label: "Skip this version"}, |
| 380 | | {Key: "disable", Label: "Turn off automatic update checks"}, |
| 381 | | {Key: "continue", Label: "Continue without updating"}, |
| 382 | | } |
| 383 | | prompt, message := buildUpdatePromptMessage(currentVersion, state) |
| 542 | // Fixed order (Update now first). Emoji labels; preserveOrder for CLI. |
| 543 | options := updateActionOptions() |
| 544 | prompt, message := buildUpdatePromptMessageMode(currentVersion, state, config.RofiSelection) |
| 384 | 545 | |
| 385 | 546 | var selected SelectionOption |
| 386 | 547 | var err error |
| 387 | 548 | if config.RofiSelection { |
| 388 | | // All details go in Rofi -mesg; zero notify-send spam for notes. |
| 549 | // Pango-colored notes in -mesg; one Rofi UI, no notify spam. |
| 389 | 550 | selected, err = RofiSelectWithMessage(options, false, prompt, message) |
| 390 | 551 | } else { |
| 391 | | fmt.Println(prompt) |
| 552 | header := lipgloss.NewStyle().Foreground(lipgloss.Color("#FFD166")).Bold(true) |
| 553 | fmt.Println(header.Render(prompt)) |
| 392 | 554 | fmt.Println(message) |
| 393 | 555 | fmt.Println() |
| 394 | 556 | selected, err = DynamicSelectPreserveOrder(options) |