From 821c198fe6700a9e9e9eb07ab6922e7bde7f2bfb Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Mon, 25 Nov 2024 16:45:33 -0500 Subject: Accept initial color using -c flag (#14) --- main.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 241adcf..4f61c75 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,10 @@ import ( "log/slog" "os" + "github.com/ChausseBenjamin/termpicker/internal/colors" "github.com/ChausseBenjamin/termpicker/internal/picker" "github.com/ChausseBenjamin/termpicker/internal/switcher" + "github.com/ChausseBenjamin/termpicker/internal/userinput" "github.com/ChausseBenjamin/termpicker/internal/util" tea "github.com/charmbracelet/bubbletea" "github.com/urfave/cli/v2" @@ -23,10 +25,34 @@ func AppAction(ctx *cli.Context) error { slog.SetDefault(slog.New(handler)) slog.Info("Starting Termpicker") - rgb := picker.RGB() - cmyk := picker.CMYK() - hsl := picker.HSL() - sw := switcher.New([]picker.Model{*rgb, *cmyk, *hsl}) + + sw := switcher.New([]picker.Model{ + *picker.RGB(), + *picker.CMYK(), + *picker.HSL(), + }) + + if colorStr := ctx.String("color"); colorStr != "" { + color, err := userinput.ParseColor(colorStr) + if err != nil { + slog.Error("Failed to parse color", util.ErrKey, err) + sw.NewNotice(err.Error()) + } else { + pc := color.ToPrecise() + switch color.(type) { + case colors.RGB: + sw.UpdatePicker(0, pc) + sw.SetActive(0) + case colors.CMYK: + sw.UpdatePicker(1, pc) + sw.SetActive(1) + case colors.HSL: + sw.UpdatePicker(2, pc) + sw.SetActive(2) + } + } + } + p := tea.NewProgram(sw) if _, err := p.Run(); err != nil { return err @@ -36,7 +62,7 @@ func AppAction(ctx *cli.Context) error { func main() { app := &cli.App{ - Name: "TermPicker", + Name: "Termpicker", Usage: "A terminal-based color picker", Action: AppAction, Flags: AppFlags, -- cgit v1.2.3