-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinge.go
More file actions
182 lines (150 loc) · 4.21 KB
/
tinge.go
File metadata and controls
182 lines (150 loc) · 4.21 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2025 The Tinge Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package tinge
import (
"io"
"os"
"strings"
"github.com/charmbracelet/lipgloss"
)
type TextStyle = lipgloss.Style
type StyledText struct {
parts []string
currLine string
indent int
pendingNewlines int
}
func Styled() *StyledText {
return &StyledText{}
}
func (s *StyledText) With(styles ...TextStyle) *StyledTextBuilder {
combined := lipgloss.NewStyle()
for _, style := range styles {
combined = combined.Inherit(style)
}
return &StyledTextBuilder{parent: s, style: combined}
}
func (s *StyledText) Space(n ...int) *StyledText {
s.ensureLineStart()
if len(n) > 0 {
s.currLine += strings.Repeat(" ", n[0])
} else {
s.currLine += " "
}
return s
}
func (s *StyledText) Newline() *StyledText {
if s.currLine != "" {
s.parts = append(s.parts, s.currLine)
s.currLine = ""
}
s.pendingNewlines++
return s
}
func (s *StyledText) Indent(spaces int) *StyledText {
s.indent = spaces
// if s.currLine == "" {
// s.currLine = strings.Repeat(" ", spaces)
// }
return s
}
func (s *StyledText) ensureLineStart() {
if s.pendingNewlines > 0 {
if s.currLine != "" {
s.parts = append(s.parts, s.currLine)
s.currLine = ""
}
// add newline
for i := 0; i < s.pendingNewlines; i++ {
s.parts = append(s.parts, "")
}
s.pendingNewlines = 0
s.currLine = strings.Repeat(" ", s.indent)
}
}
func (s *StyledText) Grey(text string) *StyledText {
return s.With(Grey).Text(text)
}
func (s *StyledText) GreyDark(text string) *StyledText {
return s.With(GreyDark).Text(text)
}
func (s *StyledText) Red(text string) *StyledText {
return s.With(Red).Text(text)
}
func (s *StyledText) Green(text string) *StyledText {
return s.With(Green).Text(text)
}
func (s *StyledText) GreenLight(text string) *StyledText {
return s.With(GreenLight).Text(text)
}
func (s *StyledText) GreenDark(text string) *StyledText {
return s.With(GreenDark).Text(text)
}
func (s *StyledText) Pink(text string) *StyledText {
return s.With(Pink).Text(text)
}
func (s *StyledText) Yellow(text string) *StyledText {
return s.With(Yellow).Text(text)
}
func (s *StyledText) Blue(text string) *StyledText {
return s.With(Blue).Text(text)
}
func (s *StyledText) BlueDark(text string) *StyledText {
return s.With(BlueDark).Text(text)
}
func (s *StyledText) Bold(text string) *StyledText {
return s.With(Bold).Text(text)
}
func (s *StyledText) Italic(text string) *StyledText {
return s.With(Italic).Text(text)
}
func (s *StyledText) BoldItalic(text string) *StyledText {
return s.With(Bold, Italic).Text(text)
}
func (s *StyledText) Text(text string) *StyledText {
s.ensureLineStart()
return s.With().Text(text)
}
func (s *StyledText) String() string {
s.ensureLineStart()
if s.currLine != "" {
s.parts = append(s.parts, s.currLine)
s.currLine = ""
}
return strings.Join(s.parts, "\n")
}
func (s *StyledText) Write(output ...io.Writer) {
out := defaultOutput
if len(output) > 0 {
out = output[0]
}
out.Write([]byte(s.String()))
}
type StyledTextBuilder struct {
parent *StyledText
style lipgloss.Style
}
func (b *StyledTextBuilder) Text(content string) *StyledText {
b.parent.ensureLineStart()
b.parent.currLine += b.style.Render(content)
return b.parent
}
var (
Grey = lipgloss.NewStyle().Foreground(lipgloss.Color("#909194"))
GreyDark = lipgloss.NewStyle().Foreground(lipgloss.Color("#454e6d"))
Green = lipgloss.NewStyle().Foreground(lipgloss.Color("#50FA7B"))
GreenLight = lipgloss.NewStyle().Foreground(lipgloss.Color("#3fed7b"))
GreenDark = lipgloss.NewStyle().Foreground(lipgloss.Color("#3C9258"))
Red = lipgloss.NewStyle().Foreground(lipgloss.Color("#ff5555"))
Pink = lipgloss.NewStyle().Foreground(lipgloss.Color("#ff79c6"))
Yellow = lipgloss.NewStyle().Foreground(lipgloss.Color("#f1fa8c"))
Blue = lipgloss.NewStyle().Foreground(lipgloss.Color("#a4ffff"))
BlueDark = lipgloss.NewStyle().Foreground(lipgloss.Color("#8be9fd"))
Bold = lipgloss.NewStyle().Bold(true)
Italic = lipgloss.NewStyle().Italic(true)
)
var defaultOutput io.Writer = os.Stdout
func SetWriter(output io.Writer) {
defaultOutput = output
}