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

76
Cargo.lock generated
View file

@ -654,6 +654,17 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "io-lifetimes"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "ipnet"
version = "2.10.1"
@ -702,6 +713,12 @@ version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64804cc6a5042d4f05379909ba25b503ec04e2c082151d62122d5dcaa274b961"
[[package]]
name = "linux-raw-sys"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519"
[[package]]
name = "linux-raw-sys"
version = "0.4.14"
@ -850,6 +867,7 @@ dependencies = [
"serde",
"serde_json",
"sys-locale",
"textwrap",
"toml 0.8.19",
]
@ -1062,6 +1080,20 @@ version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "rustix"
version = "0.37.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea8ca367a3a01fe35e6943c400addf443c0f57670e6ec51196f71a4b8762dd2"
dependencies = [
"bitflags 1.3.2",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys 0.3.8",
"windows-sys 0.48.0",
]
[[package]]
name = "rustix"
version = "0.38.41"
@ -1071,7 +1103,7 @@ dependencies = [
"bitflags 2.6.0",
"errno",
"libc",
"linux-raw-sys",
"linux-raw-sys 0.4.14",
"windows-sys 0.52.0",
]
@ -1258,6 +1290,12 @@ version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "smawk"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c"
[[package]]
name = "socket2"
version = "0.5.7"
@ -1367,10 +1405,32 @@ dependencies = [
"cfg-if",
"fastrand",
"once_cell",
"rustix",
"rustix 0.38.41",
"windows-sys 0.59.0",
]
[[package]]
name = "terminal_size"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e6bf6f19e9f8ed8d4048dc22981458ebcf406d67e94cd422e5ecd73d63b3237"
dependencies = [
"rustix 0.37.27",
"windows-sys 0.48.0",
]
[[package]]
name = "textwrap"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9"
dependencies = [
"smawk",
"terminal_size",
"unicode-linebreak",
"unicode-width",
]
[[package]]
name = "tinystr"
version = "0.7.6"
@ -1537,6 +1597,18 @@ version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "unicode-linebreak"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f"
[[package]]
name = "unicode-width"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "untrusted"
version = "0.9.0"

View file

@ -25,6 +25,7 @@ toml = { version = "0.8", optional = true }
serde_json = { version = "1.0", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
reqwest = { version = "0.12", features = ["blocking", "json"], optional = true }
textwrap = { version = "0.16", features = ["terminal_size"], optional = true}
pay-respects-parser = "0.2.5"
# pay-respects-parser = { path = "../pay-respects-parser" }
@ -32,7 +33,7 @@ pay-respects-parser = "0.2.5"
[features]
runtime-rules = ["dep:serde", "dep:toml"]
request-ai = ["dep:serde", "dep:serde_json", "dep:reqwest"]
request-ai = ["dep:serde", "dep:serde_json", "dep:reqwest", "dep:textwrap"]
[profile.release]
strip = true

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" {