From 5b28e6c3e17c7d3df1a2d4ecf9d7ca2c4e217b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Ivanovi=C4=87?= Date: Sun, 3 Sep 2023 16:27:21 +0200 Subject: [PATCH] Use XDG for history file Add an option to first check for default cache directory (usually the one set with $XDG_CACHE_HOME). If it fails, it uses the file in users home directory. More about XDG Base Directory Specification: - https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html - https://xdgbasedirectoryspecification.com/ --- command.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/command.go b/command.go index 0dbe5a2..89ea731 100644 --- a/command.go +++ b/command.go @@ -171,11 +171,25 @@ func runStdin() { func runRepl() { var historyFilePath string - homeDir, err := os.UserHomeDir() - if err == nil { - historyFilePath = path.Join(homeDir, ".oak_history") + var cacheDir string + if rootCacheDir, err := os.UserCacheDir(); err == nil && rootCacheDir != "" { + cacheDir = path.Join(rootCacheDir, "oak") + _, err = os.Stat(cacheDir) + if os.IsNotExist(err) { + if err := os.Mkdir(cacheDir, 0755); err != nil { + fmt.Println("Could not create cache directory:", cacheDir) + fmt.Println(err) + cacheDir = "" + } + } + } + if cacheDir != "" { + historyFilePath = path.Join(cacheDir, ".oak_history") + } else { + if homeDir, err := os.UserHomeDir(); err == nil { + historyFilePath = path.Join(homeDir, ".oak_history") + } } - rl, err := readline.NewEx(&readline.Config{ Prompt: "> ", HistoryFile: historyFilePath,