use std::cell::RefCell;
use promkit_widgets::{
jsonstream::{self, format::RowFormatter, JsonStream},
text::{self, Text},
};
use crate::{
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
switch::ActiveKeySwitcher,
Prompt,
};
pub mod keymap;
pub mod render;
pub struct Json {
keymap: ActiveKeySwitcher<keymap::Keymap>,
title_state: text::State,
json_state: jsonstream::State,
}
impl Json {
pub fn new(stream: JsonStream) -> Self {
Self {
title_state: text::State {
style: ContentStyle {
attributes: Attributes::from(Attribute::Bold),
..Default::default()
},
..Default::default()
},
json_state: jsonstream::State {
stream,
formatter: RowFormatter {
curly_brackets_style: ContentStyle {
attributes: Attributes::from(Attribute::Bold),
..Default::default()
},
square_brackets_style: ContentStyle {
attributes: Attributes::from(Attribute::Bold),
..Default::default()
},
key_style: ContentStyle {
foreground_color: Some(Color::DarkBlue),
..Default::default()
},
string_value_style: ContentStyle {
foreground_color: Some(Color::DarkGreen),
..Default::default()
},
number_value_style: ContentStyle::default(),
boolean_value_style: ContentStyle::default(),
null_value_style: ContentStyle {
foreground_color: Some(Color::DarkGrey),
..Default::default()
},
active_item_attribute: Attribute::Undercurled,
inactive_item_attribute: Attribute::Dim,
indent: 2,
},
lines: Default::default(),
},
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
}
}
pub fn title<T: AsRef<str>>(mut self, text: T) -> Self {
self.title_state.text = Text::from(text);
self
}
pub fn title_style(mut self, style: ContentStyle) -> Self {
self.title_state.style = style;
self
}
pub fn json_lines(mut self, lines: usize) -> Self {
self.json_state.lines = Some(lines);
self
}
pub fn indent(mut self, indent: usize) -> Self {
self.json_state.formatter.indent = indent;
self
}
pub fn active_item_attribute(mut self, attr: Attribute) -> Self {
self.json_state.formatter.active_item_attribute = attr;
self
}
pub fn inactive_item_attribute(mut self, attr: Attribute) -> Self {
self.json_state.formatter.inactive_item_attribute = attr;
self
}
pub fn register_keymap<K: AsRef<str>>(mut self, key: K, handler: keymap::Keymap) -> Self {
self.keymap = self.keymap.register(key, handler);
self
}
pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
Ok(Prompt {
renderer: render::Renderer {
keymap: RefCell::new(self.keymap),
title_state: self.title_state,
json_state: self.json_state,
},
})
}
}