Merge pull request #237 from dtolnay/bat

Inline bat's PrettyPrinter logic
This commit is contained in:
David Tolnay 2024-10-23 17:46:38 -07:00 committed by GitHub
commit 497a5a94f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 19 deletions

1
Cargo.lock generated
View file

@ -183,6 +183,7 @@ dependencies = [
"bat", "bat",
"cargo-subcommand-metadata", "cargo-subcommand-metadata",
"clap", "clap",
"console",
"fs-err", "fs-err",
"prettyplease", "prettyplease",
"proc-macro2", "proc-macro2",

View file

@ -20,6 +20,7 @@ prettyplease = []
bat = { version = "0.24", default-features = false, features = ["paging", "regex-fancy"] } bat = { version = "0.24", default-features = false, features = ["paging", "regex-fancy"] }
cargo-subcommand-metadata = "0.1" cargo-subcommand-metadata = "0.1"
clap = { version = "4", features = ["deprecated", "derive"] } clap = { version = "4", features = ["deprecated", "derive"] }
console = "0.15"
fs-err = "3" fs-err = "3"
prettyplease = { version = "0.2.25", features = ["verbatim"] } prettyplease = { version = "0.2.25", features = ["verbatim"] }
proc-macro2 = "1.0.80" proc-macro2 = "1.0.80"

View file

@ -32,7 +32,11 @@ use crate::error::Result;
use crate::opts::{Coloring, Expand, Subcommand}; use crate::opts::{Coloring, Expand, Subcommand};
use crate::unparse::unparse_maximal; use crate::unparse::unparse_maximal;
use crate::version::Version; use crate::version::Version;
use bat::{PagingMode, PrettyPrinter}; use bat::assets::HighlightingAssets;
use bat::config::VisibleLines;
use bat::line_range::{HighlightedLineRanges, LineRanges};
use bat::style::StyleComponents;
use bat::{PagingMode, SyntaxMapping, WrappingMode};
use clap::{CommandFactory as _, Parser, ValueEnum}; use clap::{CommandFactory as _, Parser, ValueEnum};
use quote::quote; use quote::quote;
use std::env; use std::env;
@ -134,7 +138,7 @@ fn do_cargo_expand() -> Result<i32> {
let config = config::deserialize(); let config = config::deserialize();
if args.themes { if args.themes {
for theme in PrettyPrinter::new().themes() { for theme in HighlightingAssets::from_binary().themes() {
let _ = writeln!(io::stdout(), "{}", theme); let _ = writeln!(io::stdout(), "{}", theme);
} }
return Ok(0); return Ok(0);
@ -286,24 +290,34 @@ fn do_cargo_expand() -> Result<i32> {
}; };
let _ = writeln!(io::stderr()); let _ = writeln!(io::stderr());
if do_color { if do_color {
let mut pretty_printer = PrettyPrinter::new(); let config = bat::config::Config {
pretty_printer language: Some("rust"),
.input_from_bytes(content.as_bytes()) show_nonprintable: false,
.language("rust") term_width: console::Term::stdout().size().1 as usize,
.tab_width(Some(4)) tab_width: 4,
.true_color(false) colored_output: true,
.header(false) true_color: false,
.line_numbers(false) style_components: StyleComponents::new(&[]),
.grid(false); wrapping_mode: WrappingMode::default(),
if let Some(theme) = theme { paging_mode: if config.pager {
pretty_printer.theme(theme); PagingMode::QuitIfOneScreen
} } else {
if config.pager { PagingMode::Never
pretty_printer.paging_mode(PagingMode::QuitIfOneScreen); },
} visible_lines: VisibleLines::Ranges(LineRanges::all()),
theme: theme.unwrap_or_else(String::new),
syntax_mapping: SyntaxMapping::empty(),
pager: None,
use_italic_text: false,
highlighted_lines: HighlightedLineRanges(LineRanges::none()),
use_custom_assets: false,
..Default::default()
};
let assets = HighlightingAssets::from_binary();
let controller = bat::controller::Controller::new(&config, &assets);
let inputs = vec![bat::input::Input::from_reader(Box::new(content.as_bytes()))];
// Ignore any errors. // Ignore any errors.
let _ = pretty_printer.print(); let _ = controller.run(inputs, None);
} else { } else {
let _ = write!(io::stdout(), "{}", content); let _ = write!(io::stdout(), "{}", content);
} }