Skip to content

Commit

Permalink
Refactored part of main() into new fn markdown_plaintext_or_error()
Browse files Browse the repository at this point in the history
  • Loading branch information
craigmayhew committed Jan 29, 2024
1 parent d327a28 commit 1a7016f
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ fn args_read (args_setup: Command) -> (std::string::String, i32, f32, f32, bool)
(prepend.to_owned(),max_tokens.to_owned(),temperature.to_owned(),top_p.to_owned(),render_markdown.to_owned())
}

/// # Render in Markdown, Plaintext or Error
///
/// Takes a result from the Reqwest API call
fn markdown_plaintext_or_error (gpt_result: Result<String, reqwest::Error>, render_markdown: bool) {
match gpt_result {
Ok(markdown) => {
if render_markdown {
let mut skin = MadSkin::default();
skin.code_block.left_margin = 4;
skin.code_block.set_fgbg(gray(17), gray(3));
skin.set_fg(Yellow);
skin.print_text(&markdown)
} else {
println!("{}", &markdown)
}
},
Err(e) => eprintln!("Error: {}", e),
}
}

/// # Entry Point for Application
///
/// - Initializes the application
Expand Down Expand Up @@ -221,18 +241,5 @@ async fn main() {
io::stdin().read_to_string(&mut input).expect("Failed to read from stdin");
}

match send_to_gpt4(&input, parsed_arguments).await {
Ok(markdown) => {
if render_markdown {
let mut skin = MadSkin::default();
skin.code_block.left_margin = 4;
skin.code_block.set_fgbg(gray(17), gray(3));
skin.set_fg(Yellow);
skin.print_text(&markdown)
} else {
println!("{}", &markdown)
}
},
Err(e) => eprintln!("Error: {}", e),
}
markdown_plaintext_or_error(send_to_gpt4(&input, parsed_arguments).await, render_markdown);
}

0 comments on commit 1a7016f

Please sign in to comment.