1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00

If OpenLink errors, show a dialog instead

If the command used by OSCommand.OpenLink fails, lazygit crashes. With this change, if the OpenLink command fails, lazygit just shows a dialog inviting the user to visit the relevant URL.

Fixes #2882
This commit is contained in:
Simon Whitaker 2023-08-05 17:56:21 +01:00
parent cdea5b4873
commit 54776052a1
4 changed files with 47 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/constants"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/mattn/go-runewidth"
)
@ -39,11 +40,28 @@ func (gui *Gui) handleInfoClick() error {
return activeMode.Reset()
}
var title, url string
// if we're not in an active mode we show the donate button
if cx <= runewidth.StringWidth(gui.c.Tr.Donate) {
return gui.os.OpenLink(constants.Links.Donate)
url = constants.Links.Donate
title = gui.c.Tr.Donate
} else if cx <= runewidth.StringWidth(gui.c.Tr.Donate)+1+runewidth.StringWidth(gui.c.Tr.AskQuestion) {
return gui.os.OpenLink(constants.Links.Discussions)
url = constants.Links.Discussions
title = gui.c.Tr.AskQuestion
}
err := gui.os.OpenLink(url)
if err != nil {
// Opening the link via the OS failed for some reason. (For example, this
// can happen if the `os.openLink` config key references a command that
// doesn't exist, or that errors when called.)
//
// In that case, rather than crash the app, fall back to simply showing a
// dialog asking the user to visit the URL.
placeholders := map[string]string{"url": url}
message := utils.ResolvePlaceholderString(gui.c.Tr.PleaseGoToURL, placeholders)
return gui.c.Alert(title, message)
}
return nil
}

View File

@ -585,6 +585,7 @@ type TranslationSet struct {
MarkAsBaseCommit string
MarkAsBaseCommitTooltip string
MarkedCommitMarker string
PleaseGoToURL string
Actions Actions
Bisect Bisect
Log Log
@ -1347,6 +1348,7 @@ func EnglishTranslationSet() TranslationSet {
MarkAsBaseCommit: "Mark commit as base commit for rebase",
MarkAsBaseCommitTooltip: "Select a base commit for the next rebase; this will effectively perform a 'git rebase --onto'.",
MarkedCommitMarker: "↑↑↑ Will rebase from here ↑↑↑",
PleaseGoToURL: "Please go to {{.url}}",
Actions: Actions{
// TODO: combine this with the original keybinding descriptions (those are all in lowercase atm)
CheckoutCommit: "Checkout commit",

View File

@ -233,6 +233,7 @@ var tests = []*components.IntegrationTest{
ui.Accordion,
ui.DoublePopup,
ui.EmptyMenu,
ui.OpenLinkFailure,
ui.SwitchTabFromMenu,
undo.UndoCheckoutAndDrop,
undo.UndoDrop,

View File

@ -0,0 +1,24 @@
package ui
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var OpenLinkFailure = NewIntegrationTest(NewIntegrationTestArgs{
Description: "When opening links via the OS fails, show a dialog instead.",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
config.UserConfig.OS.OpenLink = "exit 42"
},
SetupRepo: func(shell *Shell) {},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Information().Click(0, 0)
t.ExpectPopup().Confirmation().
Title(Equals("Donate")).
Content(Equals("Please go to https://github.com/sponsors/jesseduffield")).
Confirm()
},
})