diff --git a/color/color.go b/color/color.go deleted file mode 100644 index dc2bf2f..0000000 --- a/color/color.go +++ /dev/null @@ -1,101 +0,0 @@ -package color - -import ( - "os" - "strings" - "sync" -) - -// ANSI color codes -const ( - resetCode = "\033[0m" - redCode = "\033[31m" - greenCode = "\033[32m" - yellowCode = "\033[33m" - blueCode = "\033[34m" - purpleCode = "\033[35m" - cyanCode = "\033[36m" - whiteCode = "\033[37m" - grayCode = "\033[90m" -) - -var ( - useColors bool - colorMu sync.RWMutex -) - -// Color function; makes a call to makeColorFunc with the associated color code -var ( - Reset = makeColorFunc(resetCode) - Red = makeColorFunc(redCode) - Green = makeColorFunc(greenCode) - Yellow = makeColorFunc(yellowCode) - Blue = makeColorFunc(blueCode) - Purple = makeColorFunc(purpleCode) - Cyan = makeColorFunc(cyanCode) - White = makeColorFunc(whiteCode) - Gray = makeColorFunc(grayCode) -) - -func init() { - useColors = DetectShellColors() -} - -func makeColorFunc(code string) func(string) string { - return func(text string) string { - colorMu.RLock() - enabled := useColors - colorMu.RUnlock() - - if enabled { - return code + text + resetCode - } - return text - } -} - -// DetectShellColors checks if the current shell supports colors -func DetectShellColors() bool { - // Check NO_COLOR environment variable (standard) - if os.Getenv("NO_COLOR") != "" { - return false - } - - // Check FORCE_COLOR environment variable - if os.Getenv("FORCE_COLOR") != "" { - return true - } - - // Check if stdout is a terminal - if !isTerminal(os.Stdout) { - return false - } - - // Check TERM environment variable - term := os.Getenv("TERM") - if term == "" || term == "dumb" { - return false - } - - // Common color-supporting terminals - return strings.Contains(term, "color") || - strings.Contains(term, "xterm") || - strings.Contains(term, "screen") || - strings.Contains(term, "tmux") || - term == "linux" || - isWindowsTerminal() -} - -// SetColors enables or disables colors globally -func SetColors(enabled bool) { - colorMu.Lock() - useColors = enabled - colorMu.Unlock() -} - -// ColorsEnabled returns current global color setting -func ColorsEnabled() bool { - colorMu.RLock() - defer colorMu.RUnlock() - return useColors -} diff --git a/color/color_unix.go b/color/color_unix.go deleted file mode 100644 index 3f63471..0000000 --- a/color/color_unix.go +++ /dev/null @@ -1,33 +0,0 @@ -//go:build !windows -// +build !windows - -package color - -import ( - "os" - "syscall" - "unsafe" -) - -const ioctlReadTermios = 0x5401 - -// isTerminal checks if the file is a terminal -func isTerminal(f *os.File) bool { - fd := f.Fd() - var termios syscall.Termios - - r1, _, errno := syscall.Syscall6( - syscall.SYS_IOCTL, - fd, - ioctlReadTermios, - uintptr(unsafe.Pointer(&termios)), - 0, 0, 0, - ) - - return r1 == 0 && errno == 0 -} - -// isWindowsTerminal always returns false on Unix -func isWindowsTerminal() bool { - return false -} diff --git a/color/color_windows.go b/color/color_windows.go deleted file mode 100644 index 059ed9f..0000000 --- a/color/color_windows.go +++ /dev/null @@ -1,29 +0,0 @@ -//go:build windows -// +build windows - -package color - -import ( - "os" - "syscall" - "unsafe" -) - -var ( - kernel32 = syscall.NewLazyDLL("kernel32.dll") - procGetConsoleMode = kernel32.NewProc("GetConsoleMode") -) - -// isTerminal checks if the file is a terminal on Windows -func isTerminal(f *os.File) bool { - handle := syscall.Handle(f.Fd()) - var mode uint32 - - r1, _, _ := procGetConsoleMode.Call(uintptr(handle), uintptr(unsafe.Pointer(&mode))) - return r1 != 0 -} - -// isWindowsTerminal checks for Windows Terminal -func isWindowsTerminal() bool { - return os.Getenv("WT_SESSION") != "" -} diff --git a/go.mod b/go.mod index 7dde99f..6ba8fc3 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,9 @@ module Moonshark go 1.24.1 require ( + git.sharkk.net/Go/Color v1.1.0 git.sharkk.net/Go/LRU v1.0.0 - git.sharkk.net/Sharkk/Fin v1.2.0 + git.sharkk.net/Sharkk/Fin v1.3.0 git.sharkk.net/Sky/LuaJIT-to-Go v0.5.2 github.com/VictoriaMetrics/fastcache v1.12.4 github.com/alexedwards/argon2id v1.0.0 diff --git a/go.sum b/go.sum index f0efd6e..de6139b 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,9 @@ +git.sharkk.net/Go/Color v1.1.0 h1:1eyUwlcerJLo9/42GSnQxOY84/Htdwz/QTu3FGgLEXk= +git.sharkk.net/Go/Color v1.1.0/go.mod h1:cyZFLbUh+GkpsIABVxb5w9EZM+FPj+q9GkCsoECaeTI= git.sharkk.net/Go/LRU v1.0.0 h1:/KqdRVhHldi23aVfQZ4ss6vhCWZqA3vFiQyf1MJPpQc= git.sharkk.net/Go/LRU v1.0.0/go.mod h1:8tdTyl85mss9a+KKwo+Wj9gKHOizhfLfpJhz1ltYz50= -git.sharkk.net/Sharkk/Fin v1.2.0 h1:axhme8vHRYoaB3us7PNfXzXxKOxhpS5BMuNpN8ESe6U= -git.sharkk.net/Sharkk/Fin v1.2.0/go.mod h1:ca0Ej9yCM/vHh1o3YMvBZspme3EtbwoEL2UXN5UPXMo= +git.sharkk.net/Sharkk/Fin v1.3.0 h1:6/f7+h382jJOeo09cgdzH+PGb5XdvajZZRiES52sBkI= +git.sharkk.net/Sharkk/Fin v1.3.0/go.mod h1:ca0Ej9yCM/vHh1o3YMvBZspme3EtbwoEL2UXN5UPXMo= git.sharkk.net/Sky/LuaJIT-to-Go v0.5.2 h1:BgsZPkoqJjQ7Rb+bSs7QQ24+wwLzyc2AALbnpB/n9Kw= git.sharkk.net/Sky/LuaJIT-to-Go v0.5.2/go.mod h1:HQz+D7AFxOfNbTIogjxP+shEBtz1KKrLlLucU+w07c8= github.com/VictoriaMetrics/fastcache v1.12.4 h1:2xvmwZBW+9QtHsXggfzAZRs1FZWCsBs8QDg22bMidf0= diff --git a/http/server.go b/http/server.go index 815c2a5..cb9f824 100644 --- a/http/server.go +++ b/http/server.go @@ -6,7 +6,6 @@ import ( "strings" "time" - "Moonshark/color" "Moonshark/config" "Moonshark/logger" "Moonshark/metadata" @@ -14,6 +13,8 @@ import ( "Moonshark/sessions" "Moonshark/utils" + "git.sharkk.net/Go/Color" + "github.com/valyala/fasthttp" ) diff --git a/logger/logger.go b/logger/logger.go index cde131e..fc3fbab 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -9,7 +9,7 @@ import ( "sync/atomic" "time" - "Moonshark/color" + "git.sharkk.net/Go/Color" ) // Log levels diff --git a/main.go b/main.go index e02a53d..defefb1 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "Moonshark/color" "Moonshark/config" "Moonshark/http" "Moonshark/logger" @@ -12,9 +11,10 @@ import ( "bytes" "flag" "fmt" - "os" "time" + "git.sharkk.net/Go/Color" + fin "git.sharkk.net/Sharkk/Fin" "github.com/valyala/fasthttp" ) @@ -40,7 +40,7 @@ func main() { color.SetColors(color.DetectShellColors()) banner(sptMode) - cfg = config.New(readConfig(*cfgPath)) + cfg = config.New(fin.LoadFromFile(*cfgPath)) dbg = *dbgFlag || cfg.Server.Debug logger.Debug(dbg) if dbg { @@ -69,23 +69,6 @@ func requestMux(ctx *fasthttp.RequestCtx) { } } -// Attempt to read the given path for config, or return an empty struct for defaults -func readConfig(path string) *fin.Data { - file, err := os.Open(path) - if err != nil { - logger.Errorf("Failed to open config file %s, using defaults", color.Yellow(path)) - return fin.NewData() - } - defer file.Close() - - cfg, err := fin.Load(file) - if err != nil { - logger.Errorf("Failed to load config file %s, using defaults", color.Yellow(path)) - } - - return cfg -} - // Print our super-awesome banner with the current version! func banner(scriptMode bool) { if scriptMode { diff --git a/moonshark.go b/moonshark.go index 7decf16..39d66fd 100644 --- a/moonshark.go +++ b/moonshark.go @@ -13,7 +13,6 @@ import ( "syscall" "time" - "Moonshark/color" "Moonshark/config" "Moonshark/http" "Moonshark/logger" @@ -22,6 +21,8 @@ import ( "Moonshark/runner/lualibs" "Moonshark/sessions" "Moonshark/watchers" + + "git.sharkk.net/Go/Color" ) type Moonshark struct { diff --git a/runner/lualibs/fs.go b/runner/lualibs/fs.go index 6c71ec8..5d6d6e3 100644 --- a/runner/lualibs/fs.go +++ b/runner/lualibs/fs.go @@ -8,9 +8,10 @@ import ( "strings" "time" - "Moonshark/color" "Moonshark/logger" + "git.sharkk.net/Go/Color" + lru "git.sharkk.net/Go/LRU" luajit "git.sharkk.net/Sky/LuaJIT-to-Go" "github.com/golang/snappy" diff --git a/runner/sqlite/sqlite.go b/runner/sqlite/sqlite.go index 4e51905..4f8f46e 100644 --- a/runner/sqlite/sqlite.go +++ b/runner/sqlite/sqlite.go @@ -11,9 +11,10 @@ import ( sqlite "zombiezen.com/go/sqlite" "zombiezen.com/go/sqlite/sqlitex" - "Moonshark/color" "Moonshark/logger" + "git.sharkk.net/Go/Color" + luajit "git.sharkk.net/Sky/LuaJIT-to-Go" ) diff --git a/watchers/api.go b/watchers/api.go index 9d1bdd7..c89c305 100644 --- a/watchers/api.go +++ b/watchers/api.go @@ -5,10 +5,11 @@ import ( "strings" "sync" - "Moonshark/color" "Moonshark/logger" "Moonshark/router" "Moonshark/runner" + + "git.sharkk.net/Go/Color" ) // Global watcher manager instance with explicit creation