11package cliui
22
33import (
4+ "os"
5+
46 "github.com/charmbracelet/charm/ui/common"
57 "github.com/charmbracelet/lipgloss"
8+ "github.com/muesli/termenv"
69 "golang.org/x/xerrors"
710)
811
9- var (
10- Canceled = xerrors .New ("canceled" )
11-
12- defaultStyles = common .DefaultStyles ()
13- )
12+ var Canceled = xerrors .New ("canceled" )
1413
15- // ValidateNotEmpty is a helper function to disallow empty inputs!
16- func ValidateNotEmpty (s string ) error {
17- if s == "" {
18- return xerrors .New ("Must be provided!" )
19- }
20- return nil
21- }
14+ // DefaultStyles compose visual elements of the UI.
15+ var DefaultStyles Styles
2216
23- // Styles compose visual elements of the UI!
24- var Styles = struct {
17+ type Styles struct {
2518 Bold ,
2619 Checkmark ,
2720 Code ,
@@ -38,23 +31,45 @@ var Styles = struct {
3831 Logo ,
3932 Warn ,
4033 Wrap lipgloss.Style
41- }{
42- Bold : lipgloss .NewStyle ().Bold (true ),
43- Checkmark : defaultStyles .Checkmark ,
44- Code : defaultStyles .Code ,
45- Crossmark : defaultStyles .Error .Copy ().SetString ("✘" ),
46- DateTimeStamp : defaultStyles .LabelDim ,
47- Error : defaultStyles .Error ,
48- Field : defaultStyles .Code .Copy ().Foreground (lipgloss.AdaptiveColor {Light : "#000000" , Dark : "#FFFFFF" }),
49- Keyword : defaultStyles .Keyword ,
50- Paragraph : defaultStyles .Paragraph ,
51- Placeholder : lipgloss .NewStyle ().Foreground (lipgloss.AdaptiveColor {Light : "#585858" , Dark : "#4d46b3" }),
52- Prompt : defaultStyles .Prompt .Copy ().Foreground (lipgloss.AdaptiveColor {Light : "#9B9B9B" , Dark : "#5C5C5C" }),
53- FocusedPrompt : defaultStyles .FocusedPrompt .Copy ().Foreground (lipgloss .Color ("#651fff" )),
54- Fuchsia : defaultStyles .SelectedMenuItem .Copy (),
55- Logo : defaultStyles .Logo .Copy ().SetString ("Coder" ),
56- Warn : lipgloss .NewStyle ().Foreground (
57- lipgloss.AdaptiveColor {Light : "#04B575" , Dark : "#ECFD65" },
58- ),
59- Wrap : lipgloss .NewStyle ().Width (80 ),
34+ }
35+
36+ func init () {
37+ lipgloss .SetDefaultRenderer (
38+ lipgloss .NewRenderer (os .Stdout , termenv .WithColorCache (true )),
39+ )
40+
41+ // All Styles are set after we change the DefaultRenderer so that the ColorCache
42+ // is in effect, mitigating the severe performance issue seen here:
43+ // https://github.com/coder/coder/issues/7884.
44+
45+ charmStyles := common .DefaultStyles ()
46+
47+ DefaultStyles = Styles {
48+ Bold : lipgloss .NewStyle ().Bold (true ),
49+ Checkmark : charmStyles .Checkmark ,
50+ Code : charmStyles .Code ,
51+ Crossmark : charmStyles .Error .Copy ().SetString ("✘" ),
52+ DateTimeStamp : charmStyles .LabelDim ,
53+ Error : charmStyles .Error ,
54+ Field : charmStyles .Code .Copy ().Foreground (lipgloss.AdaptiveColor {Light : "#000000" , Dark : "#FFFFFF" }),
55+ Keyword : charmStyles .Keyword ,
56+ Paragraph : charmStyles .Paragraph ,
57+ Placeholder : lipgloss .NewStyle ().Foreground (lipgloss.AdaptiveColor {Light : "#585858" , Dark : "#4d46b3" }),
58+ Prompt : charmStyles .Prompt .Copy ().Foreground (lipgloss.AdaptiveColor {Light : "#9B9B9B" , Dark : "#5C5C5C" }),
59+ FocusedPrompt : charmStyles .FocusedPrompt .Copy ().Foreground (lipgloss .Color ("#651fff" )),
60+ Fuchsia : charmStyles .SelectedMenuItem .Copy (),
61+ Logo : charmStyles .Logo .Copy ().SetString ("Coder" ),
62+ Warn : lipgloss .NewStyle ().Foreground (
63+ lipgloss.AdaptiveColor {Light : "#04B575" , Dark : "#ECFD65" },
64+ ),
65+ Wrap : lipgloss .NewStyle ().Width (80 ),
66+ }
67+ }
68+
69+ // ValidateNotEmpty is a helper function to disallow empty inputs!
70+ func ValidateNotEmpty (s string ) error {
71+ if s == "" {
72+ return xerrors .New ("Must be provided!" )
73+ }
74+ return nil
6075}
0 commit comments