mod content;
mod lines;
mod print;
mod style;
use super::{PagerConfig, run};
use crate::config::{HistoryLocationSource, ResolvedHistoryLocation};
use content::schema_content;
use lines::generate_schema_lines;
use print::{print_schema_colored, print_schema_plain};
use std::io::{self, IsTerminal};
use std::path::Path;
#[derive(Debug)]
pub struct HistoryDirError {
detail: Option<String>,
}
impl std::fmt::Display for HistoryDirError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "No persistent history directory is available")?;
match &self.detail {
Some(detail) => write!(f, ": {detail}"),
None => Ok(()),
}
}
}
impl std::error::Error for HistoryDirError {}
fn history_directory(location: &ResolvedHistoryLocation) -> Result<&Path, HistoryDirError> {
if location.source() == HistoryLocationSource::Volatile {
return Err(HistoryDirError { detail: None });
}
location.directory().ok_or_else(|| HistoryDirError {
detail: location.probe_failure().map(ToString::to_string),
})
}
pub fn print_schema(location: &ResolvedHistoryLocation) -> Result<(), HistoryDirError> {
let history_path = history_directory(location)?;
if io::stdout().is_terminal() {
print_schema_colored(history_path);
} else {
print_schema_plain(history_path);
}
Ok(())
}
pub fn show_schema_pager(location: &ResolvedHistoryLocation) -> Result<(), HistoryDirError> {
let history_path = history_directory(location)?;
let lines = generate_schema_lines(history_path);
let mut content = schema_content(lines);
let config = PagerConfig {
title: "History Schema",
footer_hint: "↑↓/jk scroll │ c copy R example │ q exit",
manage_alternate_screen: true,
};
if let Err(e) = run(&mut content, &config) {
eprintln!("Pager error: {}", e);
}
Ok(())
}
#[cfg(test)]
mod tests;