Skip to content

Commit

Permalink
Add markdown toggle
Browse files Browse the repository at this point in the history
Signed-off-by: Slendi <[email protected]>
  • Loading branch information
slendidev committed Feb 25, 2025
1 parent e319457 commit 2eb12e2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 60 deletions.
2 changes: 2 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Application {
pub model: String,
pub system_prompts: SystemPrompts,
pub active_system_prompt: String,
pub markdown: bool,
cli: CLI,
}

Expand All @@ -39,6 +40,7 @@ impl Application {
model: AVAILABLE_MODELS[0].to_owned(),
system_prompts: SystemPrompts::new(),
active_system_prompt: "".to_owned(),
markdown: true,
cli: CLI::new(),
};
app.active_system_prompt = match app
Expand Down
22 changes: 22 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl CommandRegistry {
self.register_command("system_edit", CommandSystemEdit);
self.register_command("system_remove", CommandSystemRemove);
self.register_command("system_use", CommandSystemUse);
self.register_command("markdown", CommandMarkdown);
}

pub fn execute_command(
Expand Down Expand Up @@ -441,3 +442,24 @@ impl Command for CommandSystemUse {
Ok(())
}
}

struct CommandMarkdown;
impl Command for CommandMarkdown {
fn handle_command(
&self,
_registry: &CommandRegistry,
_args: Vec<&str>,
app: Rc<RefCell<Application>>,
) -> Result<(), CommandError> {
let mut app = app.borrow_mut();
app.markdown = !app.markdown;
println!(
"Markdown parsing is now {}.",
match app.markdown {
true => "enabled",
false => "disabled",
}
);
return Ok(());
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ fn main() {
let response = app.tokio_rt.block_on(response::process_response(
Box::pin(stream),
&mut code_blocks,
!app.markdown,
));

app.code_blocks = code_blocks;
Expand Down
125 changes: 65 additions & 60 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tokio_stream::StreamExt;
pub async fn process_response(
stream: Pin<Box<dyn tokio_stream::Stream<Item = Result<String, Error>>>>,
code_blocks: &mut Vec<String>,
raw: bool,
) -> Result<String, Error> {
tokio::pin!(stream);

Expand All @@ -21,81 +22,85 @@ pub async fn process_response(
while let Some(chunk) = stream.next().await {
match chunk {
Ok(content) => {
let mut chars = content.chars().peekable();
if raw {
print!("{}", content);
} else {
let mut chars = content.chars().peekable();

while let Some(ch) = chars.next() {
if language_reading {
if ch == '\n' {
language_reading = false;
} else {
language.push(ch);
in_code_block = true;
}
} else if ch == '`' {
tick_count += 1;
if tick_count == 3 {
tick_count = 0;
while let Some(ch) = chars.next() {
if language_reading {
if ch == '\n' {
language_reading = false;
} else {
language.push(ch);
in_code_block = true;
}
} else if ch == '`' {
tick_count += 1;
if tick_count == 3 {
tick_count = 0;

if in_code_block {
in_code_block = false;
code_blocks.push(current_code_block_content.clone());
if in_code_block {
in_code_block = false;
code_blocks.push(current_code_block_content.clone());

println!("```{}", language);
if stdout_is_terminal {
let mut language = language.trim().to_owned();
if language == "csharp" {
language = "c#".to_owned();
} else if language == "fsharp" {
language = "f#".to_owned();
}
println!("```{}", language);
if stdout_is_terminal {
let mut language = language.trim().to_owned();
if language == "csharp" {
language = "c#".to_owned();
} else if language == "fsharp" {
language = "f#".to_owned();
}

let mut pp = PrettyPrinter::new();
pp.input_from_bytes(current_code_block_content.as_bytes())
.colored_output(true);

let mut pp = PrettyPrinter::new();
pp.input_from_bytes(current_code_block_content.as_bytes())
.colored_output(true);
if !language.is_empty() {
pp.language(&language);
}

if !language.is_empty() {
pp.language(&language);
pp.print().unwrap();
} else {
println!("{}", current_code_block_content);
}
println!("```");

pp.print().unwrap();
current_code_block_content.clear();
language.clear();
} else {
println!("{}", current_code_block_content);
in_code_block = true;
language_reading = true;
language.clear();
}
println!("```");

current_code_block_content.clear();
language.clear();
} else {
in_code_block = true;
language_reading = true;
language.clear();
}
}
} else {
if tick_count > 0 {
full_response.push_str(&"`".repeat(tick_count));
if stdout_is_terminal {
print!("{}", "`".repeat(tick_count));
io::stdout().flush().await.unwrap();
} else {
if tick_count > 0 {
full_response.push_str(&"`".repeat(tick_count));
if stdout_is_terminal {
print!("{}", "`".repeat(tick_count));
io::stdout().flush().await.unwrap();
}
tick_count = 0;
}
tick_count = 0;
}

if in_code_block {
if language.is_empty() {
if ch == '\n' {
language = " ".to_string();
if in_code_block {
if language.is_empty() {
if ch == '\n' {
language = " ".to_string();
} else {
language.push(ch);
}
} else {
language.push(ch);
current_code_block_content.push(ch);
}
} else {
current_code_block_content.push(ch);
}
} else {
full_response.push(ch);
if stdout_is_terminal {
print!("{}", ch);
io::stdout().flush().await.unwrap();
full_response.push(ch);
if stdout_is_terminal {
print!("{}", ch);
io::stdout().flush().await.unwrap();
}
}
}
}
Expand Down

0 comments on commit 2eb12e2

Please sign in to comment.