blob: db53fd634feb88f1ab1c3717b3de8ec8a73b2fb0 (
plain) (
blame)
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
|
mod tiktoken;
pub use self::tiktoken::cl100k_base_singleton;
use chrono::prelude::*;
use std::io::{stdout, Write};
#[macro_export]
macro_rules! print_now {
($($arg:tt)*) => {
$crate::utils::print_now(&format!($($arg)*))
};
}
pub fn print_now<T: ToString>(text: &T) {
print!("{}", text.to_string());
let _ = stdout().flush();
}
pub fn now() -> String {
let now = Local::now();
now.to_rfc3339_opts(SecondsFormat::Secs, false)
}
pub fn get_env_name(key: &str) -> String {
format!(
"{}_{}",
env!("CARGO_CRATE_NAME").to_ascii_uppercase(),
key.to_ascii_uppercase(),
)
}
/// Split text to tokens
pub fn tokenize(text: &str) -> Vec<String> {
let tokens = cl100k_base_singleton().lock().tokenize(text);
tokens.into_iter().map(|(_, text)| text).collect()
}
/// Count how many tokens a piece of text needs to consume
pub fn count_tokens(text: &str) -> usize {
cl100k_base_singleton()
.lock()
.encode_with_special_tokens(text)
.len()
}
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)
}
|