| @@ -662,15 +662,35 @@ func isCrossDeviceError(err error) bool { |
| 662 | strings.Contains(msg, "invalid cross-device link") |
662 | strings.Contains(msg, "invalid cross-device link") |
| 663 | } |
663 | } |
| 664 | |
664 | |
| |
665 | func hasDisplay() bool { |
| |
666 | return os.Getenv("DISPLAY") != "" || os.Getenv("WAYLAND_DISPLAY") != "" |
| |
667 | } |
| |
668 | |
| |
669 | func stdinIsTerminal() bool { |
| |
670 | return term.IsTerminal(int(os.Stdin.Fd())) |
| |
671 | } |
| |
672 | |
| |
673 | // preferGUIPasswordPrompt decides whether to open a desktop password dialog. |
| |
674 | // |
| |
675 | // - Rofi/desktop mode → GUI first (zenity/yad/kdialog) |
| |
676 | // - Plain CLI with a TTY (e.g. `curd -u` in a terminal) → terminal only |
| |
677 | // - No TTY but a display → GUI |
| 665 | func preferGUIPasswordPrompt() bool { |
678 | func preferGUIPasswordPrompt() bool { |
| 666 | if cfg := GetGlobalConfig(); cfg != nil && cfg.RofiSelection { |
679 | rofi := false |
| 667 | return true |
680 | if cfg := GetGlobalConfig(); cfg != nil { |
| |
681 | rofi = cfg.RofiSelection |
| 668 | } |
682 | } |
| 669 | // No usable TTY (common when launched from a desktop entry / rofi). |
683 | tty := stdinIsTerminal() |
| 670 | if !term.IsTerminal(int(os.Stdin.Fd())) { |
684 | |
| |
685 | // Interactive CLI: never pop a GUI dialog just because DISPLAY is set. |
| |
686 | if tty && !rofi { |
| |
687 | return false |
| |
688 | } |
| |
689 | if rofi { |
| 671 | return true |
690 | return true |
| 672 | } |
691 | } |
| 673 | return os.Getenv("DISPLAY") != "" || os.Getenv("WAYLAND_DISPLAY") != "" |
692 | // Headless of TTY (desktop launcher without a terminal): GUI if available. |
| |
693 | return !tty && hasDisplay() |
| 674 | } |
694 | } |
| 675 | |
695 | |
| 676 | // promptSudoPasswordGUI tries GTK/desktop password dialogs (zenity → yad → kdialog). |
696 | // promptSudoPasswordGUI tries GTK/desktop password dialogs (zenity → yad → kdialog). |
| @@ -724,22 +744,38 @@ func promptSudoPassword(prompt string) (string, error) { |
| 724 | prompt = "Administrator password (sudo) to install update: " |
744 | prompt = "Administrator password (sudo) to install update: " |
| 725 | } |
745 | } |
| 726 | |
746 | |
| 727 | // Rofi / desktop: prefer GTK password dialog so the user isn't dumped to a TTY. |
747 | rofi := false |
| |
748 | if cfg := GetGlobalConfig(); cfg != nil { |
| |
749 | rofi = cfg.RofiSelection |
| |
750 | } |
| |
751 | tty := stdinIsTerminal() |
| |
752 | |
| |
753 | // Rofi/desktop: try zenity/yad/kdialog first. |
| 728 | if preferGUIPasswordPrompt() { |
754 | if preferGUIPasswordPrompt() { |
| 729 | if password, err := promptSudoPasswordGUI(prompt); err == nil { |
755 | if password, err := promptSudoPasswordGUI(prompt); err == nil { |
| 730 | return password, nil |
756 | return password, nil |
| 731 | } else { |
757 | } else { |
| 732 | Log(fmt.Sprintf("GUI password prompt unavailable (%v); falling back to terminal", err)) |
758 | Log(fmt.Sprintf("GUI password prompt unavailable (%v)", err)) |
| 733 | // If cancel was explicit, don't fall through to a broken TTY prompt in rofi mode. |
759 | // Explicit cancel in GUI: only abort if we shouldn't fall back to TTY. |
| 734 | if strings.Contains(err.Error(), "cancelled") { |
760 | // In rofi mode with no TTY, cancel means cancel. |
| 735 | return "", err |
761 | if strings.Contains(err.Error(), "cancelled") && (!tty || rofi) { |
| |
762 | // If we still have a real terminal under us (rare with rofi), allow TTY |
| |
763 | // fallback only when not in rofi mode. |
| |
764 | if !tty { |
| |
765 | return "", err |
| |
766 | } |
| |
767 | if rofi { |
| |
768 | // Rofi session: user closed the dialog on purpose. |
| |
769 | return "", err |
| |
770 | } |
| 736 | } |
771 | } |
| |
772 | // GUI missing/failed → fall through to terminal when possible. |
| 737 | } |
773 | } |
| 738 | } |
774 | } |
| 739 | |
775 | |
| 740 | // Terminal fallback (CLI mode or no GUI tool installed). |
776 | // Terminal path (CLI `curd -u`, or GUI unavailable). |
| 741 | if !term.IsTerminal(int(os.Stdin.Fd())) { |
777 | if !tty { |
| 742 | return "", fmt.Errorf("no terminal available for password entry; install zenity for a GUI prompt") |
778 | return "", fmt.Errorf("no terminal available for password entry; install zenity/yad/kdialog for a GUI prompt") |
| 743 | } |
779 | } |
| 744 | fmt.Fprint(os.Stderr, prompt) |
780 | fmt.Fprint(os.Stderr, prompt) |
| 745 | if !strings.HasSuffix(prompt, " ") { |
781 | if !strings.HasSuffix(prompt, " ") { |