Use Once for CachedHostname

This commit is contained in:
Kovid Goyal 2023-03-03 15:20:35 +05:30
parent e6662e11c3
commit 5b160ea599
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 8 additions and 12 deletions

View File

@ -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
})

View File

@ -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