summaryrefslogtreecommitdiffstats
path: root/src/repl
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-10-30 10:07:01 +0800
committerGitHub <noreply@github.com>2023-10-30 10:07:01 +0800
commitb276dfedd2d13de9918b9b87a3d543e7b5eca95e (patch)
tree33e1f864ef38e45ebc297e15ce519ee8105b8757 /src/repl
parent632a6fcc0bfb5b33858cd0fa3876e43ce2073cb3 (diff)
downloadaichat-b276dfedd2d13de9918b9b87a3d543e7b5eca95e.tar.gz
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
Diffstat (limited to 'src/repl')
-rw-r--r--src/repl/handler.rs51
-rw-r--r--src/repl/prompt.rs6
2 files changed, 17 insertions, 40 deletions
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<ReplyStreamEvent>>,
+ sender: Sender<ReplyStreamEvent>,
buffer: String,
abort: SharedAbortSignal,
- repl: bool,
}
impl ReplyStreamHandler {
- pub fn new(
- sender: Option<Sender<ReplyStreamEvent>>,
- repl: bool,
- abort: SharedAbortSignal,
- ) -> Self {
+ pub fn new(sender: Sender<ReplyStreamEvent>, 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,