Replace use of sync.Once

This commit is contained in:
Kovid Goyal 2023-02-22 08:17:20 +05:30
parent fa0773d9d2
commit 587d06b295
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 15 additions and 41 deletions

View File

@ -66,8 +66,8 @@ func complete_plus_open(completions *cli.Completions, word string, arg_num int)
} }
func complete_themes(completions *cli.Completions, word string, arg_num int) { func complete_themes(completions *cli.Completions, word string, arg_num int) {
kitty, err := utils.KittyExe() kitty := utils.KittyExe()
if err == nil { if kitty != "" {
out, err := exec.Command(kitty, "+runpy", "from kittens.themes.collection import *; print_theme_names()").Output() out, err := exec.Command(kitty, "+runpy", "from kittens.themes.collection import *; print_theme_names()").Output()
if err == nil { if err == nil {
mg := completions.AddMatchGroup("Themes") mg := completions.AddMatchGroup("Themes")

View File

@ -15,7 +15,6 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -63,26 +62,15 @@ func Abspath(path string) string {
return path return path
} }
var config_dir, kitty_exe, cache_dir, runtime_dir string var KittyExe = (&Once[string]{Run: func() string {
var kitty_exe_err error
var config_dir_once, kitty_exe_once, cache_dir_once, runtime_dir_once sync.Once
func find_kitty_exe() {
exe, err := os.Executable() exe, err := os.Executable()
if err == nil { if err == nil {
kitty_exe = filepath.Join(filepath.Dir(exe), "kitty") return filepath.Join(filepath.Dir(exe), "kitty")
kitty_exe_err = unix.Access(kitty_exe, unix.X_OK)
} else {
kitty_exe_err = err
}
} }
return ""
}}).Get
func KittyExe() (string, error) { var ConfigDir = (&Once[string]{Run: func() (config_dir string) {
kitty_exe_once.Do(find_kitty_exe)
return kitty_exe, kitty_exe_err
}
func find_config_dir() {
if os.Getenv("KITTY_CONFIG_DIRECTORY") != "" { if os.Getenv("KITTY_CONFIG_DIRECTORY") != "" {
config_dir = Abspath(Expanduser(os.Getenv("KITTY_CONFIG_DIRECTORY"))) config_dir = Abspath(Expanduser(os.Getenv("KITTY_CONFIG_DIRECTORY")))
} else { } else {
@ -110,14 +98,10 @@ func find_config_dir() {
} }
} }
} }
} return
}}).Get
func ConfigDir() string { var CacheDir = (&Once[string]{Run: func() (cache_dir string) {
config_dir_once.Do(find_config_dir)
return config_dir
}
func find_cache_dir() {
candidate := "" candidate := ""
if edir := os.Getenv("KITTY_CACHE_DIRECTORY"); edir != "" { if edir := os.Getenv("KITTY_CACHE_DIRECTORY"); edir != "" {
candidate = Abspath(Expanduser(edir)) candidate = Abspath(Expanduser(edir))
@ -131,13 +115,8 @@ func find_cache_dir() {
candidate = filepath.Join(Expanduser(candidate), "kitty") candidate = filepath.Join(Expanduser(candidate), "kitty")
} }
os.MkdirAll(candidate, 0o755) os.MkdirAll(candidate, 0o755)
cache_dir = candidate return candidate
} }}).Get
func CacheDir() string {
cache_dir_once.Do(find_cache_dir)
return cache_dir
}
func macos_user_cache_dir() string { func macos_user_cache_dir() string {
// Sadly Go does not provide confstr() so we use this hack. We could // Sadly Go does not provide confstr() so we use this hack. We could
@ -163,7 +142,7 @@ func macos_user_cache_dir() string {
return "" return ""
} }
func find_runtime_dir() { var RuntimeDir = (&Once[string]{Run: func() (runtime_dir string) {
var candidate string var candidate string
if q := os.Getenv("KITTY_RUNTIME_DIRECTORY"); q != "" { if q := os.Getenv("KITTY_RUNTIME_DIRECTORY"); q != "" {
candidate = q candidate = q
@ -185,13 +164,8 @@ func find_runtime_dir() {
if s, err := os.Stat(candidate); err == nil && s.Mode().Perm() != 0o700 { if s, err := os.Stat(candidate); err == nil && s.Mode().Perm() != 0o700 {
os.Chmod(candidate, 0o700) os.Chmod(candidate, 0o700)
} }
runtime_dir = candidate return candidate
} }}).Get
func RuntimeDir() string {
runtime_dir_once.Do(find_runtime_dir)
return runtime_dir
}
type Walk_callback func(path, abspath string, d fs.DirEntry, err error) error type Walk_callback func(path, abspath string, d fs.DirEntry, err error) error