Skip to content

Commit

Permalink
Merge pull request #11 from fuyufjh/eric/fix_window_prompt_display
Browse files Browse the repository at this point in the history
fix prompt display under Windows
  • Loading branch information
fuyufjh authored May 28, 2023
2 parents b849470 + da5205a commit 1df1631
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ use clap::Parser;
use console::style;
use futures::stream::StreamExt;
use log::{debug, trace};
use repl_helper::ReplHelper;
use reqwest::header::{HeaderMap, AUTHORIZATION};
use reqwest::{Client, RequestBuilder, StatusCode};
use reqwest_eventsource::{Event, EventSource};
use rustyline::error::ReadlineError;
use rustyline::{DefaultEditor, Editor};
use rustyline::Editor;
use std::io::Write;

mod model;
mod repl_helper;
mod spinner;

use model::*;
Expand Down Expand Up @@ -146,7 +148,8 @@ impl Session {
}

pub async fn run_interactive(&mut self) -> Result<()> {
let mut rl = DefaultEditor::new()?;
let mut rl = Editor::<repl_helper::ReplHelper, _>::new()?;
rl.set_helper(Some(ReplHelper::default()));

// Persist input history in `$HOME/.heygpt_history`
let history_file = {
Expand Down Expand Up @@ -211,7 +214,7 @@ impl Session {
I: rustyline::history::History,
{
loop {
let readline = rl.readline(&format!("{} => ", style(role).bold().cyan()));
let readline = rl.readline(&format!("{} => ", role));
match readline {
Ok(line) => {
if line.is_empty() {
Expand Down
44 changes: 44 additions & 0 deletions src/repl_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::borrow::Cow;

use console::style;
use rustyline::completion::Completer;
use rustyline::highlight::Highlighter;
use rustyline::hint::Hinter;
use rustyline::validate::{ValidationContext, ValidationResult, Validator};
use rustyline::Helper;

/// The rustyline helper for interactive mode.
/// Currently it's mostly for highlighting the prompt correctly.
#[derive(Clone, Debug, Default)]
pub struct ReplHelper;

impl Helper for ReplHelper {}

impl Validator for ReplHelper {
fn validate(&self, _ctx: &mut ValidationContext) -> rustyline::Result<ValidationResult> {
Ok(ValidationResult::Valid(None))
}
}

impl Completer for ReplHelper {
type Candidate = String;
}

impl Hinter for ReplHelper {
type Hint = String;
}

impl Highlighter for ReplHelper {
fn highlight_prompt<'b, 's: 'b, 'p: 'b>(
&'s self,
prompt: &'p str,
_default: bool,
) -> Cow<'b, str> {
if let Some(idx) = prompt.find(" => ") {
let (role, rest) = prompt.split_at(idx);
Cow::Owned(format!("{}{}", style(role).bold().cyan(), rest))
} else {
Cow::Borrowed(prompt)
}
}
}

0 comments on commit 1df1631

Please sign in to comment.