chore: better output

This commit is contained in:
iff 2024-11-19 21:16:34 +01:00
parent 551edc2828
commit cf9e6882e6
5 changed files with 100 additions and 24 deletions

View file

@ -64,7 +64,10 @@ fn main() {
let mut last_command = shell::last_command(&shell);
last_command = shell::expand_alias(&shell, &last_command);
let mut error_msg = command_output(&shell, &last_command);
error_msg = error_msg.split_whitespace().collect::<Vec<&str>>().join(" ");
error_msg = error_msg
.split_whitespace()
.collect::<Vec<&str>>()
.join(" ");
loop {
let suggestion = {
@ -93,7 +96,10 @@ fn main() {
} else {
last_command = suggestion;
error_msg = execution.err().unwrap();
error_msg = error_msg.split_whitespace().collect::<Vec<&str>>().join(" ");
error_msg = error_msg
.split_whitespace()
.collect::<Vec<&str>>()
.join(" ");
let retry_message = format!("{}...", t!("retry"));

View file

@ -23,6 +23,12 @@ pub struct AISuggest {
}
pub fn ai_suggestion(last_command: &str, error_msg: &str) -> Option<AISuggest> {
let error_msg = if error_msg.len() > 300 {
&error_msg[..300]
} else {
error_msg
};
let mut map = HashMap::new();
map.insert("last_command", last_command);
map.insert("error_msg", error_msg);
@ -57,9 +63,12 @@ pub fn ai_suggestion(last_command: &str, error_msg: &str) -> Option<AISuggest> {
Err(_) => "llama3-8b-8192".to_string(),
};
let user_locale = std::env::var("_PR_LOCALE").unwrap_or("en_US".to_string());
let set_locale = if user_locale != "en_US" {
format!("Plese provide the note in the language for the locale {}", user_locale)
let user_locale = std::env::var("_PR_LOCALE").unwrap_or("en-US".to_string());
let set_locale = if user_locale != "en-US" {
format!(
"Plese provide the note in the language for the locale {}\n",
user_locale
)
} else {
"".to_string()
};
@ -70,8 +79,7 @@ You run the command `{last_command}` and get the following error message: `{erro
```
{{"command":"your suggestion","note":"why you think this command will fix the error"}}
```
{set_locale}
If you don't know the answer or can't provide a good suggestion, please reply the command field with `None` and a explanation in note
{set_locale}If you don't know the answer or can't provide a good suggestion, please reply the command field with `None` and a explanation in note
"#
);

View file

@ -59,23 +59,12 @@ pub fn suggest_command(shell: &str, last_command: &str, error_msg: &str) -> Opti
#[cfg(feature = "request-ai")]
{
use crate::requests::ai_suggestion;
use textwrap::{fill, termwidth};
let suggest = ai_suggestion(last_command, error_msg);
if let Some(suggest) = suggest {
let suggest_note = suggest.note.split_whitespace().collect::<Vec<&str>>();
let mut note = String::new();
let mut line = String::new();
for word in suggest_note {
if line.len() + word.len() > 80 {
note.push_str(&line);
note.push('\n');
line.clear();
}
line.push_str(word);
line.push(' ');
}
note.push_str(&line);
let warn = format!("{}:", t!("ai-suggestion")).bold().blue();
let note = fill(&suggest.note, termwidth());
eprintln!("{}\n{}\n", warn, note);
let command = suggest.command;
if command != "None" {