-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (75 loc) · 2.33 KB
/
main.go
File metadata and controls
89 lines (75 loc) · 2.33 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"fmt"
"image/color"
"os"
"charm.land/lipgloss/v2"
"github.com/charmbracelet/fang"
"github.com/unfunco/t/internal/cmd"
"github.com/unfunco/t/internal/config"
"github.com/unfunco/t/internal/theme"
)
func main() {
configPath := ""
if path, err := config.Path(); err == nil {
configPath = path
}
cfg := config.Config{
Theme: theme.DefaultConfig(),
}
if loadedCfg, err := config.Load(); err != nil {
logConfigWarning(configPath, err)
} else {
cfg = loadedCfg
}
hasDarkBackground := lipgloss.HasDarkBackground(os.Stdin, os.Stdout)
th, err := theme.FromConfig(cfg.Theme, hasDarkBackground)
if err != nil {
logThemeWarning(configPath, err)
th = theme.MustFromConfig(theme.DefaultConfig(), hasDarkBackground)
}
if err := fang.Execute(
context.Background(),
cmd.NewDefaultTCommandWithTheme(th),
fang.WithColorSchemeFunc(func(c lipgloss.LightDarkFunc) fang.ColorScheme {
return customColorScheme(c, th)
}),
fang.WithNotifySignal(os.Interrupt, os.Kill),
); err != nil {
os.Exit(1)
}
}
func logConfigWarning(configPath string, err error) {
if configPath == "" {
_, _ = fmt.Fprintf(os.Stderr, "warning: failed to load config: %v; using defaults\n", err)
return
}
_, _ = fmt.Fprintf(os.Stderr, "warning: failed to load config at %s: %v; using defaults\n", configPath, err)
}
func logThemeWarning(configPath string, err error) {
if configPath == "" {
_, _ = fmt.Fprintf(os.Stderr, "warning: invalid theme configuration: %v; using default theme\n", err)
return
}
_, _ = fmt.Fprintf(os.Stderr, "warning: invalid theme configuration in %s: %v; using default theme\n", configPath, err)
}
func customColorScheme(c lipgloss.LightDarkFunc, th theme.Theme) fang.ColorScheme {
scheme := fang.AnsiColorScheme(c)
scheme.Base = th.Text.RGBA()
scheme.Description = th.Muted.RGBA()
scheme.Comment = th.Muted.RGBA()
scheme.Flag = th.Highlight.RGBA()
scheme.FlagDefault = th.Muted.RGBA()
scheme.Command = th.Highlight.RGBA()
scheme.Program = th.Highlight.RGBA()
scheme.QuotedString = th.Success.RGBA()
scheme.Argument = th.Text.RGBA()
scheme.DimmedArgument = th.Muted.RGBA()
scheme.Help = th.Muted.RGBA()
scheme.Dash = th.Text.RGBA()
scheme.Title = th.Highlight.RGBA()
scheme.ErrorHeader = [2]color.Color{th.Text.RGBA(), th.Worry.RGBA()}
scheme.ErrorDetails = th.Worry.RGBA()
return scheme
}