summaryrefslogtreecommitdiff
path: root/main.go
blob: 4f61c75b2036e84fe1c4f63c446f0f217019c968 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main

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"
)

func AppAction(ctx *cli.Context) error {
	logFile, err := os.Create(ctx.String(flagLogfile))
	if err != nil {
		slog.Error("Failed to create log file", util.ErrKey, err.Error())
		os.Exit(1)
	}
	defer logFile.Close()

	handler := slog.NewJSONHandler(logFile, nil)
	slog.SetDefault(slog.New(handler))

	slog.Info("Starting Termpicker")

	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
	}
	return nil
}

func main() {
	app := &cli.App{
		Name:   "Termpicker",
		Usage:  "A terminal-based color picker",
		Action: AppAction,
		Flags:  AppFlags,
	}
	if err := app.Run(os.Args); err != nil {
		slog.Error("Program crashed", util.ErrKey, err.Error())
		os.Exit(1)
	}
}