on master help sign in
[breaking] Remove special handling for theme previews
0319149Ethan P
CHANGELOG.md+3
@@ -51,6 +51,9 @@
51- `PrettyPrinter::vcs_modification_markers` has been marked deprecated when building without the `git` feature, see #997 and #1020 (@eth-p, @sharkdp)
51- `PrettyPrinter::vcs_modification_markers` has been marked deprecated when building without the `git` feature, see #997 and #1020 (@eth-p, @sharkdp)
52- Add APIs to provide `Input` descriptions with `InputDescription` (@eth-p)
52- Add APIs to provide `Input` descriptions with `InputDescription` (@eth-p)
53- Add function to directly provide `Input`s to `PrettyPrinter` (@eth-p)
53- Add function to directly provide `Input`s to `PrettyPrinter` (@eth-p)
54+- <font color="red">`Input::theme_preview_file` is no longer available.</font> (@eth-p)
55+
56+Changes colored <font color="red">red</font> are breaking changes.
54
57
55## Packaging
58## Packaging
56
59
src/assets.rs+1−5
@@ -193,11 +193,7 @@ impl HighlightingAssets {
193 input: &mut OpenedInput,
193 input: &mut OpenedInput,
194 mapping: &SyntaxMapping,
194 mapping: &SyntaxMapping,
195 ) -> Result<&SyntaxReference> {
195 ) -> Result<&SyntaxReference> {
196− if input.kind.is_theme_preview_file() {
197− self.syntax_set
198− .find_syntax_by_name("Rust")
199− .ok_or_else(|| ErrorKind::UnknownSyntax("Rust".to_owned()).into())
200− } else if let Some(language) = language {
196+ if let Some(language) = language {
201 self.syntax_set
197 self.syntax_set
202 .find_syntax_by_token(language)
198 .find_syntax_by_token(language)
203 .ok_or_else(|| ErrorKind::UnknownSyntax(language.to_owned()).into())
199 .ok_or_else(|| ErrorKind::UnknownSyntax(language.to_owned()).into())
src/bin/bat/main.rs+11−2
@@ -10,7 +10,7 @@ mod directories;
10use std::collections::HashSet;
10use std::collections::HashSet;
11use std::ffi::OsStr;
11use std::ffi::OsStr;
12use std::io;
12use std::io;
13−use std::io::Write;
13+use std::io::{BufReader, Write};
14use std::path::Path;
14use std::path::Path;
15use std::process;
15use std::process;
16
16
@@ -25,6 +25,7 @@ use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
25use clap::crate_version;
25use clap::crate_version;
26use directories::PROJECT_DIRS;
26use directories::PROJECT_DIRS;
27
27
28+use bat::input::InputDescription;
28use bat::{
29use bat::{
29 assets::HighlightingAssets,
30 assets::HighlightingAssets,
30 config::Config,
31 config::Config,
@@ -34,6 +35,8 @@ use bat::{
34 style::{StyleComponent, StyleComponents},
35 style::{StyleComponent, StyleComponents},
35};
36};
36
37
38+const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview.rs");
39+
37fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
40fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
38 if matches.is_present("build") {
41 if matches.is_present("build") {
39 let source_dir = matches
42 let source_dir = matches
@@ -118,6 +121,12 @@ pub fn list_languages(config: &Config) -> Result<()> {
118 Ok(())
121 Ok(())
119}
122}
120
123
124+fn theme_preview_file<'a>() -> Input<'a> {
125+ Input::from_reader(Box::new(BufReader::new(THEME_PREVIEW_DATA)))
126+ .with_name(Some("theme.rs".as_ref()))
127+ .with_description(Some(InputDescription::new("")))
128+}
129+
121pub fn list_themes(cfg: &Config) -> Result<()> {
130pub fn list_themes(cfg: &Config) -> Result<()> {
122 let assets = assets_from_cache_or_binary()?;
131 let assets = assets_from_cache_or_binary()?;
123 let mut config = cfg.clone();
132 let mut config = cfg.clone();
@@ -137,7 +146,7 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
137 )?;
146 )?;
138 config.theme = theme.to_string();
147 config.theme = theme.to_string();
139 Controller::new(&config, &assets)
148 Controller::new(&config, &assets)
140− .run(vec![Input::theme_preview_file()])
149+ .run(vec![theme_preview_file()])
141 .ok();
150 .ok();
142 writeln!(stdout)?;
151 writeln!(stdout)?;
143 }
152 }
src/input.rs−28
@@ -6,8 +6,6 @@ use content_inspector::{self, ContentType};
6
6
7use crate::error::*;
7use crate::error::*;
8
8
9−const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs");
10−
11/// A description of an Input source.
9/// A description of an Input source.
12/// This tells bat how to refer to the input.
10/// This tells bat how to refer to the input.
13#[derive(Clone)]
11#[derive(Clone)]
@@ -62,7 +60,6 @@ impl InputDescription {
62pub(crate) enum InputKind<'a> {
60pub(crate) enum InputKind<'a> {
63 OrdinaryFile(OsString),
61 OrdinaryFile(OsString),
64 StdIn,
62 StdIn,
65− ThemePreviewFile,
66 CustomReader(Box<dyn Read + 'a>),
63 CustomReader(Box<dyn Read + 'a>),
67}
64}
68
65
@@ -73,7 +70,6 @@ impl<'a> InputKind<'a> {
73 InputDescription::new(path.to_string_lossy()).with_kind(Some("File"))
70 InputDescription::new(path.to_string_lossy()).with_kind(Some("File"))
74 }
71 }
75 InputKind::StdIn => InputDescription::new("STDIN"),
72 InputKind::StdIn => InputDescription::new("STDIN"),
76− InputKind::ThemePreviewFile => InputDescription::new(""),
77 InputKind::CustomReader(_) => InputDescription::new("READER"),
73 InputKind::CustomReader(_) => InputDescription::new("READER"),
78 }
74 }
79 }
75 }
@@ -93,19 +89,9 @@ pub struct Input<'a> {
93pub(crate) enum OpenedInputKind {
89pub(crate) enum OpenedInputKind {
94 OrdinaryFile(OsString),
90 OrdinaryFile(OsString),
95 StdIn,
91 StdIn,
96− ThemePreviewFile,
97 CustomReader,
92 CustomReader,
98}
93}
99
94
100−impl OpenedInputKind {
101− pub(crate) fn is_theme_preview_file(&self) -> bool {
102− match self {
103− OpenedInputKind::ThemePreviewFile => true,
104− _ => false,
105− }
106− }
107−}
108−
109pub(crate) struct OpenedInput<'a> {
95pub(crate) struct OpenedInput<'a> {
110 pub(crate) kind: OpenedInputKind,
96 pub(crate) kind: OpenedInputKind,
111 pub(crate) metadata: InputMetadata,
97 pub(crate) metadata: InputMetadata,
@@ -130,14 +116,6 @@ impl<'a> Input<'a> {
130 }
116 }
131 }
117 }
132
118
133− pub fn theme_preview_file() -> Self {
134− Input {
135− kind: InputKind::ThemePreviewFile,
136− metadata: InputMetadata::default(),
137− description: None,
138− }
139− }
140−
141 pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self {
119 pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self {
142 Input {
120 Input {
143 kind: InputKind::CustomReader(reader),
121 kind: InputKind::CustomReader(reader),
@@ -196,12 +174,6 @@ impl<'a> Input<'a> {
196 InputReader::new(BufReader::new(file))
174 InputReader::new(BufReader::new(file))
197 },
175 },
198 }),
176 }),
199− InputKind::ThemePreviewFile => Ok(OpenedInput {
200− kind: OpenedInputKind::ThemePreviewFile,
201− description,
202− metadata: self.metadata,
203− reader: InputReader::new(THEME_PREVIEW_FILE),
204− }),
205 InputKind::CustomReader(reader) => Ok(OpenedInput {
177 InputKind::CustomReader(reader) => Ok(OpenedInput {
206 description,
178 description,
207 kind: OpenedInputKind::CustomReader,
179 kind: OpenedInputKind::CustomReader,