From 53a973e9dd9ecf102d7bcc345846f2632585159a Mon Sep 17 00:00:00 2001 From: sharkdp Date: Wed, 22 Apr 2020 22:05:54 +0200 Subject: [PATCH] Add syntaxes and themes method --- examples/advanced.rs | 2 +- examples/list_syntaxes_and_themes.rs | 18 ++++++++++++++++++ src/assets.rs | 4 ++-- src/pretty_printer.rs | 9 +++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 examples/list_syntaxes_and_themes.rs diff --git a/examples/advanced.rs b/examples/advanced.rs index ec90c160..ec9fb59c 100644 --- a/examples/advanced.rs +++ b/examples/advanced.rs @@ -1,5 +1,5 @@ /// A program that prints its own source code using the bat library -use bat::{LineRange, PrettyPrinter, WrappingMode, PagingMode}; +use bat::{LineRange, PagingMode, PrettyPrinter, WrappingMode}; fn main() { PrettyPrinter::new() diff --git a/examples/list_syntaxes_and_themes.rs b/examples/list_syntaxes_and_themes.rs new file mode 100644 index 00000000..27db6d4f --- /dev/null +++ b/examples/list_syntaxes_and_themes.rs @@ -0,0 +1,18 @@ +/// A simple program that prints its own source code using the bat library +use bat::PrettyPrinter; + +fn main() { + let printer = PrettyPrinter::new(); + + println!("Syntaxes:"); + for syntax in printer.syntaxes() { + println!("- {} ({})", syntax.name, syntax.file_extensions.join(", ")); + } + + println!(); + + println!("Themes:"); + for theme in printer.themes() { + println!("- {}", theme); + } +} diff --git a/src/assets.rs b/src/assets.rs index cc6f0d98..11756672 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -164,8 +164,8 @@ impl HighlightingAssets { self.syntax_set.syntaxes() } - pub fn themes(&self) -> impl Iterator { - self.theme_set.themes.keys() + pub fn themes(&self) -> impl Iterator { + self.theme_set.themes.keys().map(|s| s.as_ref()) } pub(crate) fn get_theme(&self, theme: &str) -> &Theme { diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs index 27794201..807ddb18 100644 --- a/src/pretty_printer.rs +++ b/src/pretty_printer.rs @@ -2,6 +2,7 @@ use std::ffi::OsStr; use std::io::Read; use console::Term; +use syntect::parsing::SyntaxReference; use crate::{ assets::HighlightingAssets, @@ -201,6 +202,14 @@ impl<'a> PrettyPrinter<'a> { self } + pub fn themes(&self) -> impl Iterator { + self.assets.themes() + } + + pub fn syntaxes(&self) -> impl Iterator { + self.assets.syntaxes().iter() + } + /// Pretty-print all specified inputs. This method will "use" all stored inputs. /// If you want to call 'print' multiple times, you have to call the appropriate /// input_* methods again.