fix: gag stdout and stderr while loading theme assets to silence bat warning

This commit is contained in:
Alexandre Pasmantier 2024-11-10 14:48:53 +01:00
parent 51a98db9d5
commit 32c114aa9f
6 changed files with 70 additions and 5 deletions

View File

@ -30,7 +30,7 @@ ui_scale = 80
# The theme to use for syntax highlighting
# A list of available themes can be found in the https://github.com/sharkdp/bat
# repository which uses the same syntax highlighting engine as television
theme = "Catppuccin Mocha"
theme = "Visual Studio Dark+"
# Keybindings
# ----------------------------------------------------------------------------

11
Cargo.lock generated
View File

@ -1093,6 +1093,16 @@ dependencies = [
"slab",
]
[[package]]
name = "gag"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a713bee13966e9fbffdf7193af71d54a6b35a0bb34997cd6c9519ebeb5005972"
dependencies = [
"filedescriptor",
"tempfile",
]
[[package]]
name = "generic-array"
version = "0.14.7"
@ -3200,6 +3210,7 @@ dependencies = [
"bat",
"color-eyre",
"directories",
"gag",
"ignore",
"infer",
"lazy_static",

View File

@ -61,6 +61,37 @@ contents of a file, the value of an environment variable, etc. Because entries r
represent different types of data, **Television** allows for channels to declare the type of previewer that should be
used. Television comes with a set of built-in previewers that can be used out of the box and will grow over time.
## Recipes
Here are some examples of how you can use `television` to make your life easier, more productive and fun. You may want to add some of these examples as aliases to your shell configuration file so that you can easily access them.
**NOTE**: *most of the following examples are meant for macOS. Most of the commands should work on Linux as well, but you may need to adjust them slightly.*
#### CDing into git repo
```bash
cd `tv git-repos`
```
#### Opening file in default editor
```bash
open `tv`
```
##### VSCode:
```bash
code --goto `tv`
```
##### Vim
```bash
vim `tv`
```
at a specific line using the text channel
```bash
tv text | xargs -oI {} sh -c 'vim "$(echo {} | cut -d ":" -f 1)" +$(echo {} | cut -d ":" -f 2)'
```
#### Inspecting the current directory
```bash
ls -1a | tv
```
## Customization
You may wish to customize the behavior of `television` by providing your own configuration file. The configuration file
@ -108,7 +139,7 @@ ui_scale = 80
# The theme to use for syntax highlighting
# A list of available themes can be found in the https://github.com/sharkdp/bat
# repository which uses the same syntax highlighting engine as television
theme = "Catppuccin Mocha"
theme = "Visual Studio Dark+"
# Keybindings
# ----------------------------------------------------------------------------

View File

@ -22,7 +22,9 @@ use television_utils::strings::{
preprocess_line, proportion_of_printable_ascii_characters,
PRINTABLE_ASCII_THRESHOLD,
};
use television_utils::syntax::{self, load_highlighting_assets};
use television_utils::syntax::{
self, load_highlighting_assets, HighlightingAssetsExt,
};
#[derive(Debug, Default)]
pub struct FilePreviewer {
@ -53,7 +55,7 @@ impl FilePreviewer {
let theme_set = ThemeSet::load_defaults();
theme_set.themes["base16-ocean.dark"].clone()
},
|c| hl_assets.get_theme(&c.theme).clone(),
|c| hl_assets.get_theme_no_output(&c.theme).clone(),
);
//info!("getting image picker");
//let image_picker = get_image_picker();

View File

@ -23,3 +23,4 @@ color-eyre = "0.6.3"
bat = "0.24.0"
directories = "5.0.1"
syntect = "5.2.0"
gag = "1.0.0"

View File

@ -1,4 +1,5 @@
use bat::assets::HighlightingAssets;
use gag::Gag;
use std::path::{Path, PathBuf};
use syntect::easy::HighlightLines;
use syntect::highlighting::{Style, Theme};
@ -66,7 +67,8 @@ use lazy_static::lazy_static;
#[cfg(target_os = "macos")]
use std::env;
/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
/// Wrapper for 'dirs' that treats `MacOS` more like `Linux`, by following the XDG specification.
///
/// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are
/// checked first. The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
pub struct BatProjectDirs {
@ -103,3 +105,21 @@ pub fn load_highlighting_assets() -> HighlightingAssets {
HighlightingAssets::from_cache(PROJECT_DIRS.cache_dir())
.unwrap_or_else(|_| HighlightingAssets::from_binary())
}
pub trait HighlightingAssetsExt {
fn get_theme_no_output(&self, theme_name: &str) -> &Theme;
}
impl HighlightingAssetsExt for HighlightingAssets {
/// Get a theme by name. If the theme is not found, the default theme is returned.
///
/// This is an ugly hack to work around the fact that bat actually prints a warning
/// to stderr when a theme is not found which might mess up the TUI. This function
/// suppresses that warning by temporarily redirecting stderr and stdout.
fn get_theme_no_output(&self, theme_name: &str) -> &Theme {
let _e = Gag::stderr().unwrap();
let _o = Gag::stdout().unwrap();
let theme = self.get_theme(theme_name);
theme
}
}