Skip to content

Commit

Permalink
diff kitten: Automatically use dark/light color scheme based on the c…
Browse files Browse the repository at this point in the history
…olor scheme of the parent terminal

Fixes #8170
  • Loading branch information
kovidgoyal committed Jan 4, 2025
1 parent 134271b commit e34a899
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 129 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Detailed list of changes
0.38.2 [future]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- diff kitten: Automatically use dark/light color scheme based on the color scheme of the parent terminal. Can be controlled via the new :opt:`kitten-diff.color_scheme` option. Note that this is a **behavior change** by default. (:iss:`8170`)

- When mapping a custom kitten allow using shell escaping for the kitten path (:iss:`8178`)

- Fix border colors not being changed by auto light/dark themes at startup (:iss:`8180`)
Expand Down
50 changes: 26 additions & 24 deletions kittens/diff/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,30 @@ func ansi_formatter(w io.Writer, style *chroma.Style, it chroma.Iterator) (err e
return nil
}

func resolved_chroma_style() *chroma.Style {
name := utils.IfElse(use_dark_colors, conf.Dark_pygments_style, conf.Pygments_style)
var style *chroma.Style
if name == "default" {
style = DefaultStyle()
} else {
style = styles.Get(name)
}
if style == nil {
if resolved_colors.Background.IsDark() && !resolved_colors.Foreground.IsDark() {
style = styles.Get("monokai")
if style == nil {
style = styles.Get("github-dark")
}
} else {
style = DefaultStyle()
}
if style == nil {
style = styles.Fallback
}
}
return style
}

func highlight_file(path string) (highlighted string, err error) {
filename_for_detection := filepath.Base(path)
ext := filepath.Ext(filename_for_detection)
Expand All @@ -174,42 +198,20 @@ func highlight_file(path string) (highlighted string, err error) {
}
lexer := lexers.Match(filename_for_detection)
if lexer == nil {
if err == nil {
lexer = lexers.Analyse(text)
}
lexer = lexers.Analyse(text)
}
if lexer == nil {
return "", fmt.Errorf("Cannot highlight %#v: %w", path, ErrNoLexer)
}
lexer = chroma.Coalesce(lexer)
name := conf.Pygments_style
var style *chroma.Style
if name == "default" {
style = DefaultStyle()
} else {
style = styles.Get(name)
}
if style == nil {
if conf.Background.IsDark() && !conf.Foreground.IsDark() {
style = styles.Get("monokai")
if style == nil {
style = styles.Get("github-dark")
}
} else {
style = DefaultStyle()
}
if style == nil {
style = styles.Fallback
}
}
iterator, err := lexer.Tokenise(nil, text)
if err != nil {
return "", err
}
formatter := chroma.FormatterFunc(ansi_formatter)
w := strings.Builder{}
w.Grow(len(text) * 2)
err = formatter.Format(&w, style, iterator)
err = formatter.Format(&w, resolved_chroma_style(), iterator)
// os.WriteFile(filepath.Base(path+".highlighted"), []byte(w.String()), 0o600)
return w.String(), err
}
Expand Down
2 changes: 1 addition & 1 deletion kittens/diff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func main(_ *cli.Command, opts_ *Options, args []string) (rc int, err error) {
return 1, err
}
init_caches()
create_formatters()
defer func() {
for tdir := range remote_dirs {
os.RemoveAll(tdir)
Expand Down Expand Up @@ -154,6 +153,7 @@ func main(_ *cli.Command, opts_ *Options, args []string) (rc int, err error) {
if !tc.KeyboardProtocol {
return fmt.Errorf("This terminal does not support the kitty keyboard protocol, or you are running inside a terminal multiplexer that is blocking querying for kitty keyboard protocol support. The diff kitten cannot function without it.")
}
h.on_capabilities_received(tc)
return nil
}
lp.OnWakeup = h.on_wakeup
Expand Down
129 changes: 58 additions & 71 deletions kittens/diff/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,103 +71,90 @@ def main(args: List[str]) -> None:
# colors {{{
agr('colors', 'Colors')

opt('pygments_style', 'default',
long_text='''
opt('color_scheme', 'auto', choices=('auto', 'light', 'dark'), long_text='''
Whether to use the light or dark colors. The default of :code:`auto` means
to follow the parent terminal color scheme.
''')

opt('pygments_style', 'default', long_text='''
The pygments color scheme to use for syntax highlighting. See :link:`pygments
builtin styles <https://pygments.org/styles/>` for a list of schemes. Note that
this **does not** change the colors used for diffing,
only the colors used for syntax highlighting. To change the general colors use the settings below.
This sets the colors used for light color schemes, use :opt:`dark_pygments_style` to change the
colors for dark color schemes.
'''
)

opt('foreground', 'black',
option_type='to_color',
long_text='Basic colors'
)
opt('dark_pygments_style', 'github-dark', long_text='''
The pygments color scheme to use for syntax highlighting with dark colors. See :link:`pygments
builtin styles <https://pygments.org/styles/>` for a list of schemes. Note that
this **does not** change the colors used for diffing,
only the colors used for syntax highlighting. To change the general colors use the settings below.
This sets the colors used for dark color schemes, use :opt:`pygments_style` to change the
colors for light color schemes.''')

opt('background', 'white',
option_type='to_color',
)
opt('foreground', 'black', option_type='to_color', long_text='Basic colors')
opt('dark_foreground', '#f8f8f2', option_type='to_color', long_text='Basic colors')

opt('title_fg', 'black',
option_type='to_color',
long_text='Title colors'
)
dark_bg = '#212830'
opt('background', 'white', option_type='to_color',)
opt('dark_background', dark_bg, option_type='to_color',)

opt('title_bg', 'white',
option_type='to_color',
)
opt('title_fg', 'black', option_type='to_color', long_text='Title colors')
opt('dark_title_fg', 'white', option_type='to_color')

opt('margin_bg', '#fafbfc',
option_type='to_color',
long_text='Margin colors'
)
opt('title_bg', 'white', option_type='to_color',)
opt('dark_title_bg', dark_bg, option_type='to_color',)

opt('margin_fg', '#aaaaaa',
option_type='to_color',
)
opt('margin_bg', '#fafbfc', option_type='to_color', long_text='Margin colors')
opt('dark_margin_bg', dark_bg, option_type='to_color')

opt('removed_bg', '#ffeef0',
option_type='to_color',
long_text='Removed text backgrounds'
)
opt('margin_fg', '#aaaaaa', option_type='to_color')
opt('dark_margin_fg', '#aaaaaa', option_type='to_color')

opt('highlight_removed_bg', '#fdb8c0',
option_type='to_color',
)
opt('removed_bg', '#ffeef0', option_type='to_color', long_text='Removed text backgrounds')
opt('dark_removed_bg', '#352c33', option_type='to_color')

opt('removed_margin_bg', '#ffdce0',
option_type='to_color',
)
opt('highlight_removed_bg', '#fdb8c0', option_type='to_color')
opt('dark_highlight_removed_bg', '#5c3539', option_type='to_color')

opt('added_bg', '#e6ffed',
option_type='to_color',
long_text='Added text backgrounds'
)
opt('removed_margin_bg', '#ffdce0', option_type='to_color')
opt('dark_removed_margin_bg', '#5c3539', option_type='to_color')

opt('highlight_added_bg', '#acf2bd',
option_type='to_color',
)
opt('added_bg', '#e6ffed', option_type='to_color', long_text='Added text backgrounds')
opt('dark_added_bg', '#263834', option_type='to_color')

opt('added_margin_bg', '#cdffd8',
option_type='to_color',
)
opt('highlight_added_bg', '#acf2bd', option_type='to_color')
opt('dark_highlight_added_bg', '#31503d', option_type='to_color')

opt('filler_bg', '#fafbfc',
option_type='to_color',
long_text='Filler (empty) line background'
)
opt('added_margin_bg', '#cdffd8', option_type='to_color')
opt('dark_added_margin_bg', '#31503d', option_type='to_color')

opt('margin_filler_bg', 'none',
option_type='to_color_or_none',
long_text='Filler (empty) line background in margins, defaults to the filler background'
)
opt('filler_bg', '#fafbfc', option_type='to_color', long_text='Filler (empty) line background')
opt('dark_filler_bg', '#262c36', option_type='to_color')

opt('hunk_margin_bg', '#dbedff',
option_type='to_color',
long_text='Hunk header colors'
)
opt('margin_filler_bg', 'none', option_type='to_color_or_none', long_text='Filler (empty) line background in margins, defaults to the filler background')
opt('dark_margin_filler_bg', 'none', option_type='to_color_or_none')

opt('hunk_bg', '#f1f8ff',
option_type='to_color',
)

opt('search_bg', '#444',
option_type='to_color',
long_text='Highlighting'
)
opt('hunk_margin_bg', '#dbedff', option_type='to_color', long_text='Hunk header colors')
opt('dark_hunk_margin_bg', '#0c2d6b', option_type='to_color')

opt('search_fg', 'white',
option_type='to_color',
)
opt('hunk_bg', '#f1f8ff', option_type='to_color')
opt('dark_hunk_bg', '#253142', option_type='to_color')

opt('select_bg', '#b4d5fe',
option_type='to_color',
)
opt('search_bg', '#444', option_type='to_color', long_text='Highlighting')
opt('dark_search_bg', '#2c599c', option_type='to_color')

opt('select_fg', 'black',
option_type='to_color_or_none',
)
opt('search_fg', 'white', option_type='to_color')
opt('dark_search_fg', 'white', option_type='to_color')

opt('select_bg', '#b4d5fe', option_type='to_color')
opt('dark_select_bg', '#2c599c', option_type='to_color')

opt('select_fg', 'black', option_type='to_color_or_none')
opt('dark_select_fg', 'white', option_type='to_color_or_none')
egr() # }}}

# shortcuts {{{
Expand Down
Loading

0 comments on commit e34a899

Please sign in to comment.