on master help sign in
Add syntaxes and themes method
commit53a973e9dd9ecf102d7bcc345846f2632585159a authorsharkdp <davidpeter@web.de> dateWed 22 Apr 2020, 22:05 (7 years ago) parentcba9df7
4 files changed+30−3
examples/advanced.rs+1−1 examples/list_syntaxes_and_themes.rs+18 src/assets.rs+2−2 src/pretty_printer.rs+9
examples/advanced.rs+1−1
@@ -1,5 +1,5 @@
11/// A program that prints its own source code using the bat library
2−use bat::{LineRange, PrettyPrinter, WrappingMode, PagingMode};
2+use bat::{LineRange, PagingMode, PrettyPrinter, WrappingMode};
33
44fn main() {
55 PrettyPrinter::new()
examples/list_syntaxes_and_themes.rsnew file+18
@@ -0,0 +1,18 @@
1+/// A simple program that prints its own source code using the bat library
2+use bat::PrettyPrinter;
3+
4+fn main() {
5+ let printer = PrettyPrinter::new();
6+
7+ println!("Syntaxes:");
8+ for syntax in printer.syntaxes() {
9+ println!("- {} ({})", syntax.name, syntax.file_extensions.join(", "));
10+ }
11+
12+ println!();
13+
14+ println!("Themes:");
15+ for theme in printer.themes() {
16+ println!("- {}", theme);
17+ }
18+}
src/assets.rs+2−2
@@ -164,8 +164,8 @@ impl HighlightingAssets {
164164 self.syntax_set.syntaxes()
165165 }
166166
167− pub fn themes(&self) -> impl Iterator<Item = &String> {
168− self.theme_set.themes.keys()
167+ pub fn themes(&self) -> impl Iterator<Item = &str> {
168+ self.theme_set.themes.keys().map(|s| s.as_ref())
169169 }
170170
171171 pub(crate) fn get_theme(&self, theme: &str) -> &Theme {
src/pretty_printer.rs+9
@@ -2,6 +2,7 @@ use std::ffi::OsStr;
22use std::io::Read;
33
44use console::Term;
5+use syntect::parsing::SyntaxReference;
56
67use crate::{
78 assets::HighlightingAssets,
@@ -201,6 +202,14 @@ impl<'a> PrettyPrinter<'a> {
201202 self
202203 }
203204
205+ pub fn themes(&self) -> impl Iterator<Item = &str> {
206+ self.assets.themes()
207+ }
208+
209+ pub fn syntaxes(&self) -> impl Iterator<Item = &SyntaxReference> {
210+ self.assets.syntaxes().iter()
211+ }
212+
204213 /// Pretty-print all specified inputs. This method will "use" all stored inputs.
205214 /// If you want to call 'print' multiple times, you have to call the appropriate
206215 /// input_* methods again.