| @@ -268,6 +268,51 @@ func pendingUpdateShouldPrompt(config *CurdConfig, currentVersion string, state |
| 268 | return true |
268 | return true |
| 269 | } |
269 | } |
| 270 | |
270 | |
| |
271 | func formatLocalTime(t time.Time) string { |
| |
272 | return t.In(time.Local).Format("Mon Jan 2 2006, 3:04 PM MST") |
| |
273 | } |
| |
274 | |
| |
275 | func buildUpdatePromptMessage(currentVersion string, state updatePendingState) (prompt, message string) { |
| |
276 | from := normalizeReleaseVersion(currentVersion) |
| |
277 | to := normalizeReleaseVersion(state.LatestVersion) |
| |
278 | prompt = fmt.Sprintf("Update %s → %s", from, to) |
| |
279 | |
| |
280 | var b strings.Builder |
| |
281 | if state.ReleaseName != "" { |
| |
282 | fmt.Fprintf(&b, "%s\n", state.ReleaseName) |
| |
283 | } |
| |
284 | fmt.Fprintf(&b, "Current: %s Latest: %s\n", from, to) |
| |
285 | if state.HTMLURL != "" { |
| |
286 | fmt.Fprintf(&b, "%s\n", state.HTMLURL) |
| |
287 | } |
| |
288 | b.WriteString("\n") |
| |
289 | b.WriteString(state.ReleaseNotes) |
| |
290 | message = strings.TrimSpace(b.String()) |
| |
291 | // Rofi -mesg stays readable; keep a hard cap. |
| |
292 | if runes := []rune(message); len(runes) > maxReleaseNotesRunes { |
| |
293 | message = string(runes[:maxReleaseNotesRunes]) + "\n… (truncated)" |
| |
294 | } |
| |
295 | return prompt, message |
| |
296 | } |
| |
297 | |
| |
298 | // updateUserMessage prints a single status line. With Rofi mode, CurdOut becomes |
| |
299 | // notify-send — so we only send one short notification (or log) for status. |
| |
300 | func updateUserMessage(config *CurdConfig, msg string) { |
| |
301 | msg = strings.TrimSpace(msg) |
| |
302 | if msg == "" { |
| |
303 | return |
| |
304 | } |
| |
305 | Log(msg) |
| |
306 | if config != nil && config.RofiSelection { |
| |
307 | // One short desktop notification, not a barrage of CurdOut lines. |
| |
308 | _ = exec.Command("notify-send", "-a", "Curd", |
| |
309 | "-h", "string:x-canonical-private-synchronous:curd-update", |
| |
310 | "Curd", msg).Run() |
| |
311 | return |
| |
312 | } |
| |
313 | fmt.Println(msg) |
| |
314 | } |
| |
315 | |
| 271 | // HandlePendingUpdatePrompt shows a previously detected update (from idle check). |
316 | // HandlePendingUpdatePrompt shows a previously detected update (from idle check). |
| 272 | // Returns true if the caller should exit (user updated or chose to quit the session). |
317 | // Returns true if the caller should exit (user updated or chose to quit the session). |
| 273 | func HandlePendingUpdatePrompt(config *CurdConfig, currentVersion string) bool { |
318 | func HandlePendingUpdatePrompt(config *CurdConfig, currentVersion string) bool { |
| @@ -279,63 +324,64 @@ func HandlePendingUpdatePrompt(config *CurdConfig, currentVersion string) bool { |
| 279 | return false |
324 | return false |
| 280 | } |
325 | } |
| 281 | |
326 | |
| 282 | CurdOut("") |
327 | options := []SelectionOption{ |
| 283 | CurdOut(fmt.Sprintf("Update available: %s → %s", normalizeReleaseVersion(currentVersion), state.LatestVersion)) |
| |
| 284 | if state.ReleaseName != "" { |
| |
| 285 | CurdOut(state.ReleaseName) |
| |
| 286 | } |
| |
| 287 | if state.HTMLURL != "" { |
| |
| 288 | CurdOut(state.HTMLURL) |
| |
| 289 | } |
| |
| 290 | CurdOut("") |
| |
| 291 | CurdOut("Release notes:") |
| |
| 292 | CurdOut(state.ReleaseNotes) |
| |
| 293 | CurdOut("") |
| |
| 294 | |
| |
| 295 | selected, err := promptSelect([]SelectionOption{ |
| |
| 296 | {Key: "update", Label: "Update now"}, |
328 | {Key: "update", Label: "Update now"}, |
| 297 | {Key: "later", Label: "Remind me later"}, |
329 | {Key: "later", Label: "Remind me later"}, |
| 298 | {Key: "skip", Label: "Skip this version"}, |
330 | {Key: "skip", Label: "Skip this version"}, |
| 299 | {Key: "disable", Label: "Turn off automatic update checks"}, |
331 | {Key: "disable", Label: "Turn off automatic update checks"}, |
| 300 | {Key: "continue", Label: "Continue without updating"}, |
332 | {Key: "continue", Label: "Continue without updating"}, |
| 301 | }) |
333 | } |
| |
334 | prompt, message := buildUpdatePromptMessage(currentVersion, state) |
| |
335 | |
| |
336 | var selected SelectionOption |
| |
337 | var err error |
| |
338 | if config.RofiSelection { |
| |
339 | // All details go in Rofi -mesg; zero notify-send spam for notes. |
| |
340 | selected, err = RofiSelectWithMessage(options, false, prompt, message) |
| |
341 | } else { |
| |
342 | fmt.Println(prompt) |
| |
343 | fmt.Println(message) |
| |
344 | fmt.Println() |
| |
345 | selected, err = promptSelect(options) |
| |
346 | } |
| 302 | if err != nil || selected.Key == "-1" || selected.Key == "-2" || selected.Key == "continue" || selected.Key == "" { |
347 | if err != nil || selected.Key == "-1" || selected.Key == "-2" || selected.Key == "continue" || selected.Key == "" { |
| 303 | return false |
348 | return false |
| 304 | } |
349 | } |
| 305 | |
350 | |
| 306 | switch selected.Key { |
351 | switch selected.Key { |
| 307 | case "update": |
352 | case "update": |
| 308 | CurdOut("Downloading and installing update…") |
353 | updateUserMessage(config, "Downloading and installing update…") |
| 309 | if err := UpdateCurd(defaultUpdateRepo, "curd"); err != nil { |
354 | if err := UpdateCurd(defaultUpdateRepo, "curd"); err != nil { |
| 310 | CurdOut(fmt.Sprintf("Update failed: %v", err)) |
355 | updateUserMessage(config, fmt.Sprintf("Update failed: %v", err)) |
| 311 | Log(fmt.Sprintf("Update failed: %v", err)) |
356 | Log(fmt.Sprintf("Update failed: %v", err)) |
| 312 | return false |
357 | return false |
| 313 | } |
358 | } |
| 314 | state.Available = false |
359 | state.Available = false |
| 315 | state.RemindAfter = "" |
360 | state.RemindAfter = "" |
| 316 | _ = saveUpdatePendingState(config.StoragePath, state) |
361 | _ = saveUpdatePendingState(config.StoragePath, state) |
| 317 | CurdOut(fmt.Sprintf("Updated to %s. Please restart curd.", state.LatestVersion)) |
362 | updateUserMessage(config, fmt.Sprintf("Updated to %s. Please restart curd.", state.LatestVersion)) |
| 318 | return true |
363 | return true |
| 319 | case "later": |
364 | case "later": |
| 320 | state.RemindAfter = time.Now().Add(defaultRemindLaterDuration).UTC().Format(time.RFC3339) |
365 | until := time.Now().Add(defaultRemindLaterDuration) |
| |
366 | state.RemindAfter = until.UTC().Format(time.RFC3339) |
| 321 | state.Available = true |
367 | state.Available = true |
| 322 | _ = saveUpdatePendingState(config.StoragePath, state) |
368 | _ = saveUpdatePendingState(config.StoragePath, state) |
| 323 | CurdOut(fmt.Sprintf("Will remind again after %s.", state.RemindAfter)) |
369 | updateUserMessage(config, fmt.Sprintf("Will remind again after %s.", formatLocalTime(until))) |
| 324 | return false |
370 | return false |
| 325 | case "skip": |
371 | case "skip": |
| 326 | state.SkippedVersion = state.LatestVersion |
372 | state.SkippedVersion = state.LatestVersion |
| 327 | state.Available = false |
373 | state.Available = false |
| 328 | state.RemindAfter = "" |
374 | state.RemindAfter = "" |
| 329 | _ = saveUpdatePendingState(config.StoragePath, state) |
375 | _ = saveUpdatePendingState(config.StoragePath, state) |
| 330 | CurdOut(fmt.Sprintf("Skipping version %s.", state.LatestVersion)) |
376 | updateUserMessage(config, fmt.Sprintf("Skipping version %s.", state.LatestVersion)) |
| 331 | return false |
377 | return false |
| 332 | case "disable": |
378 | case "disable": |
| 333 | if err := setConfigBoolOption(GlobalConfigPath, "CheckUpdates", false); err != nil { |
379 | if err := setConfigBoolOption(GlobalConfigPath, "CheckUpdates", false); err != nil { |
| 334 | CurdOut(fmt.Sprintf("Could not write config: %v", err)) |
380 | updateUserMessage(config, fmt.Sprintf("Could not write config: %v", err)) |
| 335 | Log(fmt.Sprintf("disable CheckUpdates: %v", err)) |
381 | Log(fmt.Sprintf("disable CheckUpdates: %v", err)) |
| 336 | } else { |
382 | } else { |
| 337 | config.CheckUpdates = false |
383 | config.CheckUpdates = false |
| 338 | CurdOut("Automatic update checks disabled (CheckUpdates=false).") |
384 | updateUserMessage(config, "Automatic update checks disabled (CheckUpdates=false).") |
| 339 | } |
385 | } |
| 340 | state.Available = false |
386 | state.Available = false |
| 341 | _ = saveUpdatePendingState(config.StoragePath, state) |
387 | _ = saveUpdatePendingState(config.StoragePath, state) |
| @@ -443,7 +489,7 @@ func replaceExecutable(tmpPath, executablePath string) error { |
| 443 | } |
489 | } |
| 444 | |
490 | |
| 445 | // Permission denied — e.g. installed to /usr/local/bin. Ask for sudo. |
491 | // Permission denied — e.g. installed to /usr/local/bin. Ask for sudo. |
| 446 | CurdOut("Update needs elevated permissions to replace the installed binary.") |
492 | updateUserMessage(GetGlobalConfig(), "Update needs elevated permissions to replace the installed binary.") |
| 447 | if err := installExecutableWithSudo(tmpPath, executablePath); err != nil { |
493 | if err := installExecutableWithSudo(tmpPath, executablePath); err != nil { |
| 448 | return err |
494 | return err |
| 449 | } |
495 | } |