From 0fa1ae215ac1434591968818f8f75ad5b35d52ec Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 4 Mar 2023 09:02:57 +0800 Subject: feat: support highlight reply markdown (#3) * feat: support highlight reply markdown * migrate markdown highlighter from termimad to mdcat * optimize render No need to clear screen when there is no newline in reply token * handle ctrl-c when rendering stream * update readme * fix ctrlc don't abort acquire_stream when establish connection * ensure render_stream's dtect_ctrlc is exit before next readline This will make reedline don't throw err 'The cursor position could not be read within a normal duration' --- src/main.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index ddd856e..893850a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,20 @@ mod cli; mod client; mod config; +mod render; mod repl; -use std::process::exit; use std::sync::Arc; +use std::{io::stdout, process::exit}; use cli::Cli; use client::ChatGptClient; use config::{Config, Role}; +use is_terminal::IsTerminal; use anyhow::{anyhow, Result}; use clap::Parser; +use render::MarkdownRender; use repl::{Repl, ReplCmdHandler}; fn main() { @@ -52,8 +55,15 @@ fn start_directive( ) -> Result<()> { let mut file = config.open_message_file()?; let output = client.acquire(input, role.map(|v| v.prompt))?; - println!("{}", output.trim()); - Config::save_message(file.as_mut(), input, &output); + let output = output.trim(); + if config.highlight && stdout().is_terminal() { + let markdown_render = MarkdownRender::init()?; + markdown_render.print(output)?; + } else { + println!("{output}"); + } + + Config::save_message(file.as_mut(), input, output); Ok(()) } -- cgit v1.2.3