use super::lines::extract_r_code_block;
use super::style::{StyleState, style_line_to_ratatui};
use crate::pager::lines_content::{CopyAction, LineStyler, LinesContent};
use ratatui::text::Line;
use std::cell::Cell;
pub(super) struct SchemaStyler {
style_state: Cell<StyleState>,
}
impl SchemaStyler {
fn new() -> Self {
Self {
style_state: Cell::new(StyleState::new()),
}
}
}
impl LineStyler for SchemaStyler {
type Line = String;
fn style_line(&self, lines: &[String], index: usize) -> Line<'static> {
let line = &lines[index];
let mut state = self.style_state.get();
let styled = style_line_to_ratatui(line, &state);
state.update(line);
self.style_state.set(state);
styled
}
fn prepare_render(&mut self, lines: &[String], scroll_offset: usize) {
let mut state = StyleState::new();
for line in lines.iter().take(scroll_offset) {
state.update(line);
}
self.style_state.set(state);
}
}
pub(super) type SchemaContent = LinesContent<SchemaStyler>;
pub(super) fn schema_content(lines: Vec<String>) -> SchemaContent {
let copy = CopyAction {
text: extract_r_code_block(&lines),
copied_label: "Copied R example to clipboard",
};
LinesContent::new(lines, SchemaStyler::new(), Some(copy))
}