From 5b160ea59959c5cd2e4b384fe9af87a77cd48076 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 3 Mar 2023 15:20:35 +0530 Subject: [PATCH] Use Once for CachedHostname --- tools/cli/markup/prettify.go | 4 ++-- tools/utils/hostname.go | 16 ++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/tools/cli/markup/prettify.go b/tools/cli/markup/prettify.go index d42c0d858..a78f5dbf0 100644 --- a/tools/cli/markup/prettify.go +++ b/tools/cli/markup/prettify.go @@ -96,7 +96,7 @@ func (self *Context) hyperlink_for_path(path string, text string) string { if err == nil && fi.IsDir() { path = strings.TrimSuffix(path, "/") + "/" } - host := utils.CachedHostname() + host := utils.Hostname() url := "file://" + host + path return self.hyperlink_for_url(url, text) } @@ -119,7 +119,7 @@ func (self *Context) link(x string) string { func (self *Context) ref_hyperlink(x string, prefix string) string { text, target := text_and_target(x) - url := "kitty+doc://" + utils.CachedHostname() + "/#ref=" + prefix + target + url := "kitty+doc://" + utils.Hostname() + "/#ref=" + prefix + target text = replace_all_rst_roles(text, func(group rst_format_match) string { return group.payload }) diff --git a/tools/utils/hostname.go b/tools/utils/hostname.go index 471cacd67..a7e81b9a0 100644 --- a/tools/utils/hostname.go +++ b/tools/utils/hostname.go @@ -11,14 +11,10 @@ var _ = fmt.Print var hostname string = "*" -func CachedHostname() string { - if hostname == "*" { - h, err := os.Hostname() - if err != nil { - hostname = h - } else { - hostname = "" - } +var Hostname = (&Once[string]{Run: func() string { + h, err := os.Hostname() + if err == nil { + return h } - return hostname -} + return "" +}}).Get