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