mirror of
https://github.com/sigoden/aichat
synced 2024-11-18 09:28:27 +00:00
refactor: replace dump with print_now! (#35)
This commit is contained in:
parent
b7cb6f89f1
commit
f694a59dcc
@ -163,9 +163,9 @@ impl Config {
|
||||
Some(role) => {
|
||||
let output = format!("{}>> {}", role.name, role.prompt.trim());
|
||||
self.role = Some(role);
|
||||
format!("{}\n", output.trim())
|
||||
output
|
||||
}
|
||||
None => "Error: Unknown role\n".into(),
|
||||
None => "Error: Unknown role".into(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -278,7 +278,7 @@ impl Config {
|
||||
let value = value.parse().with_context(|| "Invalid value")?;
|
||||
self.dry_run = value;
|
||||
}
|
||||
_ => return Ok(format!("Error: Unknown key `{key}`\n")),
|
||||
_ => return Ok(format!("Error: Unknown key `{key}`")),
|
||||
}
|
||||
Ok("".into())
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ mod config;
|
||||
mod render;
|
||||
mod repl;
|
||||
mod term;
|
||||
#[macro_use]
|
||||
mod utils;
|
||||
|
||||
use std::cell::RefCell;
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::MarkdownRender;
|
||||
use crate::print_now;
|
||||
use crate::repl::{ReplyStreamEvent, SharedAbortSignal};
|
||||
use crate::utils::dump;
|
||||
|
||||
use anyhow::Result;
|
||||
use crossbeam::channel::Receiver;
|
||||
@ -20,7 +20,7 @@ pub fn cmd_render_stream(rx: Receiver<ReplyStreamEvent>, abort: SharedAbortSigna
|
||||
let mut lines: Vec<&str> = text.split('\n').collect();
|
||||
buffer = lines.pop().unwrap_or_default().to_string();
|
||||
let output = lines.join("\n");
|
||||
dump(markdown_render.render(&output), 1);
|
||||
print_now!("{}\n", markdown_render.render(&output))
|
||||
} else {
|
||||
buffer = format!("{buffer}{text}");
|
||||
if !(markdown_render.is_code_block()
|
||||
@ -30,7 +30,7 @@ pub fn cmd_render_stream(rx: Receiver<ReplyStreamEvent>, abort: SharedAbortSigna
|
||||
|| buffer.starts_with('|'))
|
||||
{
|
||||
if let Some((output, remain)) = split_line(&buffer) {
|
||||
dump(markdown_render.render_line_stateless(&output), 0);
|
||||
print_now!("{}", markdown_render.render_line_stateless(&output));
|
||||
buffer = remain
|
||||
}
|
||||
}
|
||||
@ -38,7 +38,7 @@ pub fn cmd_render_stream(rx: Receiver<ReplyStreamEvent>, abort: SharedAbortSigna
|
||||
}
|
||||
ReplyStreamEvent::Done => {
|
||||
let output = markdown_render.render(&buffer);
|
||||
dump(output, 2);
|
||||
print_now!("{}\n\n", output);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::client::ChatGptClient;
|
||||
use crate::config::SharedConfig;
|
||||
use crate::print_now;
|
||||
use crate::render::render_stream;
|
||||
use crate::utils::dump;
|
||||
|
||||
use anyhow::Result;
|
||||
use crossbeam::channel::Sender;
|
||||
@ -67,23 +67,28 @@ impl ReplCmdHandler {
|
||||
}
|
||||
ReplCmd::SetRole(name) => {
|
||||
let output = self.config.borrow_mut().change_role(&name);
|
||||
dump(output, 1);
|
||||
print_now!("{}\n\n", output.trim_end());
|
||||
}
|
||||
ReplCmd::ClearRole => {
|
||||
self.config.borrow_mut().role = None;
|
||||
dump("", 1);
|
||||
print_now!("\n");
|
||||
}
|
||||
ReplCmd::Prompt(prompt) => {
|
||||
self.config.borrow_mut().create_temp_role(&prompt);
|
||||
dump("", 1);
|
||||
print_now!("\n");
|
||||
}
|
||||
ReplCmd::Info => {
|
||||
let output = self.config.borrow().info()?;
|
||||
dump(output, 1);
|
||||
print_now!("{}\n\n", output.trim_end());
|
||||
}
|
||||
ReplCmd::UpdateConfig(input) => {
|
||||
let output = self.config.borrow_mut().update(&input)?;
|
||||
dump(output, 1);
|
||||
let output = output.trim();
|
||||
if output.is_empty() {
|
||||
print_now!("\n");
|
||||
} else {
|
||||
print_now!("{}\n\n", output);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@ -111,7 +116,7 @@ impl ReplyStreamHandler {
|
||||
let _ = tx.send(ReplyStreamEvent::Text(text.to_string()));
|
||||
}
|
||||
None => {
|
||||
dump(text, 0);
|
||||
print_now!("{}", text);
|
||||
}
|
||||
}
|
||||
self.buffer.push_str(text);
|
||||
@ -123,7 +128,11 @@ impl ReplyStreamHandler {
|
||||
let _ = tx.send(ReplyStreamEvent::Done);
|
||||
}
|
||||
None => {
|
||||
dump("", 2);
|
||||
if self.buffer.ends_with('\n') {
|
||||
print_now!("\n");
|
||||
} else {
|
||||
print_now!("\n\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,8 @@ mod init;
|
||||
|
||||
use crate::client::ChatGptClient;
|
||||
use crate::config::SharedConfig;
|
||||
use crate::print_now;
|
||||
use crate::term;
|
||||
use crate::utils::dump;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use reedline::{DefaultPrompt, Reedline, Signal};
|
||||
@ -36,11 +36,8 @@ impl Repl {
|
||||
pub fn run(&mut self, client: ChatGptClient, config: SharedConfig) -> Result<()> {
|
||||
let abort = AbortSignal::new();
|
||||
let handler = ReplCmdHandler::init(client, config, abort.clone())?;
|
||||
dump(
|
||||
format!("Welcome to aichat {}", env!("CARGO_PKG_VERSION")),
|
||||
1,
|
||||
);
|
||||
dump("Type \".help\" for more information.", 1);
|
||||
print_now!("Welcome to aichat {}\n", env!("CARGO_PKG_VERSION"));
|
||||
print_now!("Type \".help\" for more information.\n");
|
||||
let mut already_ctrlc = false;
|
||||
let handler = Arc::new(handler);
|
||||
loop {
|
||||
@ -63,7 +60,7 @@ impl Repl {
|
||||
}
|
||||
Err(err) => {
|
||||
let err = format!("{err:?}");
|
||||
dump(err.trim(), 2);
|
||||
print_now!("{}\n\n", err.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -71,7 +68,7 @@ impl Repl {
|
||||
abort.set_ctrlc();
|
||||
if !already_ctrlc {
|
||||
already_ctrlc = true;
|
||||
dump("(To exit, press Ctrl+C again or Ctrl+D or type .exit)", 2);
|
||||
print_now!("(To exit, press Ctrl+C again or Ctrl+D or type .exit)\n\n");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@ -104,18 +101,18 @@ impl Repl {
|
||||
Some("history") => {
|
||||
let history = Box::new(self.editor.history_mut());
|
||||
history.clear().with_context(|| "Failed to clear history")?;
|
||||
dump("", 1);
|
||||
print_now!("\n");
|
||||
}
|
||||
Some("role") => handler.handle(ReplCmd::ClearRole)?,
|
||||
_ => dump_unknown_command(),
|
||||
},
|
||||
".history" => {
|
||||
self.editor.print_history()?;
|
||||
dump("", 1);
|
||||
print_now!("\n");
|
||||
}
|
||||
".role" => match args {
|
||||
Some(name) => handler.handle(ReplCmd::SetRole(name.to_string()))?,
|
||||
None => dump("Usage: .role <name>", 2),
|
||||
None => print_now!("Usage: .role <name>\n\n"),
|
||||
},
|
||||
".info" => {
|
||||
handler.handle(ReplCmd::Info)?;
|
||||
@ -123,7 +120,7 @@ impl Repl {
|
||||
".editor" => {
|
||||
let mut text = args.unwrap_or_default().to_string();
|
||||
if text.is_empty() {
|
||||
dump("Usage: .editor { <your multiline/paste content> }", 2);
|
||||
print_now!("Usage: .editor {{ <your multiline/paste content here> }}\n\n");
|
||||
} else {
|
||||
if text.starts_with('{') && text.ends_with('}') {
|
||||
text = text[1..text.len() - 1].to_string()
|
||||
@ -137,7 +134,7 @@ impl Repl {
|
||||
".prompt" => {
|
||||
let mut text = args.unwrap_or_default().to_string();
|
||||
if text.is_empty() {
|
||||
dump("Usage: .prompt { <your multiline/paste content> }.", 2);
|
||||
print_now!("Usage: .prompt {{ <your content here> }}.\n\n");
|
||||
} else {
|
||||
if text.starts_with('{') && text.ends_with('}') {
|
||||
text = text[1..text.len() - 1].to_string()
|
||||
@ -156,10 +153,7 @@ impl Repl {
|
||||
}
|
||||
|
||||
fn dump_unknown_command() {
|
||||
dump(
|
||||
"Error: Unknown command. Type \".help\" for more information.",
|
||||
2,
|
||||
);
|
||||
print_now!("Error: Unknown command. Type \".help\" for more information.\n\n");
|
||||
}
|
||||
|
||||
fn dump_repl_help() {
|
||||
@ -168,8 +162,8 @@ fn dump_repl_help() {
|
||||
.map(|(name, desc, _)| format!("{name:<15} {desc}"))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n");
|
||||
dump(
|
||||
format!("{head}\n\nPress Ctrl+C to abort session, Ctrl+D to exit the REPL"),
|
||||
2,
|
||||
print_now!(
|
||||
"{}\n\nPress Ctrl+C to abort session, Ctrl+D to exit the REPL\n\n",
|
||||
head,
|
||||
);
|
||||
}
|
||||
|
11
src/utils.rs
11
src/utils.rs
@ -1,8 +1,15 @@
|
||||
use chrono::prelude::*;
|
||||
use std::io::{stdout, Write};
|
||||
|
||||
pub fn dump<T: ToString>(text: T, newlines: usize) {
|
||||
print!("{}{}", text.to_string(), "\n".repeat(newlines));
|
||||
#[macro_export]
|
||||
macro_rules! print_now {
|
||||
($($arg:tt)*) => {
|
||||
$crate::utils::print_now(&format!($($arg)*))
|
||||
};
|
||||
}
|
||||
|
||||
pub fn print_now<T: ToString>(text: T) {
|
||||
print!("{}", text.to_string());
|
||||
let _ = stdout().flush();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user