2026-01-16 14:41:26 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
2026-01-16 16:02:17 +01:00
|
|
|
"time"
|
2026-01-16 14:41:26 +01:00
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// debugLog writes to a debug file
|
|
|
|
|
var debugFile *os.File
|
|
|
|
|
|
|
|
|
|
func debugLog(format string, args ...any) {
|
|
|
|
|
if debugFile != nil {
|
|
|
|
|
fmt.Fprintf(debugFile, format+"\n", args...)
|
|
|
|
|
debugFile.Sync()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
serverAddr := flag.String("server", "127.0.0.1:9987", "TeamSpeak 3 Server Address")
|
|
|
|
|
nickname := flag.String("nickname", "TUI-User", "Your nickname")
|
2026-01-16 16:02:17 +01:00
|
|
|
debug := flag.Bool("debug", true, "Enable debug logging to file (default true)")
|
2026-01-16 14:41:26 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
// Disable log output completely to prevent TUI corruption
|
|
|
|
|
log.SetOutput(io.Discard)
|
|
|
|
|
|
|
|
|
|
// Enable debug file logging if requested
|
|
|
|
|
if *debug {
|
|
|
|
|
var err error
|
2026-01-16 16:02:17 +01:00
|
|
|
timestamp := time.Now().Format("20060102-150405")
|
|
|
|
|
filename := fmt.Sprintf("tui-%s.log", timestamp)
|
|
|
|
|
debugFile, err = os.Create(filename)
|
2026-01-16 14:41:26 +01:00
|
|
|
if err == nil {
|
|
|
|
|
defer debugFile.Close()
|
2026-01-16 16:02:17 +01:00
|
|
|
debugLog("TUI Debug started at %s", timestamp)
|
2026-01-16 14:41:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the TUI model
|
|
|
|
|
m := NewModel(*serverAddr, *nickname)
|
|
|
|
|
|
|
|
|
|
// Create Bubble Tea program
|
|
|
|
|
p := tea.NewProgram(m, tea.WithAltScreen())
|
|
|
|
|
|
2026-01-16 19:50:44 +01:00
|
|
|
// Pass program reference to model for event handlers
|
|
|
|
|
m.SetProgram(p)
|
|
|
|
|
|
2026-01-16 16:02:17 +01:00
|
|
|
// Set up log capture
|
|
|
|
|
r, w, _ := os.Pipe()
|
|
|
|
|
if debugFile != nil {
|
|
|
|
|
log.SetOutput(io.MultiWriter(w, debugFile))
|
|
|
|
|
} else {
|
|
|
|
|
log.SetOutput(w)
|
|
|
|
|
}
|
|
|
|
|
// Make sure logs have timestamp removed (TUI adds it if needed, or we keep it)
|
|
|
|
|
log.SetFlags(log.Ltime) // Just time
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
for {
|
|
|
|
|
n, err := r.Read(buf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if n > 0 {
|
|
|
|
|
lines := string(buf[:n])
|
|
|
|
|
// Split by newline and send each line
|
|
|
|
|
// Simple split, might need better buffering for partial lines but OK for debug
|
|
|
|
|
p.Send(logMsg(lines))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2026-01-16 14:41:26 +01:00
|
|
|
// Run
|
|
|
|
|
if _, err := p.Run(); err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|