Debian Codegraph @

helix-term/src/lib.rs

raw symbols SCIP
1 #[macro_use]
2 extern crate helix_view;
3
4 pub mod application;
5 pub mod args;
6 pub mod commands;
7 pub mod compositor;
8 pub mod config;
9 pub mod events;
10 pub mod health;
11 pub mod job;
12 pub mod keymap;
13 pub mod logging;
14 pub mod ui;
15
16 #[cfg(not(windows))]
17 use std::env::var_os;
18
19 use std::path::Path;
20
21 use futures_util::Future;
22 mod handlers;
23
24 use helix_stdx::Url;
25 use ignore::DirEntry;
26
27 #[cfg(windows)]
28 fn true_color() -> bool {
29 true
30 }
31
32 #[cfg(not(windows))]
33 fn true_color() -> bool {
34 if var_os("COLORTERM").is_some_and(|v| v == "truecolor" || v == "24bit")
35 || var_os("WSL_DISTRO_NAME").is_some()
36 {
37 return true;
38 }
39
40 match termini::TermInfo::from_env() {
41 Ok(t) => {
42 t.extended_cap("RGB").is_some()
43 || t.extended_cap("Tc").is_some()
44 || (t.extended_cap("setrgbf").is_some() && t.extended_cap("setrgbb").is_some())
45 }
46 Err(_) => false,
47 }
48 }
49
50 /// Heuristic "is this a binary (non-text) file?" check over a leading chunk of a
51 /// file. Replaces the `content_inspector` crate — we only need the binary/text
52 /// verdict, not its encoding classification.
53 ///
54 /// A leading byte-order mark marks the content as text (UTF-16/32 text
55 /// legitimately contains NUL bytes, so it must be excluded before the NUL scan);
56 /// otherwise a NUL byte in the first kilobyte — or a known binary magic number —
57 /// means binary.
58 pub(crate) fn is_binary(buffer: &[u8]) -> bool {
59 // UTF-32 BOMs must be checked before UTF-16 (their BOMs overlap).
60 const BYTE_ORDER_MARKS: &[&[u8]] = &[
61 &[0xEF, 0xBB, 0xBF], // UTF-8
62 &[0x00, 0x00, 0xFE, 0xFF], // UTF-32BE
63 &[0xFF, 0xFE, 0x00, 0x00], // UTF-32LE
64 &[0xFE, 0xFF], // UTF-16BE
65 &[0xFF, 0xFE], // UTF-16LE
66 ];
67
68 if BYTE_ORDER_MARKS.iter().any(|bom| buffer.starts_with(bom)) {
69 return false;
70 }
71
72 let scan = &buffer[..buffer.len().min(1024)];
73 scan.contains(&0) || buffer.starts_with(b"%PDF") || buffer.starts_with(b"\x89PNG")
74 }
75
76 /// Function used for filtering dir entries in the various file pickers.
77 fn filter_picker_entry(entry: &DirEntry, root: &Path, dedup_symlinks: bool) -> bool {
78 // We always want to ignore popular VCS directories, otherwise if
79 // `ignore` is turned off, we end up with a lot of noise
80 // in our picker.
81 if matches!(
82 entry.file_name().to_str(),
83 Some(".git" | ".pijul" | ".jj" | ".hg" | ".svn")
84 ) {
85 return false;
86 }
87
88 // We also ignore symlinks that point inside the current directory
89 // if `dedup_links` is enabled.
90 if dedup_symlinks && entry.path_is_symlink() {
91 return entry
92 .path()
93 .canonicalize()
94 .ok()
95 .is_some_and(|path| !path.starts_with(root));
96 }
97
98 true
99 }
100
101 /// Opens URL in external program.
102 fn open_external_url_callback(
103 url: Url,
104 ) -> impl Future<Output = Result<job::Callback, anyhow::Error>> + Send + 'static {
105 let commands = open::commands(url.as_str());
106 async {
107 for cmd in commands {
108 let mut command: tokio::process::Command = cmd.into();
109 if command.status().await.is_ok() {
110 return Ok(job::Callback::Editor(Box::new(|_| {})));
111 }
112 }
113 Ok(job::Callback::Editor(Box::new(move |editor| {
114 editor.set_error("Opening URL in external program failed")
115 })))
116 }
117 }
118
119 #[cfg(test)]
120 mod tests {
121 use super::is_binary;
122
123 #[test]
124 fn binary_detection() {
125 assert!(!is_binary(b""));
126 assert!(!is_binary(b"plain text\nsecond line"));
127 // a NUL byte in the scanned range -> binary
128 assert!(is_binary(b"text\0with nul"));
129 // binary magic numbers with no NUL prefix
130 assert!(is_binary(b"%PDF-1.7 ..."));
131 assert!(is_binary(b"\x89PNG\r\n"));
132 // a BOM marks the content as text even though it carries NUL bytes
133 assert!(!is_binary(b"\xFF\xFEt\0e\0x\0t\0")); // UTF-16LE
134 assert!(!is_binary(b"\x00\x00\xFE\xFFtext")); // UTF-32BE
135 assert!(!is_binary(b"\xEF\xBB\xBFtext")); // UTF-8 BOM
136 }
137 }