1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
mod abort_signal;
mod clipboard;
mod command;
mod crypto;
mod html_to_md;
mod loader;
mod path;
mod render_prompt;
mod request;
mod spinner;
mod variables;
pub use self::abort_signal::*;
pub use self::clipboard::set_text;
pub use self::command::*;
pub use self::crypto::*;
pub use self::html_to_md::*;
pub use self::loader::*;
pub use self::path::*;
pub use self::render_prompt::render_prompt;
pub use self::request::*;
pub use self::spinner::*;
pub use self::variables::*;
use anyhow::{Context, Result};
use fancy_regex::Regex;
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use is_terminal::IsTerminal;
use std::borrow::Cow;
use std::sync::LazyLock;
use std::{env, path::PathBuf, process};
use unicode_segmentation::UnicodeSegmentation;
pub static CODE_BLOCK_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?ms)```\w*(.*)```").unwrap());
pub static THINK_TAG_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?s)^\s*<think>.*?</think>(\s*|$)").unwrap());
pub static IS_STDOUT_TERMINAL: LazyLock<bool> = LazyLock::new(|| std::io::stdout().is_terminal());
pub static NO_COLOR: LazyLock<bool> = LazyLock::new(|| {
env::var("NO_COLOR")
.ok()
.and_then(|v| parse_bool(&v))
.unwrap_or_default()
|| !*IS_STDOUT_TERMINAL
});
pub fn now() -> String {
chrono::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, false)
}
pub fn now_timestamp() -> i64 {
chrono::Local::now().timestamp()
}
pub fn get_env_name(key: &str) -> String {
format!("{}_{key}", env!("CARGO_CRATE_NAME"),).to_ascii_uppercase()
}
pub fn normalize_env_name(value: &str) -> String {
value.replace('-', "_").to_ascii_uppercase()
}
pub fn parse_bool(value: &str) -> Option<bool> {
match value {
"1" | "true" => Some(true),
"0" | "false" => Some(false),
_ => None,
}
}
pub fn estimate_token_length(text: &str) -> usize {
let words: Vec<&str> = text.unicode_words().collect();
let mut output: f32 = 0.0;
for word in words {
if word.is_ascii() {
output += 1.3;
} else {
let count = word.chars().count();
if count == 1 {
output += 1.0
} else {
output += (count as f32) * 0.5;
}
}
}
output.ceil() as usize
}
pub fn light_theme_from_colorfgbg(colorfgbg: &str) -> Option<bool> {
let parts: Vec<_> = colorfgbg.split(';').collect();
let bg = match parts.len() {
2 => &parts[1],
3 => &parts[2],
_ => {
return None;
}
};
let bg = bg.parse::<u8>().ok()?;
let (r, g, b) = ansi_colours::rgb_from_ansi256(bg);
let v = 0.2126 * r as f32 + 0.7152 * g as f32 + 0.0722 * b as f32;
let light = v > 128.0;
Some(light)
}
pub fn strip_think_tag(text: &str) -> Cow<str> {
THINK_TAG_RE.replace_all(text, "")
}
pub fn extract_code_block(text: &str) -> &str {
CODE_BLOCK_RE
.captures(text)
.ok()
.and_then(|v| v?.get(1).map(|v| v.as_str().trim()))
.unwrap_or(text)
}
pub fn convert_option_string(value: &str) -> Option<String> {
if value.is_empty() {
None
} else {
Some(value.to_string())
}
}
pub fn fuzzy_filter<T, F>(values: Vec<T>, get: F, pattern: &str) -> Vec<T>
where
F: Fn(&T) -> &str,
{
let matcher = SkimMatcherV2::default();
let mut list: Vec<(T, i64)> = values
.into_iter()
.filter_map(|v| {
let score = matcher.fuzzy_match(get(&v), pattern)?;
Some((v, score))
})
.collect();
list.sort_unstable_by(|a, b| b.1.cmp(&a.1));
list.into_iter().map(|(v, _)| v).collect()
}
pub fn pretty_error(err: &anyhow::Error) -> String {
let mut output = vec![];
output.push(format!("Error: {err}"));
let causes: Vec<_> = err.chain().skip(1).collect();
let causes_len = causes.len();
if causes_len > 0 {
output.push("\nCaused by:".to_string());
if causes_len == 1 {
output.push(format!(" {}", indent_text(causes[0], 4).trim()));
} else {
for (i, cause) in causes.into_iter().enumerate() {
output.push(format!("{i:5}: {}", indent_text(cause, 7).trim()));
}
}
}
output.join("\n")
}
pub fn indent_text<T: ToString>(s: T, size: usize) -> String {
let indent_str = " ".repeat(size);
s.to_string()
.split('\n')
.map(|line| format!("{}{}", indent_str, line))
.collect::<Vec<String>>()
.join("\n")
}
pub fn error_text(input: &str) -> String {
color_text(input, nu_ansi_term::Color::Red)
}
pub fn warning_text(input: &str) -> String {
color_text(input, nu_ansi_term::Color::Yellow)
}
pub fn color_text(input: &str, color: nu_ansi_term::Color) -> String {
if *NO_COLOR {
return input.to_string();
}
nu_ansi_term::Style::new()
.fg(color)
.paint(input)
.to_string()
}
pub fn dimmed_text(input: &str) -> String {
if *NO_COLOR {
return input.to_string();
}
nu_ansi_term::Style::new().dimmed().paint(input).to_string()
}
pub fn multiline_text(input: &str) -> String {
input
.split('\n')
.enumerate()
.map(|(i, v)| {
if i == 0 {
v.to_string()
} else {
format!(".. {v}")
}
})
.collect::<Vec<String>>()
.join("\n")
}
pub fn temp_file(prefix: &str, suffix: &str) -> PathBuf {
env::temp_dir().join(format!(
"{}-{}{prefix}{}{suffix}",
env!("CARGO_CRATE_NAME").to_lowercase(),
process::id(),
uuid::Uuid::new_v4()
))
}
pub fn is_url(path: &str) -> bool {
path.starts_with("http://") || path.starts_with("https://")
}
pub fn set_proxy(
mut builder: reqwest::ClientBuilder,
proxy: &str,
) -> Result<reqwest::ClientBuilder> {
builder = builder.no_proxy();
if !proxy.is_empty() && proxy != "-" {
builder = builder
.proxy(reqwest::Proxy::all(proxy).with_context(|| format!("Invalid proxy `{proxy}`"))?);
};
Ok(builder)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(not(target_os = "windows"))]
fn test_safe_join_path() {
assert_eq!(
safe_join_path("/home/user/dir1", "files/file1"),
Some(PathBuf::from("/home/user/dir1/files/file1"))
);
assert!(safe_join_path("/home/user/dir1", "/files/file1").is_none());
assert!(safe_join_path("/home/user/dir1", "../file1").is_none());
}
#[test]
#[cfg(target_os = "windows")]
fn test_safe_join_path() {
assert_eq!(
safe_join_path("C:\\Users\\user\\dir1", "files/file1"),
Some(PathBuf::from("C:\\Users\\user\\dir1\\files\\file1"))
);
assert!(safe_join_path("C:\\Users\\user\\dir1", "/files/file1").is_none());
assert!(safe_join_path("C:\\Users\\user\\dir1", "../file1").is_none());
}
}
|