From b276dfedd2d13de9918b9b87a3d543e7b5eca95e Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 30 Oct 2023 10:07:01 +0800 Subject: feat: support textwrap (#171) * feat: support textwrap * improve cmd_render_stream * done cmd_render_stream * add `-w` alias to `--wrap` * done repl_render_stream * add `config.wrap_code` * remove cached config.wrap_width * fix unxpected duplicate lines on kitty * refactor markdown render * improve render * fix test --- src/repl/handler.rs | 51 ++++++++++++++------------------------------------- src/repl/prompt.rs | 6 +++--- 2 files changed, 17 insertions(+), 40 deletions(-) (limited to 'src/repl') diff --git a/src/repl/handler.rs b/src/repl/handler.rs index fd157dc..35a57da 100644 --- a/src/repl/handler.rs +++ b/src/repl/handler.rs @@ -115,9 +115,9 @@ impl ReplCmdHandler { } ReplCmd::ReadFile(file) => { let mut contents = String::new(); - let mut file = fs::File::open(file).expect("Unable to open file"); + let mut file = fs::File::open(file).with_context(|| "Unable to open file")?; file.read_to_string(&mut contents) - .expect("Unable to read file"); + .with_context(|| "Unable to read file")?; self.handle(ReplCmd::Submit(contents))?; } } @@ -137,23 +137,17 @@ impl ReplCmdHandler { #[allow(clippy::module_name_repetitions)] pub struct ReplyStreamHandler { - sender: Option>, + sender: Sender, buffer: String, abort: SharedAbortSignal, - repl: bool, } impl ReplyStreamHandler { - pub fn new( - sender: Option>, - repl: bool, - abort: SharedAbortSignal, - ) -> Self { + pub fn new(sender: Sender, abort: SharedAbortSignal) -> Self { Self { sender, abort, buffer: String::new(), - repl, } } @@ -162,37 +156,20 @@ impl ReplyStreamHandler { return Ok(()); } self.buffer.push_str(text); - match self.sender.as_ref() { - Some(tx) => { - let ret = tx - .send(ReplyStreamEvent::Text(text.to_string())) - .with_context(|| "Failed to send StreamEvent:Text"); - self.safe_ret(ret)?; - } - None => { - print_now!("{}", text); - } - } + let ret = self + .sender + .send(ReplyStreamEvent::Text(text.to_string())) + .with_context(|| "Failed to send StreamEvent:Text"); + self.safe_ret(ret)?; Ok(()) } pub fn done(&mut self) -> Result<()> { - if let Some(tx) = self.sender.as_ref() { - let ret = tx - .send(ReplyStreamEvent::Done) - .with_context(|| "Failed to send StreamEvent:Done"); - self.safe_ret(ret)?; - } else { - if !self.buffer.ends_with('\n') { - print_now!("\n"); - } - if self.repl { - print_now!("\n"); - if cfg!(macos) { - print_now!("\n"); - } - } - } + let ret = self + .sender + .send(ReplyStreamEvent::Done) + .with_context(|| "Failed to send StreamEvent:Done"); + self.safe_ret(ret)?; Ok(()) } diff --git a/src/repl/prompt.rs b/src/repl/prompt.rs index be3763b..f48ce64 100644 --- a/src/repl/prompt.rs +++ b/src/repl/prompt.rs @@ -41,15 +41,15 @@ impl ReplPrompt { } pub fn get_colors(config: &SharedConfig) -> (Color, nu_ansi_term::Color, Color, Color) { - let (highlight, light_theme) = config.read().get_render_options(); - if highlight { + let render_options = config.read().get_render_options(); + if render_options.highlight { ( PROMPT_COLOR, PROMPT_MULTILINE_COLOR, INDICATOR_COLOR, PROMPT_RIGHT_COLOR, ) - } else if light_theme { + } else if render_options.light_theme { ( Color::Black, nu_ansi_term::Color::Black, -- cgit v1.2.3