From e1acb6a547946cfe9e2324f44e78777fe1f6f36a Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 16 Aug 2024 09:09:25 +0200 Subject: [PATCH] Set an openHyperlink function on gocui --- pkg/gui/gui.go | 23 +++++++++++++++++++++++ pkg/i18n/english.go | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index c26774fd0..d608fb3a4 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "reflect" + "regexp" "sort" "strings" "sync" @@ -359,6 +360,28 @@ func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, contextKey types.Context return nil }) + gui.g.SetOpenHyperlinkFunc(func(url string) error { + if strings.HasPrefix(url, "lazygit-edit:") { + re := regexp.MustCompile(`^lazygit-edit://(.+?)(?::(\d+))?$`) + matches := re.FindStringSubmatch(url) + if matches == nil { + return fmt.Errorf(gui.Tr.InvalidLazygitEditURL, url) + } + filepath := matches[1] + if matches[2] != "" { + lineNumber := utils.MustConvertToInt(matches[2]) + return gui.helpers.Files.EditFileAtLine(filepath, lineNumber) + } + return gui.helpers.Files.EditFiles([]string{filepath}) + } + + if err := gui.os.OpenLink(url); err != nil { + return fmt.Errorf(gui.Tr.FailedToOpenURL, url, err) + } + + return nil + }) + // if a context key has been given, push that instead, and set its index to 0 if contextKey != context.NO_CONTEXT { contextToPush = gui.c.ContextForKey(contextKey) diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index f4520a3ee..cdc1017c0 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -784,7 +784,9 @@ type TranslationSet struct { MarkAsBaseCommit string MarkAsBaseCommitTooltip string MarkedCommitMarker string + FailedToOpenURL string PleaseGoToURL string + InvalidLazygitEditURL string NoCopiedCommits string DisabledMenuItemPrefix string QuickStartInteractiveRebase string @@ -1770,7 +1772,9 @@ func EnglishTranslationSet() *TranslationSet { MarkAsBaseCommit: "Mark as base commit for rebase", MarkAsBaseCommitTooltip: "Select a base commit for the next rebase. When you rebase onto a branch, only commits above the base commit will be brought across. This uses the `git rebase --onto` command.", MarkedCommitMarker: "↑↑↑ Will rebase from here ↑↑↑", + FailedToOpenURL: "Failed to open URL %s\n\nError: %v", PleaseGoToURL: "Please go to {{.url}}", + InvalidLazygitEditURL: "Invalid lazygit-edit URL format: %s", DisabledMenuItemPrefix: "Disabled: ", NoCopiedCommits: "No copied commits", QuickStartInteractiveRebase: "Start interactive rebase",