Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose Theme Based on The Terminal's Color Scheme #2896

Merged
merged 30 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1423dd9
Choose theme based on the terminal's color scheme
bash Apr 16, 2024
de79639
Deprecate old `default_theme` function
bash Apr 16, 2024
cda363a
Use `default_theme()` function from theme module
bash Apr 16, 2024
cea45e0
Expose new theme selection in CLI
bash Jul 18, 2024
9a1bfe9
Update completions and man page
bash Apr 16, 2024
14ce668
Add generated powershell completion to ignore list
bash Apr 16, 2024
ff81cfd
Move actual detection into library
bash Apr 16, 2024
30b0143
Make default_theme pub
bash Jul 18, 2024
6498615
Improve upon the documentation
bash Apr 16, 2024
e8ca6ec
Remove cargo feature
bash Apr 16, 2024
594b141
Update readme
bash Apr 16, 2024
c3b190d
Disable color detection in test
bash Apr 16, 2024
06b6454
Add changelog entry
bash Apr 16, 2024
1b0a6da
Use new `default_theme` fn for --list-themes
bash Apr 16, 2024
5c69747
Respect --detect-color-scheme flag when listing themes
bash May 1, 2024
abf9dad
Remove `HighlightingAssets::default_theme()`
bash Jul 18, 2024
b9b981f
Generalize --detect-color-scheme to --color-scheme
bash Jul 18, 2024
bc42149
Merge color scheme options into theme / BAT_THEME
bash Aug 18, 2024
89ce060
Update help, man page and completions
bash Aug 18, 2024
5095847
Return theme alongside detected color scheme
bash Aug 23, 2024
16d9b99
Flatten preference enum
bash Sep 4, 2024
e075fee
Add infallible constructor
bash Sep 7, 2024
60e4027
Expose theme env vars
bash Sep 7, 2024
10e823c
Rename internal function
bash Sep 7, 2024
f6cbee9
Update docs
bash Sep 7, 2024
0ebb9cb
Add Display impl
bash Sep 7, 2024
02ae6ef
Remove redundant guard
bash Sep 7, 2024
b747184
Accept `impl Into<String>` to avoid cloning strings
bash Sep 8, 2024
60693db
Merge branch 'master' into dark-light
keith-hall Nov 13, 2024
08047a6
Merge branch 'master' into dark-light
keith-hall Nov 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
**/*.rs.bk

# Generated files
/assets/completions/_bat.ps1
/assets/completions/bat.bash
/assets/completions/bat.fish
/assets/completions/bat.zsh
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Syntax highlighting for JavaScript files that start with `#!/usr/bin/env bun` #2913 (@sharunkumar)
- `bat --strip-ansi={never,always,auto}` to remove ANSI escape sequences from bat's input, see #2999 (@eth-p)
- Add or remove individual style components without replacing all styles #2929 (@eth-p)
- Automatically choose theme based on the terminal's color scheme, see #2896 (@bash)

## Bugfixes

Expand Down Expand Up @@ -69,6 +70,9 @@
- [BREAKING] `SyntaxMapping::mappings` is replaced by `SyntaxMapping::{builtin,custom,all}_mappings`
- Make `Controller::run_with_error_handler`'s error handler `FnMut`, see #2831 (@rhysd)
- Improve compile time by 20%, see #2815 (@dtolnay)
- Add `theme::theme` for choosing an appropriate theme based on the
terminal's color scheme, see #2896 (@bash)
- [BREAKING] Remove `HighlightingAssets::default_theme`. Use `theme::default_theme` instead.

# v0.24.0

Expand Down
51 changes: 47 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ bytesize = { version = "1.3.0" }
encoding_rs = "0.8.34"
os_str_bytes = { version = "~7.0", optional = true }
run_script = { version = "^0.10.1", optional = true}
terminal-colorsaurus = "0.4"

[dependencies.git2]
version = "0.18"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,10 @@ the following command (you need [`fzf`](https://github.com/junegunn/fzf) for thi
bat --list-themes | fzf --preview="bat --theme={} --color=always /path/to/file"
```

`bat` looks good on a dark background by default. However, if your terminal uses a
light background, some themes like `GitHub` or `OneHalfLight` will work better for you.
`bat` automatically picks a fitting theme depending on your terminal's background color.
You can use the `--theme-light` / `--theme-light` options or the `BAT_THEME_DARK` / `BAT_THEME_LIGHT` environment variables
to customize the themes used. This is especially useful if you frequently switch between dark and light mode.

You can also use a custom theme by following the
['Adding new themes' section below](https://github.com/sharkdp/bat#adding-new-themes).

Expand Down
3 changes: 3 additions & 0 deletions assets/completions/_bat.ps1.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ Register-ArgumentCompleter -Native -CommandName '{{PROJECT_EXECUTABLE}}' -Script
[CompletionResult]::new('--color', 'color', [CompletionResultType]::ParameterName, 'When to use colors (*auto*, never, always).')
[CompletionResult]::new('--italic-text', 'italic-text', [CompletionResultType]::ParameterName, 'Use italics in output (always, *never*)')
[CompletionResult]::new('--decorations', 'decorations', [CompletionResultType]::ParameterName, 'When to show the decorations (*auto*, never, always).')
[CompletionResult]::new('--color-scheme', 'color-scheme', [CompletionResultType]::ParameterName, 'Whether to choose a dark or light syntax highlighting theme (*auto*, auto:always, dark, light, system).')
[CompletionResult]::new('--paging', 'paging', [CompletionResultType]::ParameterName, 'Specify when to use the pager, or use `-P` to disable (*auto*, never, always).')
[CompletionResult]::new('--pager', 'pager', [CompletionResultType]::ParameterName, 'Determine which pager to use.')
[CompletionResult]::new('-m', 'm', [CompletionResultType]::ParameterName, 'Use the specified syntax for files matching the glob pattern (''*.cpp:C++'').')
[CompletionResult]::new('--map-syntax', 'map-syntax', [CompletionResultType]::ParameterName, 'Use the specified syntax for files matching the glob pattern (''*.cpp:C++'').')
[CompletionResult]::new('--theme', 'theme', [CompletionResultType]::ParameterName, 'Set the color theme for syntax highlighting.')
[CompletionResult]::new('--theme-dark', 'theme', [CompletionResultType]::ParameterName, 'Set the color theme for syntax highlighting for dark backgrounds.')
[CompletionResult]::new('--theme-light', 'theme', [CompletionResultType]::ParameterName, 'Set the color theme for syntax highlighting for light backgrounds.')
[CompletionResult]::new('--style', 'style', [CompletionResultType]::ParameterName, 'Comma-separated list of style elements to display (*default*, auto, full, plain, changes, header, header-filename, header-filesize, grid, rule, numbers, snip).')
[CompletionResult]::new('-r', 'r', [CompletionResultType]::ParameterName, 'Only print the lines from N to M.')
[CompletionResult]::new('--line-range', 'line-range', [CompletionResultType]::ParameterName, 'Only print the lines from N to M.')
Expand Down
9 changes: 8 additions & 1 deletion assets/completions/bat.bash.in
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ _bat() {
COMPREPLY=($(compgen -W "auto never always" -- "$cur"))
return 0
;;
--color-scheme)
COMPREPLY=($(compgen -W "auto auto:always dark light system" -- "$cur"))
--italic-text)
COMPREPLY=($(compgen -W "always never" -- "$cur"))
return 0
Expand All @@ -112,7 +114,9 @@ _bat() {
COMPREPLY=($(compgen -c -- "$cur"))
return 0
;;
--theme)
--theme | \
--theme-dark | \
--theme-light)
local IFS=$'\n'
COMPREPLY=($(compgen -W "$("$1" --list-themes)" -- "$cur"))
__bat_escape_completions
Expand Down Expand Up @@ -170,6 +174,9 @@ _bat() {
--map-syntax
--ignored-suffix
--theme
--theme-dark
--theme-light
--color-scheme
--list-themes
--squeeze-blank
--squeeze-limit
Expand Down
13 changes: 13 additions & 0 deletions assets/completions/bat.fish.in
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ set -l color_opts '
'
set -l decorations_opts $color_opts
set -l paging_opts $color_opts
set -l color_scheme_opts "
auto\t'Use the terminal\'s color scheme if the output is not redirected (default)'
auto:always\t'Always use the terminal\'s color scheme'
dark\t'Use a dark syntax highlighting theme'
light\t'Use a light syntax highlighting theme'
system\t'Query the OS for its color scheme (macOS only)'
"

# Include some examples so we can indicate the default.
set -l pager_opts '
Expand Down Expand Up @@ -143,6 +150,8 @@ complete -c $bat -l config-file -f -d "Display location of configuration file" -

complete -c $bat -l decorations -x -a "$decorations_opts" -d "When to use --style decorations" -n __bat_no_excl_args

complete -c $bat -l color-scheme -x -a "$color_scheme_opts" -d "Whether to choose a dark or light syntax highlighting theme" -n __bat_no_excl_args

complete -c $bat -l diagnostic -d "Print diagnostic info for bug reports" -n __fish_is_first_arg

complete -c $bat -s d -l diff -d "Only show lines with Git changes" -n __bat_no_excl_args
Expand Down Expand Up @@ -205,6 +214,10 @@ complete -c $bat -l terminal-width -x -d "Set terminal <width>, +<offset>, or -<

complete -c $bat -l theme -x -a "(command $bat --list-themes | command cat)" -d "Set the syntax highlighting theme" -n __bat_no_excl_args

complete -c $bat -l theme-dark -x -a "(command $bat --list-themes | command cat)" -d "Set the syntax highlighting theme for dark backgrounds" -n __bat_no_excl_args

complete -c $bat -l theme-light -x -a "(command $bat --list-themes | command cat)" -d "Set the syntax highlighting theme for light backgrounds" -n __bat_no_excl_args

complete -c $bat -s V -l version -f -d "Show version information" -n __fish_is_first_arg

complete -c $bat -l wrap -x -a "$wrap_opts" -d "Text-wrapping mode" -n __bat_no_excl_args
Expand Down
3 changes: 3 additions & 0 deletions assets/completions/bat.zsh.in
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ _{{PROJECT_EXECUTABLE}}_main() {
--color='[specify when to use colors]:when:(auto never always)'
--italic-text='[use italics in output]:when:(always never)'
--decorations='[specify when to show the decorations]:when:(auto never always)'
--color-scheme="[whether to choose a dark or light syntax highlighting theme]:scheme:(auto auto:always dark light system)"
--paging='[specify when to use the pager]:when:(auto never always)'
'(-m --map-syntax)'{-m+,--map-syntax=}'[map a glob pattern to an existing syntax name]: :->syntax-maps'
'(--theme)'--theme='[set the color theme for syntax highlighting]:theme:->themes'
'(--theme-dark)'--theme-dark='[set the color theme for syntax highlighting for dark backgrounds]:theme:->themes'
'(--theme-light)'--theme-light='[set the color theme for syntax highlighting for light backgrounds]:theme:->themes'
'(: --list-themes --list-languages -L)'--list-themes'[show all supported highlighting themes]'
--style='[comma-separated list of style elements to display]: : _values "style [default]"
default auto full plain changes header header-filename header-filesize grid rule numbers snip'
Expand Down
38 changes: 35 additions & 3 deletions assets/manual/bat.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ Specify when to use the decorations that have been specified via '\-\-style'. Th
automatic mode only enables decorations if an interactive terminal is detected. Possible
values: *auto*, never, always.
.HP
\fB\-\-detect\-color\-scheme\fR <when>
.IP
Specify when to query the terminal for its colors in order to pick an appropriate syntax
highlighting theme. Use \fB\-\-theme-light\fP and \fB\-\-theme-dark\fP (or the environment variables
\fBBAT_THEME_LIGHT\fP and \fBBAT_THEME_DARK\fP) to configure which themes are picked. You can also use
\fP\-\-theme\fP to set a theme that is used regardless of the terminal's colors.
.IP
\fI<when>\fP can be one of:
.RS
.IP "\fBauto\fP"
Only query the terminals colors if the output is not redirected. This is to prevent
race conditions with pagers such as less.
.IP "never"
Never query the terminal for its colors and assume that the terminal has a dark background.
.IP "always"
Always query the terminal for its colors, regardless of whether or not the output is redirected.
.RE
.HP
\fB\-f\fR, \fB\-\-force\-colorization\fR
.IP
Alias for '--decorations=always --color=always'. This is useful \
Expand Down Expand Up @@ -152,9 +170,23 @@ will use JSON syntax, and ignore '.dev'
.HP
\fB\-\-theme\fR <theme>
.IP
Set the theme for syntax highlighting. Use '\-\-list\-themes' to see all available themes.
To set a default theme, add the '\-\-theme="..."' option to the configuration file or
export the BAT_THEME environment variable (e.g.: export BAT_THEME="...").
Set the theme for syntax highlighting. Use \fB\-\-list\-themes\fP to see all available themes.
To set a default theme, add the \fB\-\-theme="..."\fP option to the configuration file or
export the \fBBAT_THEME\fP environment variable (e.g.: \fBexport BAT_THEME="..."\fP).
.HP
\fB\-\-theme\-dark\fR <theme>
.IP
Sets the theme name for syntax highlighting used when the terminal uses a dark background.
To set a default theme, add the \fB\-\-theme-dark="..."\fP option to the configuration file or
export the \fBBAT_THEME_DARK\fP environment variable (e.g. \fBexport BAT_THEME_DARK="..."\fP).
This option is ignored if \fB\-\-theme\fP option is set.
.HP
\fB\-\-theme\-light\fR <theme>
.IP
Sets the theme name for syntax highlighting used when the terminal uses a dark background.
To set a default theme, add the \fB\-\-theme-dark="..."\fP option to the configuration file or
export the \fBBAT_THEME_LIGHT\fP environment variable (e.g. \fBexport BAT_THEME_LIGHT="..."\fP).
This option is ignored if \fB\-\-theme\fP option is set.
.HP
\fB\-\-list\-themes\fR
.IP
Expand Down
36 changes: 33 additions & 3 deletions doc/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,39 @@ Options:
'bat --ignored-suffix ".dev" my_file.json.dev' will use JSON syntax, and ignore '.dev'

--theme <theme>
Set the theme for syntax highlighting. Use '--list-themes' to see all available themes. To
set a default theme, add the '--theme="..."' option to the configuration file or export
the BAT_THEME environment variable (e.g.: export BAT_THEME="...").
Set the theme for syntax highlighting. Note that this option overrides '--theme-dark' and
'--theme-light'. Use '--list-themes' to see all available themes. To set a default theme,
add the '--theme="..."' option to the configuration file or export the BAT_THEME
environment variable (e.g.: export BAT_THEME="...").

--color-scheme <scheme>
Specify whether to choose a dark or light syntax highlighting theme. Use '--theme-light'
and '--theme-dark' (or the environment variables BAT_THEME_LIGHT and BAT_THEME_DARK) to
configure which themes are picked. You may also use '--theme' to set a theme that is used
regardless of this choice.

Possible values:
* auto (default):
Query the terminals for its color scheme if the output is not redirected. This is to
prevent race conditions with pagers such as less.
* 'auto:always':
Always query the terminal for its color scheme, regardless of whether or not the
output is redirected.
* dark: Use a dark syntax highlighting theme.
* light: Use a light syntax highlighting theme.
* system: Query the OS for its color scheme. Only works on macOS.

--theme-light <theme>
Sets the theme name for syntax highlighting used when the terminal uses a light
background. Use '--list-themes' to see all available themes. To set a default theme, add
the '--theme-light="..." option to the configuration file or export the BAT_THEME_LIGHT
environment variable (e.g. export BAT_THEME_LIGHT="...").

--theme-dark <theme>
Sets the theme name for syntax highlighting used when the terminal uses a dark background.
Use '--list-themes' to see all available themes. To set a default theme, add the
'--theme-dark="..." option to the configuration file or export the BAT_THEME_DARK
environment variable (e.g. export BAT_THEME_DARK="...").

--list-themes
Display a list of supported themes for syntax highlighting.
Expand Down
6 changes: 6 additions & 0 deletions doc/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Options:
Use the specified syntax for files matching the glob pattern ('*.cpp:C++').
--theme <theme>
Set the color theme for syntax highlighting.
--color-scheme <scheme>
Specify whether to choose a dark or light theme.
--theme-light <theme>
Sets the color theme for syntax highlighting used for light backgrounds.
--theme-dark <theme>
Sets the color theme for syntax highlighting used for dark backgrounds.
--list-themes
Display all supported highlighting themes.
-s, --squeeze-blank
Expand Down
Loading