From 03885140e3ddb94f559e2aba703622e2d989efaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96rjan=20Fors?= Date: Sat, 11 May 2024 01:59:56 +0200 Subject: [PATCH] feat: return error --- README.md | 10 ++++++++-- redlog.go | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1894af0..c35515f 100644 --- a/README.md +++ b/README.md @@ -68,13 +68,19 @@ slog.SetDefault(slog.New(log.Default())) It is possible to request a specific theme. ```go -log.SetStyles(redlog.Theme("catppuccin")) +styles, _ := redlog.Theme("catppuccin") +logger := log.New(os.Stderr) +logger.SetStyles(styles) ``` And with a specific variation of the theme. ```go -log.SetStyles(redlog.Theme("catppuccin", redlog.WithVariant("mocha"))) +styles, err := redlog.Theme("catppuccin", redlog.WithVariant("mocha")) +if err != nil { + log.Fatal(err) +} +log.SetStyles(styles) ``` ## Specific theme diff --git a/redlog.go b/redlog.go index ff7ade5..622f92b 100644 --- a/redlog.go +++ b/redlog.go @@ -1,10 +1,17 @@ package redlog import ( + "errors" + "github.com/charmbracelet/log" "github.com/op/redlog/internal/themes" ) +var ( + ErrUnknownTheme = errors.New("unknown theme") + ErrUnknownVariant = errors.New("unknown variant") +) + // Default is the default theme style. var Default = themes.Default.Default.Styles @@ -15,9 +22,9 @@ func WithVariant(name string) func(*option) { // Theme returns the most suitable theme given the provided arguments. // -// This function will fallback to the default theme or the found themes -// variant, if the requested was not found. -func Theme(name string, opts ...Option) *log.Styles { +// This function will always return a usable style, together with an error if +// the requested theme or variant was not found. +func Theme(name string, opts ...Option) (*log.Styles, error) { o := option{name: name} for _, opt := range opts { opt(&o) @@ -25,13 +32,13 @@ func Theme(name string, opts ...Option) *log.Styles { t, ok := themes.ByName(o.name) if !ok { - return Default + return Default, ErrUnknownTheme } v, ok := themes.VariantByName(t, o.variant) if !ok { - return t.Default.Styles + return t.Default.Styles, ErrUnknownVariant } - return v.Styles + return v.Styles, nil } type Option func(*option)