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
use bat::PrettyPrinter;
use bat::Syntax;
use copypasta_ext::prelude::*;
use copypasta_ext::x11_fork::ClipboardContext;
use std::error::Error;
fn lang_exists(lang: &str, langs: &Vec<Syntax>) -> bool {
for l in langs {
if l.name.to_lowercase() == lang.to_lowercase() {
return true;
}
for e in &l.file_extensions {
if e == &lang.to_lowercase() {
return true;
}
}
}
false
}
pub fn pretty_print(str: &str, lang: &str) {
let mut lang = lang.to_owned();
let mut pp = PrettyPrinter::new();
let langs: Vec<_> = pp.syntaxes().collect();
if !lang_exists(&lang, &langs) {
lang = "txt".to_owned();
}
pp.input_from_bytes(str.as_bytes())
.language(&lang)
.print()
.unwrap();
}
pub fn copy_to_clipboard(str: &str) -> Result<(), Box<dyn Error>> {
let mut ctx = match ClipboardContext::new() {
Ok(c) => c,
Err(e) => {
return Err(format!(
"Cannot initialize clipboard context: {e}\n
Consider disabling the \"clipboard\" feature in Cargo.toml\n"
)
.into())
}
};
match ctx.set_contents(str.to_owned()) {
Ok(_) => return Ok(()),
Err(e) => Err(format!(
"Cannot write to clipboard: {e}\n
Consider disabling the \"clipboard\" feature in Cargo.toml\n"
)
.into()),
}
}