refactor!: replace hilbish.xdg with hilbish.userDir

"xdg" is basically a linux specific thing and it acted linux specific also.
so replace that with a more portable and general "userDir" table

config is for the user's config directory and also defaults to xdg vars on linux.
data is a directory to store data, mostly for scripts.
on other oses beside linux, it is just the config directory for now
dev
TorchedSammy 2021-12-13 20:13:17 -04:00
parent b920f7d7b1
commit 2e4bea8292
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 35 additions and 16 deletions

View File

@ -5,6 +5,7 @@ package main
import (
"os"
"path/filepath"
"runtime"
"strings"
@ -40,12 +41,22 @@ func hilbishLoader(L *lua.LState) int {
util.SetField(L, mod, "interactive", lua.LBool(interactive), "If this is an interactive shell")
util.SetField(L, mod, "login", lua.LBool(interactive), "Whether this is a login shell")
xdg := L.NewTable()
util.SetField(L, xdg, "config", lua.LString(confDir), "XDG config directory")
util.SetField(L, xdg, "data", lua.LString(getenv("XDG_DATA_HOME", curuser.HomeDir + "/.local/share")), "XDG data directory")
L.SetField(mod, "xdg", xdg)
hshuser := L.NewTable()
userConfigDir, _ := os.UserConfigDir()
userDataDir := ""
// i honestly dont know what directories to use for this
switch runtime.GOOS {
case "linux":
userDataDir = getenv("XDG_DATA_HOME", curuser.HomeDir + "/.local/share")
default:
userDataDir = filepath.Join(userConfigDir, "data")
}
util.SetField(L, hshuser, "config", lua.LString(userConfigDir), "User's config directory")
util.SetField(L, hshuser, "data", lua.LString(userDataDir), "XDG data directory")
util.Document(L, hshuser, "User directories to store configs and/or modules.")
L.SetField(mod, "userDir", hshuser)
util.Document(L, xdg, "Variables for the XDG base directory spec.")
util.Document(L, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.")
L.Push(mod)
@ -53,7 +64,7 @@ func hilbishLoader(L *lua.LState) int {
}
// run(cmd)
// Runs `cmd` in Hilbish's sh interpreter
// Runs `cmd` in Hilbish's sh interpreter.
func hlrun(L *lua.LState) int {
var exitcode uint8
cmd := L.CheckString(1)
@ -74,7 +85,14 @@ func hlrun(L *lua.LState) int {
func hlflag(L *lua.LState) int {
flagchar := L.CheckString(1)
L.Push(lua.LBool(getopt.Lookup([]rune(flagchar)[0]).Seen()))
flag := getopt.Lookup([]rune(flagchar)[0])
if flag == nil {
L.Push(lua.LNil)
return 1
}
passed := flag.Seen()
L.Push(lua.LBool(passed))
return 1
}

View File

@ -8,12 +8,12 @@ var (
.. hilbish.dataDir .. '/libs/?/init.lua;'
.. hilbish.dataDir .. '/libs/?/?.lua;'` + linuxUserPaths
linuxUserPaths = `
.. hilbish.xdg.data .. '/hilbish/libs/?/init.lua;'
.. hilbish.xdg.data .. '/hilbish/libs/?/?.lua;'
.. hilbish.xdg.data .. '/hilbish/libs/?.lua;'
.. hilbish.xdg.config .. '/hilbish/?/init.lua;'
.. hilbish.xdg.config .. '/hilbish/?/?.lua;'
.. hilbish.xdg.config .. '/hilbish/?.lua'`
.. hilbish.userDir.data .. '/hilbish/libs/?/init.lua;'
.. hilbish.userDir.data .. '/hilbish/libs/?/?.lua;'
.. hilbish.userDir.data .. '/hilbish/libs/?.lua;'
.. hilbish.userDir.config .. '/hilbish/?/init.lua;'
.. hilbish.userDir.config .. '/hilbish/?/?.lua;'
.. hilbish.userDir.config .. '/hilbish/?.lua'`
dataDir = "/usr/share/hilbish"
preloadPath = dataDir + "/preload.lua"
sampleConfPath = dataDir + "/.hilbishrc.lua" // Path to default/sample config

View File

@ -4,9 +4,10 @@ package main
// String vars that are free to be changed at compile time
var (
requirePaths = commonRequirePaths + `
.. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\init.lua;'
.. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\?.lua;'`
requirePaths = commonRequirePaths + `.. ';'
.. hilbish.user.config .. '\\Hilbish\\libs\\?\\init.lua;'
.. hilbish.user.config .. '\\Hilbish\\libs\\?\\?.lua;'
.. hilbish.user.config .. '\\Hilbish\\libs\\?.lua;'`
dataDir = "~\\Appdata\\Roaming\\Hilbish" // ~ and \ gonna cry?
preloadPath = dataDir + "\\preload.lua"
sampleConfPath = dataDir + "\\hilbishrc.lua" // Path to default/sample config