bring in ext color package, use new fin loading func

This commit is contained in:
Sky Johnson 2025-06-06 13:56:46 -05:00
parent a2d9b0ad9f
commit 43b9dd7320
12 changed files with 20 additions and 192 deletions

View File

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

View File

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

View File

@ -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") != ""
}

3
go.mod
View File

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

6
go.sum
View File

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

View File

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

View File

@ -9,7 +9,7 @@ import (
"sync/atomic"
"time"
"Moonshark/color"
"git.sharkk.net/Go/Color"
)
// Log levels

23
main.go
View File

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

View File

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

View File

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

View File

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

View File

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