use std::io;
use crate::command::chat::markdown::highlight::highlight_code_line;
use crate::command::chat::theme::{Theme, ThemeName};
use crate::tui::editor_core::{EditorTheme, HighlightFn, ThemeGalleryItem};
use crate::tui::editor_core::{
open_markdown_editor as core_open, open_markdown_editor_on_terminal as core_open_on_terminal,
open_markdown_editor_with_content as core_open_with_content,
};
impl From<&Theme> for EditorTheme {
fn from(t: &Theme) -> Self {
EditorTheme {
bg_primary: t.bg_primary,
bg_input: t.bg_input,
code_bg: t.code_bg,
cursor_fg: t.cursor_fg,
cursor_bg: t.cursor_bg,
text_normal: t.text_normal,
text_dim: t.text_dim,
text_bold: t.text_bold,
md_h1: t.md_h1,
md_h2: t.md_h2,
md_h3: t.md_h3,
md_h4: t.md_h4,
md_link: t.md_link,
md_list_bullet: t.md_list_bullet,
md_blockquote_bar: t.md_blockquote_bar,
md_blockquote_bg: t.md_blockquote_bg,
md_blockquote_text: t.md_blockquote_text,
md_inline_code_fg: t.md_inline_code_fg,
md_inline_code_bg: t.md_inline_code_bg,
code_default: t.code_default,
code_keyword: t.code_keyword,
code_string: t.code_string,
code_comment: t.code_comment,
code_number: t.code_number,
code_type: t.code_type,
code_primitive: t.code_primitive,
code_macro: t.code_macro,
code_lifetime: t.code_lifetime,
code_attribute: t.code_attribute,
code_shell_var: t.code_shell_var,
}
}
}
fn bridge_highlight(
line: &str,
lang: &str,
theme: &EditorTheme,
) -> Vec<ratatui::text::Span<'static>> {
highlight_code_line(line, lang, theme)
}
fn build_theme_gallery() -> Vec<ThemeGalleryItem> {
ThemeName::all()
.iter()
.map(|name| {
let theme = Theme::from_name(name);
let editor_theme = EditorTheme::from(&theme);
(name.display_name(), editor_theme)
})
.collect()
}
pub fn open_markdown_editor_on_terminal(
terminal: &mut ratatui::Terminal<ratatui::backend::CrosstermBackend<io::Stdout>>,
title: &str,
content: &str,
theme: &Theme,
) -> io::Result<Option<String>> {
let editor_theme = EditorTheme::from(theme);
core_open_on_terminal(
terminal,
title,
content,
&editor_theme,
bridge_highlight as HighlightFn,
build_theme_gallery(),
)
}
pub fn open_markdown_editor(
title: &str,
content: &str,
theme: &Theme,
) -> io::Result<Option<String>> {
let editor_theme = EditorTheme::from(theme);
core_open(
title,
content,
&editor_theme,
bridge_highlight as HighlightFn,
build_theme_gallery(),
)
}
pub fn open_markdown_editor_with_content(
title: &str,
initial_lines: &[String],
theme: &Theme,
) -> io::Result<Option<String>> {
let editor_theme = EditorTheme::from(theme);
core_open_with_content(
title,
initial_lines,
&editor_theme,
bridge_highlight as HighlightFn,
build_theme_gallery(),
)
}
pub fn open_script_editor(title: &str, initial_lines: &[String]) -> io::Result<Option<String>> {
let theme = Theme::dark();
let editor_theme = EditorTheme::from(&theme);
core_open_with_content(
title,
initial_lines,
&editor_theme,
bridge_highlight as HighlightFn,
build_theme_gallery(),
)
}