mirror of
https://github.com/sharkdp/bat
synced 2024-11-16 21:25:56 +00:00
parent
a1e1170319
commit
96cc391f2d
27
src/app.rs
27
src/app.rs
@ -181,16 +181,24 @@ impl App {
|
|||||||
Some("never") => false,
|
Some("never") => false,
|
||||||
Some("auto") | _ => self.interactive_output,
|
Some("auto") | _ => self.interactive_output,
|
||||||
},
|
},
|
||||||
paging: match self.matches.value_of("paging") {
|
paging_mode: match self.matches.value_of("paging") {
|
||||||
Some("always") => true,
|
Some("always") => PagingMode::Always,
|
||||||
Some("never") => false,
|
Some("never") => PagingMode::Never,
|
||||||
Some("auto") | _ => if files.contains(&None) {
|
Some("auto") | _ => if files.contains(&None) {
|
||||||
// If we are reading from stdin, only enable paging if we write to an
|
// If we are reading from stdin, only enable paging if we write to an
|
||||||
// interactive terminal and if we do not *read* from an interactive
|
// interactive terminal and if we do not *read* from an interactive
|
||||||
// terminal.
|
// terminal.
|
||||||
self.interactive_output && !atty::is(Stream::Stdin)
|
if self.interactive_output && !atty::is(Stream::Stdin) {
|
||||||
|
PagingMode::QuitIfOneScreen
|
||||||
|
} else {
|
||||||
|
PagingMode::Never
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.interactive_output
|
if self.interactive_output {
|
||||||
|
PagingMode::QuitIfOneScreen
|
||||||
|
} else {
|
||||||
|
PagingMode::Never
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
term_width: Term::stdout().size().1 as usize,
|
term_width: Term::stdout().size().1 as usize,
|
||||||
@ -230,13 +238,20 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum PagingMode {
|
||||||
|
Always,
|
||||||
|
QuitIfOneScreen,
|
||||||
|
Never,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Config<'a> {
|
pub struct Config<'a> {
|
||||||
pub true_color: bool,
|
pub true_color: bool,
|
||||||
pub output_wrap: OutputWrap,
|
pub output_wrap: OutputWrap,
|
||||||
pub output_components: OutputComponents,
|
pub output_components: OutputComponents,
|
||||||
pub language: Option<&'a str>,
|
pub language: Option<&'a str>,
|
||||||
pub colored_output: bool,
|
pub colored_output: bool,
|
||||||
pub paging: bool,
|
pub paging_mode: PagingMode,
|
||||||
pub term_width: usize,
|
pub term_width: usize,
|
||||||
pub files: Vec<Option<&'a str>>,
|
pub files: Vec<Option<&'a str>>,
|
||||||
pub theme: Option<&'a str>,
|
pub theme: Option<&'a str>,
|
||||||
|
35
src/main.rs
35
src/main.rs
@ -37,7 +37,7 @@ use syntect::easy::HighlightLines;
|
|||||||
use syntect::highlighting::Theme;
|
use syntect::highlighting::Theme;
|
||||||
use syntect::parsing::SyntaxDefinition;
|
use syntect::parsing::SyntaxDefinition;
|
||||||
|
|
||||||
use app::App;
|
use app::{App, PagingMode};
|
||||||
use assets::{config_dir, syntax_set_path, theme_set_path, HighlightingAssets};
|
use assets::{config_dir, syntax_set_path, theme_set_path, HighlightingAssets};
|
||||||
use diff::get_git_diff;
|
use diff::get_git_diff;
|
||||||
use printer::Printer;
|
use printer::Printer;
|
||||||
@ -59,12 +59,27 @@ enum OutputType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl OutputType {
|
impl OutputType {
|
||||||
fn pager() -> Result<Self> {
|
fn from_mode(mode: PagingMode) -> Self {
|
||||||
Ok(OutputType::Pager(Command::new("less")
|
use PagingMode::*;
|
||||||
.args(&["--quit-if-one-screen", "--RAW-CONTROL-CHARS", "--no-init"])
|
match mode {
|
||||||
|
Always => OutputType::try_pager(false),
|
||||||
|
QuitIfOneScreen => OutputType::try_pager(true),
|
||||||
|
_ => OutputType::stdout(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to launch the pager. Fall back to stdout in case of errors.
|
||||||
|
fn try_pager(quit_if_one_screen: bool) -> Self {
|
||||||
|
let mut args = vec!["--RAW-CONTROL-CHARS", "--no-init"];
|
||||||
|
if quit_if_one_screen {
|
||||||
|
args.push("--quit-if-one-screen");
|
||||||
|
}
|
||||||
|
Command::new("less")
|
||||||
|
.args(&args)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.spawn()
|
.spawn()
|
||||||
.chain_err(|| "Could not spawn pager")?))
|
.map(OutputType::Pager)
|
||||||
|
.unwrap_or_else(|_| OutputType::stdout())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stdout() -> Self {
|
fn stdout() -> Self {
|
||||||
@ -153,14 +168,6 @@ fn print_file(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_output_type(paging: bool) -> OutputType {
|
|
||||||
if paging {
|
|
||||||
OutputType::pager().unwrap_or_else(|_| OutputType::stdout())
|
|
||||||
} else {
|
|
||||||
OutputType::stdout()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run() -> Result<()> {
|
fn run() -> Result<()> {
|
||||||
let app = App::new();
|
let app = App::new();
|
||||||
|
|
||||||
@ -243,7 +250,7 @@ fn run() -> Result<()> {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut output_type = get_output_type(config.paging);
|
let mut output_type = OutputType::from_mode(config.paging_mode);
|
||||||
let handle = output_type.handle()?;
|
let handle = output_type.handle()?;
|
||||||
let mut printer = Printer::new(handle, &config);
|
let mut printer = Printer::new(handle, &config);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user