From d1524a5a6ff7e07469ec226376f51d99e3a01a96 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 19 Jun 2020 03:35:05 +0100 Subject: refactor(cheddar): Pass references readers & writers into renderers This paves the way for using other things than stdin/stdout as sources/sinks, which is required for example for implementing a syntect_server replacement based on cheddar. Change-Id: I5779db8dbf7b7ced109c26b940f721d237d60785 Reviewed-on: https://cl.tvl.fyi/c/depot/+/491 Reviewed-by: glittershark Reviewed-by: lukegb --- tools/cheddar/src/main.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/tools/cheddar/src/main.rs b/tools/cheddar/src/main.rs index 9e16b1de439b..5b16fc4aafa8 100644 --- a/tools/cheddar/src/main.rs +++ b/tools/cheddar/src/main.rs @@ -8,7 +8,7 @@ use std::collections::HashMap; use std::env; use std::ffi::OsStr; use std::io::BufRead; -use std::io::Read; +use std::io::Write; use std::io; use std::path::Path; use syntect::dumps::from_binary; @@ -182,12 +182,11 @@ fn format_callout_paragraph(callout: Callout) -> NodeValue { }) } -fn format_markdown() { +fn format_markdown(reader: &mut R, writer: &mut W) { let document = { let mut buffer = String::new(); - let stdin = io::stdin(); - let mut stdin = stdin.lock(); - stdin.read_to_string(&mut buffer).expect("failed to read stdin"); + reader.read_to_string(&mut buffer).expect("reading should work"); + drop(reader); buffer }; @@ -228,17 +227,15 @@ fn format_markdown() { } }); - format_html(root, &MD_OPTS, &mut io::stdout()) + format_html(root, &MD_OPTS, writer) .expect("Markdown rendering failed"); } -fn format_code(args: &Args) { - let stdin = io::stdin(); - let mut stdin = stdin.lock(); +fn format_code(reader: &mut R, writer: &mut W, args: &Args) { let mut linebuf = String::new(); // Get the first line, we might need it for syntax identification. - let mut read_result = stdin.read_line(&mut linebuf); + let mut read_result = reader.read_line(&mut linebuf); // Set up the highlighter let theme = &THEMES.themes["InspiredGitHub"]; @@ -272,15 +269,15 @@ fn format_code(args: &Args) { // immediately output the current state to avoid keeping // things in memory - print!("{}", outbuf); + write!(writer, "{}", outbuf).expect("write should not fail"); // merry go round again linebuf.clear(); outbuf.clear(); - read_result = stdin.read_line(&mut linebuf); + read_result = reader.read_line(&mut linebuf); } - println!(""); + writeln!(writer, "").expect("write should not fail"); } fn main() { @@ -311,8 +308,14 @@ fn main() { .and_then(OsStr::to_str) .map(|s| s.to_string()); + let stdin = io::stdin(); + let mut in_handle = stdin.lock(); + + let stdout = io::stdout(); + let mut out_handle = stdout.lock(); + match args.extension.as_ref().map(String::as_str) { - Some("md") if args.about_filter => format_markdown(), - _ => format_code(&args), + Some("md") if args.about_filter => format_markdown(&mut in_handle, &mut out_handle), + _ => format_code(&mut in_handle, &mut out_handle, &args), } } -- cgit 1.4.1