Skip to content

Commit 9068bba

Browse files
committed
Make code to highlight file re-useable
1 parent 2e92d61 commit 9068bba

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

kittens/diff/highlight.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,35 @@ func ansi_formatter(w io.Writer, style *chroma.Style, it chroma.Iterator) (err e
158158
return nil
159159
}
160160

161-
func resolved_chroma_style(use_light_colors bool) *chroma.Style {
162-
name := utils.IfElse(use_light_colors, conf.Pygments_style, conf.Dark_pygments_style)
161+
type prefer_light_colors bool
162+
163+
func (s prefer_light_colors) StyleName() string {
164+
return utils.IfElse(bool(s), conf.Pygments_style, conf.Dark_pygments_style)
165+
}
166+
167+
func (s prefer_light_colors) UseLightColors() bool { return bool(s) }
168+
169+
type StyleResolveData interface {
170+
StyleName() string
171+
UseLightColors() bool
172+
}
173+
174+
func resolved_chroma_style(srd StyleResolveData) *chroma.Style {
175+
name := srd.StyleName()
163176
var style *chroma.Style
164177
if name == "default" {
165178
style = DefaultStyle()
166179
} else {
167180
style = styles.Get(name)
168181
}
169182
if style == nil {
170-
if resolved_colors.Background.IsDark() && !resolved_colors.Foreground.IsDark() {
183+
if srd.UseLightColors() {
184+
style = DefaultStyle()
185+
} else {
171186
style = styles.Get("monokai")
172187
if style == nil {
173188
style = styles.Get("github-dark")
174189
}
175-
} else {
176-
style = DefaultStyle()
177190
}
178191
if style == nil {
179192
style = styles.Fallback
@@ -185,7 +198,7 @@ func resolved_chroma_style(use_light_colors bool) *chroma.Style {
185198
var tokens_map map[string][]chroma.Token
186199
var mu sync.Mutex
187200

188-
func highlight_file(path string, use_light_colors bool) (highlighted string, err error) {
201+
func HighlightFile(path string, srd StyleResolveData) (highlighted string, err error) {
189202
defer func() {
190203
if r := recover(); r != nil {
191204
e, ok := r.(error)
@@ -235,17 +248,18 @@ func highlight_file(path string, use_light_colors bool) (highlighted string, err
235248
formatter := chroma.FormatterFunc(ansi_formatter)
236249
w := strings.Builder{}
237250
w.Grow(len(text) * 2)
238-
err = formatter.Format(&w, resolved_chroma_style(use_light_colors), chroma.Literator(tokens...))
251+
err = formatter.Format(&w, resolved_chroma_style(srd), chroma.Literator(tokens...))
239252
// os.WriteFile(filepath.Base(path+".highlighted"), []byte(w.String()), 0o600)
240253
return w.String(), err
241254
}
242255

243256
func highlight_all(paths []string, light bool) {
244257
ctx := images.Context{}
258+
srd := prefer_light_colors(light)
245259
ctx.Parallel(0, len(paths), func(nums <-chan int) {
246260
for i := range nums {
247261
path := paths[i]
248-
raw, err := highlight_file(path, light)
262+
raw, err := HighlightFile(path, &srd)
249263
if err != nil {
250264
continue
251265
}

0 commit comments

Comments
 (0)