diff options
| author | adamdottv <[email protected]> | 2025-05-15 12:04:15 -0500 |
|---|---|---|
| committer | adamdottv <[email protected]> | 2025-05-15 12:04:15 -0500 |
| commit | ddbb217d0d9f707f22610433664e15362cf98202 (patch) | |
| tree | a41b1e3a4c855692890ec9778a0a594568341345 /internal/status | |
| parent | ab150be7c393a91d9fb2347b7012ad2f7d77700b (diff) | |
| download | opencode-ddbb217d0d9f707f22610433664e15362cf98202.tar.gz opencode-ddbb217d0d9f707f22610433664e15362cf98202.zip | |
feat: better status bar
Diffstat (limited to 'internal/status')
| -rw-r--r-- | internal/status/status.go | 73 |
1 files changed, 49 insertions, 24 deletions
diff --git a/internal/status/status.go b/internal/status/status.go index 26492a76a..3648a64ae 100644 --- a/internal/status/status.go +++ b/internal/status/status.go @@ -20,9 +20,28 @@ const ( ) type StatusMessage struct { - Level Level `json:"level"` - Message string `json:"message"` - Timestamp time.Time `json:"timestamp"` + Level Level `json:"level"` + Message string `json:"message"` + Timestamp time.Time `json:"timestamp"` + Critical bool `json:"critical"` + Duration time.Duration `json:"duration"` +} + +// StatusOption is a function that configures a status message +type StatusOption func(*StatusMessage) + +// WithCritical marks a status message as critical, causing it to be displayed immediately +func WithCritical(critical bool) StatusOption { + return func(msg *StatusMessage) { + msg.Critical = critical + } +} + +// WithDuration sets a custom display duration for a status message +func WithDuration(duration time.Duration) StatusOption { + return func(msg *StatusMessage) { + msg.Duration = duration + } } const ( @@ -32,10 +51,10 @@ const ( type Service interface { pubsub.Subscriber[StatusMessage] - Info(message string) - Warn(message string) - Error(message string) - Debug(message string) + Info(message string, opts ...StatusOption) + Warn(message string, opts ...StatusOption) + Error(message string, opts ...StatusOption) + Debug(message string, opts ...StatusOption) } type service struct { @@ -63,32 +82,38 @@ func GetService() Service { return globalStatusService } -func (s *service) Info(message string) { - s.publish(LevelInfo, message) +func (s *service) Info(message string, opts ...StatusOption) { + s.publish(LevelInfo, message, opts...) slog.Info(message) } -func (s *service) Warn(message string) { - s.publish(LevelWarn, message) +func (s *service) Warn(message string, opts ...StatusOption) { + s.publish(LevelWarn, message, opts...) slog.Warn(message) } -func (s *service) Error(message string) { - s.publish(LevelError, message) +func (s *service) Error(message string, opts ...StatusOption) { + s.publish(LevelError, message, opts...) slog.Error(message) } -func (s *service) Debug(message string) { - s.publish(LevelDebug, message) +func (s *service) Debug(message string, opts ...StatusOption) { + s.publish(LevelDebug, message, opts...) slog.Debug(message) } -func (s *service) publish(level Level, messageText string) { +func (s *service) publish(level Level, messageText string, opts ...StatusOption) { statusMsg := StatusMessage{ Level: level, Message: messageText, Timestamp: time.Now(), } + + // Apply all options + for _, opt := range opts { + opt(&statusMsg) + } + s.broker.Publish(EventStatusPublished, statusMsg) } @@ -96,20 +121,20 @@ func (s *service) Subscribe(ctx context.Context) <-chan pubsub.Event[StatusMessa return s.broker.Subscribe(ctx) } -func Info(message string) { - GetService().Info(message) +func Info(message string, opts ...StatusOption) { + GetService().Info(message, opts...) } -func Warn(message string) { - GetService().Warn(message) +func Warn(message string, opts ...StatusOption) { + GetService().Warn(message, opts...) } -func Error(message string) { - GetService().Error(message) +func Error(message string, opts ...StatusOption) { + GetService().Error(message, opts...) } -func Debug(message string) { - GetService().Debug(message) +func Debug(message string, opts ...StatusOption) { + GetService().Debug(message, opts...) } func Subscribe(ctx context.Context) <-chan pubsub.Event[StatusMessage] { |
