[breaking] Remove special handling for theme previews

This commit is contained in:
Ethan P 2020-05-15 16:19:41 -07:00 committed by David Peter
parent 2f823d59b0
commit 0319149b4d
4 changed files with 15 additions and 35 deletions

View File

@ -51,6 +51,9 @@
- `PrettyPrinter::vcs_modification_markers` has been marked deprecated when building without the `git` feature, see #997 and #1020 (@eth-p, @sharkdp) - `PrettyPrinter::vcs_modification_markers` has been marked deprecated when building without the `git` feature, see #997 and #1020 (@eth-p, @sharkdp)
- Add APIs to provide `Input` descriptions with `InputDescription` (@eth-p) - Add APIs to provide `Input` descriptions with `InputDescription` (@eth-p)
- Add function to directly provide `Input`s to `PrettyPrinter` (@eth-p) - Add function to directly provide `Input`s to `PrettyPrinter` (@eth-p)
- <font color="red">`Input::theme_preview_file` is no longer available.</font> (@eth-p)
Changes colored <font color="red">red</font> are breaking changes.
## Packaging ## Packaging

View File

@ -193,11 +193,7 @@ impl HighlightingAssets {
input: &mut OpenedInput, input: &mut OpenedInput,
mapping: &SyntaxMapping, mapping: &SyntaxMapping,
) -> Result<&SyntaxReference> { ) -> Result<&SyntaxReference> {
if input.kind.is_theme_preview_file() { if let Some(language) = language {
self.syntax_set
.find_syntax_by_name("Rust")
.ok_or_else(|| ErrorKind::UnknownSyntax("Rust".to_owned()).into())
} else if let Some(language) = language {
self.syntax_set self.syntax_set
.find_syntax_by_token(language) .find_syntax_by_token(language)
.ok_or_else(|| ErrorKind::UnknownSyntax(language.to_owned()).into()) .ok_or_else(|| ErrorKind::UnknownSyntax(language.to_owned()).into())

View File

@ -10,7 +10,7 @@ mod directories;
use std::collections::HashSet; use std::collections::HashSet;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::io; use std::io;
use std::io::Write; use std::io::{BufReader, Write};
use std::path::Path; use std::path::Path;
use std::process; use std::process;
@ -25,6 +25,7 @@ use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
use clap::crate_version; use clap::crate_version;
use directories::PROJECT_DIRS; use directories::PROJECT_DIRS;
use bat::input::InputDescription;
use bat::{ use bat::{
assets::HighlightingAssets, assets::HighlightingAssets,
config::Config, config::Config,
@ -34,6 +35,8 @@ use bat::{
style::{StyleComponent, StyleComponents}, style::{StyleComponent, StyleComponents},
}; };
const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview.rs");
fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> { fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
if matches.is_present("build") { if matches.is_present("build") {
let source_dir = matches let source_dir = matches
@ -118,6 +121,12 @@ pub fn list_languages(config: &Config) -> Result<()> {
Ok(()) Ok(())
} }
fn theme_preview_file<'a>() -> Input<'a> {
Input::from_reader(Box::new(BufReader::new(THEME_PREVIEW_DATA)))
.with_name(Some("theme.rs".as_ref()))
.with_description(Some(InputDescription::new("")))
}
pub fn list_themes(cfg: &Config) -> Result<()> { pub fn list_themes(cfg: &Config) -> Result<()> {
let assets = assets_from_cache_or_binary()?; let assets = assets_from_cache_or_binary()?;
let mut config = cfg.clone(); let mut config = cfg.clone();
@ -137,7 +146,7 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
)?; )?;
config.theme = theme.to_string(); config.theme = theme.to_string();
Controller::new(&config, &assets) Controller::new(&config, &assets)
.run(vec![Input::theme_preview_file()]) .run(vec![theme_preview_file()])
.ok(); .ok();
writeln!(stdout)?; writeln!(stdout)?;
} }

View File

@ -6,8 +6,6 @@ use content_inspector::{self, ContentType};
use crate::error::*; use crate::error::*;
const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs");
/// A description of an Input source. /// A description of an Input source.
/// This tells bat how to refer to the input. /// This tells bat how to refer to the input.
#[derive(Clone)] #[derive(Clone)]
@ -62,7 +60,6 @@ impl InputDescription {
pub(crate) enum InputKind<'a> { pub(crate) enum InputKind<'a> {
OrdinaryFile(OsString), OrdinaryFile(OsString),
StdIn, StdIn,
ThemePreviewFile,
CustomReader(Box<dyn Read + 'a>), CustomReader(Box<dyn Read + 'a>),
} }
@ -73,7 +70,6 @@ impl<'a> InputKind<'a> {
InputDescription::new(path.to_string_lossy()).with_kind(Some("File")) InputDescription::new(path.to_string_lossy()).with_kind(Some("File"))
} }
InputKind::StdIn => InputDescription::new("STDIN"), InputKind::StdIn => InputDescription::new("STDIN"),
InputKind::ThemePreviewFile => InputDescription::new(""),
InputKind::CustomReader(_) => InputDescription::new("READER"), InputKind::CustomReader(_) => InputDescription::new("READER"),
} }
} }
@ -93,19 +89,9 @@ pub struct Input<'a> {
pub(crate) enum OpenedInputKind { pub(crate) enum OpenedInputKind {
OrdinaryFile(OsString), OrdinaryFile(OsString),
StdIn, StdIn,
ThemePreviewFile,
CustomReader, CustomReader,
} }
impl OpenedInputKind {
pub(crate) fn is_theme_preview_file(&self) -> bool {
match self {
OpenedInputKind::ThemePreviewFile => true,
_ => false,
}
}
}
pub(crate) struct OpenedInput<'a> { pub(crate) struct OpenedInput<'a> {
pub(crate) kind: OpenedInputKind, pub(crate) kind: OpenedInputKind,
pub(crate) metadata: InputMetadata, pub(crate) metadata: InputMetadata,
@ -130,14 +116,6 @@ impl<'a> Input<'a> {
} }
} }
pub fn theme_preview_file() -> Self {
Input {
kind: InputKind::ThemePreviewFile,
metadata: InputMetadata::default(),
description: None,
}
}
pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self { pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self {
Input { Input {
kind: InputKind::CustomReader(reader), kind: InputKind::CustomReader(reader),
@ -196,12 +174,6 @@ impl<'a> Input<'a> {
InputReader::new(BufReader::new(file)) InputReader::new(BufReader::new(file))
}, },
}), }),
InputKind::ThemePreviewFile => Ok(OpenedInput {
kind: OpenedInputKind::ThemePreviewFile,
description,
metadata: self.metadata,
reader: InputReader::new(THEME_PREVIEW_FILE),
}),
InputKind::CustomReader(reader) => Ok(OpenedInput { InputKind::CustomReader(reader) => Ok(OpenedInput {
description, description,
kind: OpenedInputKind::CustomReader, kind: OpenedInputKind::CustomReader,