{"modulePath":"github.com/panyam/devloop","version":"v0.1.4","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package utils\n\nPackage utils provides common utilities for devloop.\n\nThis package contains shared functionality used across the devloop ecosystem: - Logging infrastructure with colored prefix support - Color management for terminal output - Log management and streaming capabilities - Prefix writers for enhanced output formatting\n\n### Logging {#hdr-Logging}\n\nThe package provides structured logging with prefix support:\n\n\tlogger := utils.NewDevloopLogger(true, 10, colorManager)\n\tlogger.LogWithPrefix(\"build\", \"Starting build process\")\n\t// Output: [build     ] Starting build process\n\n### Color Management {#hdr-Color_Management}\n\nColor management provides consistent terminal coloring:\n\n\tcolorManager := utils.NewColorManager(true)\n\tcoloredText := colorManager.FormatPrefix(\"[build]\", rule)\n\n### Log Streaming {#hdr-Log_Streaming}\n\nLog streaming enables real-time log access:\n\n\tlogManager := utils.NewLogManager(logsDir)\n\terr := logManager.StreamLogs(\"ruleName\", \"filter\", stream)\n\n### Prefix Writers {#hdr-Prefix_Writers}\n\nEnhanced output writers with prefix and color support:\n\n\twriter := utils.NewColoredPrefixWriter(writers, \"[prefix] \", colorManager, rule)\n\twriter.Write([]byte(\"Hello World\"))\n\t// Output: [prefix] Hello World (with colors)\n\n## Functions\n\n```go\nfunc InitGlobalLogger(prefixLogs bool, prefixMaxLength int, colorManager ColorFormatter)\n```\nInitGlobalLogger initializes the global logger with configuration\n\n```go\nfunc LogDevloop(format string, args ...any)\n```\nLogDevloop logs a devloop message using the global logger\n\n```go\nfunc LogGateway(format string, args ...any)\n```\nLogGateway logs a gateway message using the global logger\n\n```go\nfunc LogMCP(format string, args ...any)\n```\nLogMCP logs an MCP message using the global logger\n\n```go\nfunc StripColorCodes(input string) string\n```\nStripColorCodes removes ANSI color codes from a string using regex\n\n## Types\n\n```go\ntype ColorFormatter interface {\n\tIsEnabled() bool\n\tFormatPrefix(prefix string, rule any) string\n}\n```\nColorFormatter interface for formatting colored prefixes\n\n```go\ntype ColorManager struct {\n\t// contains filtered or unexported fields\n}\n```\nColorManager handles color assignment and formatting for rule output\n\n```go\nfunc NewColorManager(settings ColorSettings) *ColorManager\n```\nNewColorManager creates a new color manager with the given settings\n\n```go\nfunc (cm *ColorManager) DisableColors()\n```\nDisableColors explicitly disables color output for this ColorManager only\n\n```go\nfunc (cm *ColorManager) FormatPrefix(prefix string, rule interface{}) string\n```\nFormatPrefix applies color formatting to a prefix string\n\n```go\nfunc (cm *ColorManager) GetColorForRule(rule ColorRule) *color.Color\n```\nGetColorForRule returns the appropriate color for a given rule\n\n```go\nfunc (cm *ColorManager) GetColoredString(text string, rule ColorRule) string\n```\nGetColoredString returns a colored version of the input string for the given rule\n\n```go\nfunc (cm *ColorManager) IsEnabled() bool\n```\nIsEnabled returns whether color formatting is enabled\n\n```go\ntype ColorRule interface {\n\tGetName() string\n\tGetColor() string\n\tGetPrefix() string\n}\n```\nColorRule represents a rule that can have colors applied to it\n\n```go\ntype ColorSettings interface {\n\tGetColorLogs() bool\n\tGetColorScheme() string\n\tGetCustomColors() map[string]string\n}\n```\nColorSettings represents the color configuration needed by ColorManager\n\n```go\ntype ColoredPrefixWriter struct {\n\t// contains filtered or unexported fields\n}\n```\nColoredPrefixWriter is an enhanced io.Writer that adds colored prefixes to each line of output It separates terminal output (with colors) from file output (without colors)\n\n```go\nfunc NewColoredPrefixWriter(writers []io.Writer, prefix string, colorManager *ColorManager, rule Nameable) *ColoredPrefixWriter\n```\nNewColoredPrefixWriter creates a new ColoredPrefixWriter\n\n```go\nfunc (cpw *ColoredPrefixWriter) UpdatePrefix(newPrefix string)\n```\nUpdatePrefix updates the prefix and regenerates the colored version\n\n```go\nfunc (cpw *ColoredPrefixWriter) Write(p []byte) (n int, err error)\n```\nWrite implements the io.Writer interface with color support\n\n```go\nfunc (cpw *ColoredPrefixWriter) WriteToFileOnly(p []byte) (n int, err error)\n```\nWriteToFileOnly writes output only to file writers (without colors)\n\n```go\nfunc (cpw *ColoredPrefixWriter) WriteToTerminalOnly(p []byte) (n int, err error)\n```\nWriteToTerminalOnly writes output only to terminal writers (with colors)\n\n```go\ntype DevloopLogger struct {\n\t// contains filtered or unexported fields\n}\n```\nDevloopLogger provides consistent logging across all components\n\n```go\nfunc NewDevloopLogger(prefixLogs bool, prefixMaxLength int, colorManager ColorFormatter) *DevloopLogger\n```\nNewDevloopLogger creates a new logger with the given configuration\n\n```go\nfunc (dl *DevloopLogger) LogWithPrefix(prefix, format string, args ...any)\n```\nLogWithPrefix logs a message with the specified prefix (e.g., \"devloop\", \"gateway\", \"mcp\")\n\n```go\ntype FileLogSource struct {\n\t// contains filtered or unexported fields\n}\n```\nFileLogSource reads new lines from a log file, detecting truncation.\n\n```go\nfunc NewFileLogSource(path string) *FileLogSource\n```\nNewFileLogSource creates a FileLogSource for the given path. The file is opened lazily on the first ReadLines call.\n\n```go\nfunc (fs *FileLogSource) Close() error\n```\nClose releases the file handle.\n\n```go\nfunc (fs *FileLogSource) ReadLines(ctx context.Context) ([]string, error)\n```\nReadLines returns new complete lines since the last call. Returns nil, nil if the file doesn't exist yet or has no new content.\n\n```go\nfunc (fs *FileLogSource) Reset() error\n```\nReset re-opens the file from the beginning.\n\n```go\ntype LogBroadcaster struct {\n\t// contains filtered or unexported fields\n}\n```\nLogBroadcaster reads new lines from a LogSource once and fans out to all subscribers. One broadcaster per rule eliminates redundant file polling.\n\n```go\nfunc NewLogBroadcaster(ruleName string, source LogSource) *LogBroadcaster\n```\nNewLogBroadcaster creates and starts a broadcaster for the given rule.\n\n```go\nfunc (b *LogBroadcaster) BroadcastEvent(event *pb.LogEvent)\n```\nBroadcastEvent sends a LogEvent to all subscribers (non-blocking).\n\n```go\nfunc (b *LogBroadcaster) GetHistory(n int) []string\n```\nGetHistory returns the last n lines from the ring buffer.\n\n```go\nfunc (b *LogBroadcaster) IsFinished() bool\n```\nIsFinished returns whether the rule has finished.\n\n```go\nfunc (b *LogBroadcaster) SignalFinished()\n```\nSignalFinished marks the rule as finished. The poller will do a final drain and then close all subscriber Done channels.\n\n```go\nfunc (b *LogBroadcaster) SignalNewRun(appendMode bool)\n```\nSignalNewRun prepares the broadcaster for a new rule run. In truncate mode (appendMode=false): clears the ring buffer, resets the source to offset 0, and clears the finished flag. In append mode (appendMode=true): only clears the finished flag so the source continues from its current offset (picking up the separator + new output) and the ring buffer preserves history across runs.\n\n```go\nfunc (b *LogBroadcaster) Stop()\n```\nStop cancels the poller goroutine, closes all subscriber Done channels, and closes the source.\n\n```go\nfunc (b *LogBroadcaster) Subscribe(id, filter string) *Subscriber\n```\nSubscribe adds a subscriber that will receive new log lines. Returns the subscriber. The caller should select on sub.Ch and sub.Done.\n\n```go\nfunc (b *LogBroadcaster) Unsubscribe(id string)\n```\nUnsubscribe removes a subscriber by ID.\n\n```go\ntype LogManager struct {\n\t// contains filtered or unexported fields\n}\n```\nLogManager manages log files and provides streaming capabilities. It uses a single LogBroadcaster per rule to fan out log lines to all subscribers instead of each client polling independently.\n\n```go\nfunc NewLogManager(logDir string) (*LogManager, error)\n```\nNewLogManager creates a new LogManager instance.\n\n```go\nfunc (lm *LogManager) Close() error\n```\nClose closes the LogManager and stops all broadcasters.\n\n```go\nfunc (lm *LogManager) GetWriter(ruleName string, appendOnRestart bool) (io.Writer, error)\n```\nGetWriter returns an io.Writer for a specific rule's log file. When appendOnRestart is true, the log file is opened in append mode and a separator line is written to mark the start of a new run. When false (default), the file is truncated on each run.\n\n```go\nfunc (lm *LogManager) SignalFinished(ruleName string, success bool, errMsg string)\n```\nSignalFinished signals that a rule's execution has finished. success indicates whether the run completed successfully. errMsg is an optional error message when success is false.\n\n```go\nfunc (lm *LogManager) StreamLogs(ruleName, filter string, timeoutSeconds int64, lastNLines int32, writer *gocurrent.Writer[*pb.StreamLogsResponse]) error\n```\nStreamLogs streams logs for a given rule to the provided gocurrent Writer. lastNLines controls history replay: 0 = no history, negative = all available.\n\n```go\ntype LogSource interface {\n\t// ReadLines returns new lines since the last call. Returns nil, nil if no new content or file doesn't exist yet.\n\tReadLines(ctx context.Context) ([]string, error)\n\t// Reset seeks back to the beginning (e.g. after file truncation).\n\tReset() error\n\t// Close releases the underlying resource.\n\tClose() error\n}\n```\nLogSource provides an interface for reading new log lines incrementally.\n\n```go\ntype LoggerConfig struct {\n\tPrefixLogs      bool\n\tPrefixMaxLength int\n\tColorManager    ColorFormatter\n}\n```\nLoggerConfig holds configuration for consistent logging\n\n```go\ntype Nameable interface {\n\tGetName() string\n}\n```\nNameable represents anything that has a name (used for color generation)\n\n```go\ntype RingBuffer struct {\n\t// contains filtered or unexported fields\n}\n```\nRingBuffer is a thread-safe circular buffer of strings. We use a simple slice-based approach instead of container/ring because:\n\n  - container/ring is a linked list without fixed-capacity overwrite semantics\n  - No built-in \"read last N\" — would require O(n) linked-list traversal\n  - Per-element heap allocations vs a single contiguous slice here\n  - Uses any/interface{} — loses type safety\n\nTODO: If we need a generic version, consider making this RingBuffer\\[T any].\n\n```go\nfunc NewRingBuffer(capacity int) *RingBuffer\n```\nNewRingBuffer creates a new RingBuffer with the given capacity.\n\n```go\nfunc (rb *RingBuffer) Clear()\n```\nClear resets the buffer to empty.\n\n```go\nfunc (rb *RingBuffer) Len() int\n```\nLen returns the number of lines currently in the buffer.\n\n```go\nfunc (rb *RingBuffer) ReadLast(n int) []string\n```\nReadLast returns the last n lines from the buffer. If n \\\u003c 0, returns all available lines. If n \u003e available, returns all available lines.\n\n```go\nfunc (rb *RingBuffer) Write(lines []string)\n```\nWrite appends lines to the ring buffer, overwriting oldest entries if full.\n\n```go\ntype SimpleColorRule struct {\n\t// contains filtered or unexported fields\n}\n```\nSimpleColorRule is a basic implementation of ColorRule for simple cases\n\n```go\nfunc (r *SimpleColorRule) GetColor() string\n```\n\n```go\nfunc (r *SimpleColorRule) GetName() string\n```\n\n```go\nfunc (r *SimpleColorRule) GetPrefix() string\n```\n\n```go\ntype Subscriber struct {\n\tID     string\n\tFilter string\n\tCh     chan []*pb.LogLine // buffered, batched delivery\n\tEvents chan *pb.LogEvent  // buffered, event delivery\n\tDone   chan struct{}      // closed when broadcaster stops or rule finishes\n}\n```\nSubscriber represents a client subscribed to log broadcasts.\n\n","path":"github.com/panyam/devloop/utils","name":"utils","synopsis":"Package utils provides common utilities for devloop.","isRedistributable":true}
