internal/progress/progress.go (view raw)
1package progress
2
3import (
4 "fmt"
5 "io"
6)
7
8// Logger provides logging interface for the aggregator
9type Logger interface {
10 Logf(format string, args ...any)
11 Warningf(format string, args ...any)
12 Debugf(format string, args ...any)
13}
14
15// Progress provides progress reporting interface for TUI
16type Progress interface {
17 Logger
18 SetStage(stage string)
19 SetSubStage(status string)
20 SetArticleCount(total, processed int)
21}
22
23// SimpleLogger implements Logger with io.Writer output
24type SimpleLogger struct {
25 writer io.Writer
26 debug bool
27}
28
29// NewSimpleLogger creates a new SimpleLogger
30func NewSimpleLogger(writer io.Writer, debug bool) *SimpleLogger {
31 return &SimpleLogger{
32 writer: writer,
33 debug: debug,
34 }
35}
36
37func (sl *SimpleLogger) Logf(format string, args ...any) {
38 fmt.Fprintf(sl.writer, format+"\n", args...)
39}
40
41func (sl *SimpleLogger) Warningf(format string, args ...any) {
42 fmt.Fprintf(sl.writer, "Warning: "+format+"\n", args...)
43}
44
45func (sl *SimpleLogger) Debugf(format string, args ...any) {
46 if sl.debug {
47 fmt.Fprintf(sl.writer, "Debug: "+format+"\n", args...)
48 }
49}
50
51func (sl *SimpleLogger) SetStage(stage string) {}
52func (sl *SimpleLogger) SetSubStage(stage string) {}
53func (sl *SimpleLogger) SetArticleCount(total, processed int) {}
54
55// NoopLogger implements Logger with no output
56type NoopLogger struct{}
57
58func (nl *NoopLogger) Logf(format string, args ...any) {}
59func (nl *NoopLogger) Warningf(format string, args ...any) {}
60func (nl *NoopLogger) Debugf(format string, args ...any) {}
61func (nl *NoopLogger) SetStage(stage string) {}
62func (nl *NoopLogger) SetSubStage(status string) {}
63func (nl *NoopLogger) SetArticleCount(total, processed int) {}
64
65// Context provides progress context for components
66type Context struct {
67 logger Logger
68}
69
70// NewContext creates a new progress context
71func NewContext(logger Logger) *Context {
72 return &Context{
73 logger: logger,
74 }
75}
76
77// Logf logs a message
78func (c *Context) Logf(format string, args ...any) {
79 if c.logger != nil {
80 c.logger.Logf(format, args...)
81 }
82}
83
84// Warningf logs a warning
85func (c *Context) Warningf(format string, args ...any) {
86 if c.logger != nil {
87 c.logger.Warningf(format, args...)
88 }
89}
90
91// Debugf logs a debug message
92func (c *Context) Debugf(format string, args ...any) {
93 if c.logger != nil {
94 c.logger.Debugf(format, args...)
95 }
96}