~adnano/astronaut

ref: 5b2ba7d7bffd153f57a7eac22f172a5d4d56c6a9 astronaut/styles/styles.go -rw-r--r-- 2.8 KiB
5b2ba7d7Adnan Maolood ui/view: Remove focus variable 7 months ago
                                                                                
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package styles

import (
	"fmt"

	"git.sr.ht/~emersion/go-scfg"
	"github.com/gdamore/tcell/v2"
)

var (
	Default  tcell.Style
	Heading1 tcell.Style
	Heading2 tcell.Style
	Heading3 tcell.Style
	Link     tcell.Style
	ListItem tcell.Style
	Pre      tcell.Style
	Quote    tcell.Style
	Text     tcell.Style
	Error    tcell.Style
	Warning  tcell.Style
)

func init() {
	SetDefault(tcell.StyleDefault)
}

// Set default style.
func SetDefault(style tcell.Style) {
	Default = style
	Heading1 = style.Bold(true)
	Heading2 = style.Bold(true)
	Heading3 = style.Bold(true)
	Link = style.Underline(true)
	ListItem = style
	Pre = style
	Quote = style.Dim(true)
	Text = style

	Error = style.
		Background(tcell.ColorRed).
		Foreground(tcell.ColorBlack).
		Bold(true)
	Warning = style.
		Background(tcell.ColorYellow).
		Foreground(tcell.ColorBlack).
		Bold(true)
}

func Status() tcell.Style {
	return Default.Reverse(true).Bold(true)
}

func Tab() tcell.Style {
	return Default
}

/*
Styles inherit from the default style.
If a style is not present, its parent style will be used.
To prevent inheritance, use the reset directive:

style {
	reset
}

The reset directive should be the first directive in the style block,
otherwise all previous styles will be overidden.
*/
func Parse(block scfg.Block) error {
	for _, d := range block {
		var err error
		switch d.Name {
		case "default":
			var default_ tcell.Style
			default_, err = parseStyle(d.Children, tcell.StyleDefault)
			SetDefault(default_)
		case "h1":
			Heading1, err = parseStyle(d.Children, Heading1)
		case "h2":
			Heading2, err = parseStyle(d.Children, Heading2)
		case "h3":
			Heading3, err = parseStyle(d.Children, Heading3)
		case "link":
			Link, err = parseStyle(d.Children, Link)
		case "li":
			ListItem, err = parseStyle(d.Children, ListItem)
		case "pre":
			Pre, err = parseStyle(d.Children, Pre)
		case "quote":
			Quote, err = parseStyle(d.Children, Quote)
		case "text":
			Text, err = parseStyle(d.Children, Text)
		}
		if err != nil {
			return fmt.Errorf("failed to parse style: directive %q: %w", d.Name, err)
		}
	}
	return nil
}

func parseStyle(block scfg.Block, style tcell.Style) (tcell.Style, error) {
	for _, d := range block {
		switch d.Name {
		case "background":
			var color string
			if err := d.ParseParams(&color); err != nil {
				return tcell.StyleDefault, err
			}
			style = style.Background(tcell.GetColor(color))
		case "foreground":
			var color string
			if err := d.ParseParams(&color); err != nil {
				return tcell.StyleDefault, err
			}
			style = style.Foreground(tcell.GetColor(color))
		case "bold":
			style = style.Bold(true)
		case "italic":
			style = style.Italic(true)
		case "underline":
			style = style.Underline(true)
		case "reverse":
			style = style.Reverse(true)
		case "dim":
			style = style.Dim(true)
		case "reset":
			style = Default
		}
	}
	return style, nil
}