#![warn(missing_docs)]
pub mod themes;
pub use themes::{color::Color, get_default_theme, ElementTheme, TextStyle, Theme};
mod writer;
use std::io::{IsTerminal, Read};
use std::{
fs::File,
io::{self},
path::PathBuf,
};
#[derive(Debug, PartialEq)]
pub enum ColorChoice {
Auto,
Always,
Never,
}
pub fn render_file_to_stdout(
file_path: &PathBuf,
theme: Option<&self::Theme>,
color_choice: ColorChoice,
) -> Result<(), std::io::Error> {
let mut stdout = std::io::stdout().lock();
let should_colorize = match color_choice {
ColorChoice::Always => true,
ColorChoice::Never => false,
ColorChoice::Auto => stdout.is_terminal(),
};
render_file(file_path, theme, &mut stdout, should_colorize)
}
pub fn render_file(
file_path: &PathBuf,
theme: Option<&Theme>,
writer: &mut impl std::io::Write,
should_colorize: bool,
) -> Result<(), std::io::Error> {
let file = match File::open(file_path) {
Ok(f) => f,
Err(e) => {
panic!("Unable to open file: {e}");
}
};
let mut file_contents = String::new();
let _files = io::BufReader::new(file)
.read_to_string(&mut file_contents)
.unwrap();
render_text(&file_contents, theme, writer, should_colorize)
}
pub fn render_text_to_stdout(
text: &str,
theme: Option<&Theme>,
color_choice: ColorChoice,
) -> Result<(), std::io::Error> {
let mut stdout = std::io::stdout().lock();
let should_colorize = match color_choice {
ColorChoice::Always => true,
ColorChoice::Never => false,
ColorChoice::Auto => stdout.is_terminal(),
};
render_text(text, theme, &mut stdout, should_colorize)
}
pub fn render_text(
text: &str,
theme: Option<&Theme>,
writer: &mut impl std::io::Write,
should_colorize: bool,
) -> Result<(), std::io::Error> {
let default_theme = get_default_theme();
let theme = match theme {
Some(x) => x,
None => &default_theme,
};
writer::write(text, theme, writer, should_colorize)
}