Skip to content

Commit

Permalink
remove cached config.wrap_width
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Oct 30, 2023
1 parent e52b454 commit 78cf32a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
11 changes: 3 additions & 8 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ pub struct Config {
#[serde(skip)]
pub model_info: ModelInfo,
#[serde(skip)]
pub text_width: Option<u16>,
#[serde(skip)]
pub last_message: Option<(String, String)>,
}

Expand All @@ -102,7 +100,6 @@ impl Default for Config {
role: None,
session: None,
model_info: Default::default(),
text_width: None,
last_message: None,
}
}
Expand Down Expand Up @@ -315,15 +312,13 @@ impl Config {
pub fn set_wrap(&mut self, value: &str) -> Result<()> {
if value == "no" {
self.wrap = None;
self.text_width = None;
} else if value == "auto" {
self.wrap = Some(value.into());
self.text_width = Some(0);
} else {
let width = value
value
.parse::<u16>()
.map_err(|_| anyhow!("Invalid wrap value"))?;
self.text_width = Some(width);
self.wrap = Some(value.into())
}
Ok(())
}
Expand Down Expand Up @@ -543,7 +538,7 @@ impl Config {
RenderOptions::new(
self.highlight,
self.light_theme,
self.text_width,
self.wrap.clone(),
self.wrap_code,
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn start_directive(
let output = if no_stream {
let render_options = config.read().get_render_options();
let output = client.send_message(input)?;
let mut markdown_render = MarkdownRender::new(render_options);
let mut markdown_render = MarkdownRender::init(render_options)?;
println!("{}", markdown_render.render(&output).trim());
output
} else {
Expand Down
59 changes: 40 additions & 19 deletions src/render/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::{anyhow, Context, Result};
use crossterm::style::{Color, Stylize};
use crossterm::terminal;
use lazy_static::lazy_static;
use std::collections::HashMap;
use syntect::highlighting::{Color as SyntectColor, FontStyle, Style, Theme};
Expand Down Expand Up @@ -29,36 +31,55 @@ pub struct MarkdownRender {
md_syntax: SyntaxReference,
code_syntax: Option<SyntaxReference>,
prev_line_type: LineType,
wrap_width: Option<u16>,
options: RenderOptions,
}

impl MarkdownRender {
pub fn new(options: RenderOptions) -> Self {
let syntax_set: SyntaxSet =
bincode::deserialize_from(SYNTAXES).expect("invalid syntaxes binary");
pub fn init(options: RenderOptions) -> Result<Self> {
let syntax_set: SyntaxSet = bincode::deserialize_from(SYNTAXES)
.with_context(|| "MarkdownRender: invalid syntaxes binary")?;

let md_theme: Option<Theme> = match (options.highlight, options.light_theme) {
(false, _) => None,
(true, false) => {
Some(bincode::deserialize_from(MD_THEME).expect("invalid theme binary"))
}
(true, true) => {
Some(bincode::deserialize_from(MD_THEME_LIGHT).expect("invalid theme binary"))
}
(true, false) => Some(
bincode::deserialize_from(MD_THEME)
.with_context(|| "MarkdownRender: invalid theme binary")?,
),
(true, true) => Some(
bincode::deserialize_from(MD_THEME_LIGHT)
.expect("MarkdownRender: invalid theme binary"),
),
};
let code_color = md_theme.as_ref().map(get_code_color);
let md_syntax = syntax_set.find_syntax_by_extension("md").unwrap().clone();
let line_type = LineType::Normal;

Self {
let wrap_width = match options.wrap.as_deref() {
None => None,
Some("auto") => {
let (columns, _) =
terminal::size().with_context(|| "Unable to get terminal size")?;
Some(columns)
}
Some(value) => {
let (columns, _) =
terminal::size().with_context(|| "Unable to get terminal size")?;
let value = value
.parse::<u16>()
.map_err(|_| anyhow!("Invalid wrap value"))?;
Some(columns.min(value))
}
};
Ok(Self {
syntax_set,
md_theme,
code_color,
md_syntax,
code_syntax: None,
prev_line_type: line_type,
wrap_width,
options,
}
})
}

pub fn render(&mut self, text: &str) -> String {
Expand Down Expand Up @@ -141,7 +162,7 @@ impl MarkdownRender {
}

fn wrap_line(&self, line: String, is_code: bool) -> String {
if let Some(width) = self.options.text_width {
if let Some(width) = self.wrap_width {
if is_code && !self.options.wrap_code {
return line;
}
Expand All @@ -167,7 +188,7 @@ impl MarkdownRender {
pub struct RenderOptions {
pub highlight: bool,
pub light_theme: bool,
pub text_width: Option<u16>,
pub wrap: Option<String>,
pub wrap_code: bool,
}

Expand All @@ -176,7 +197,7 @@ impl Default for RenderOptions {
Self {
highlight: true,
light_theme: false,
text_width: None,
wrap: None,
wrap_code: false,
}
}
Expand All @@ -186,13 +207,13 @@ impl RenderOptions {
pub(crate) fn new(
highlight: bool,
light_theme: bool,
text_width: Option<u16>,
wrap: Option<String>,
wrap_code: bool,
) -> Self {
Self {
highlight,
light_theme,
text_width,
wrap,
wrap_code,
}
}
Expand Down Expand Up @@ -298,7 +319,7 @@ fn unzip_file(path: &str, output_dir: &str) -> Result<(), Box<dyn std::error::Er
#[test]
fn test_render() {
let options = RenderOptions::default();
let render = MarkdownRender::new(options);
let render = MarkdownRender::init(options).unwrap();
assert!(render.find_syntax("csharp").is_some());
}

Expand All @@ -308,7 +329,7 @@ fn unzip_file(path: &str, output_dir: &str) -> Result<(), Box<dyn std::error::Er
highlight: false,
..Default::default()
};
let mut render = MarkdownRender::new(options);
let mut render = MarkdownRender::init(options).unwrap();
let output = render.render(TEXT);
assert_eq!(TEXT, output);
}
Expand Down
16 changes: 9 additions & 7 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ pub fn render_stream(
let (tx, rx) = unbounded();
let abort_clone = abort.clone();
spawn(move || {
let err = if repl {
let mut render = MarkdownRender::new(render_options);
repl_render_stream(&rx, &mut render, &abort)
} else {
let mut render = MarkdownRender::new(render_options);
cmd_render_stream(&rx, &mut render, &abort)
let run = move || {
if repl {
let mut render = MarkdownRender::init(render_options)?;
repl_render_stream(&rx, &mut render, &abort)
} else {
let mut render = MarkdownRender::init(render_options)?;
cmd_render_stream(&rx, &mut render, &abort)
}
};
if let Err(err) = err {
if let Err(err) = run() {
let err = format!("{err:?}");
print_now!("{}\n\n", err.trim());
}
Expand Down

0 comments on commit 78cf32a

Please sign in to comment.