| 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 |
|
| 51 |
xt |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
— |
| 57 |
|
| 58 |
pub(crate) fn is_binary(buffer: &[u8]) -> bool { |
| 59 |
|
| 60 |
const BYTE_ORDER_MARKS: &[&[u8]] = &[ |
| 61 |
&[0xEF, 0xBB, 0xBF], |
| 62 |
&[0x00, 0x00, 0xFE, 0xFF], |
| 63 |
&[0xFF, 0xFE, 0x00, 0x00], |
| 64 |
&[0xFE, 0xFF], |
| 65 |
&[0xFF, 0xFE], |
| 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 |
|
| 77 |
fn filter_picker_entry(entry: &DirEntry, root: &Path, dedup_symlinks: bool) -> bool { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
if matches!( |
| 82 |
entry.file_name().to_str(), |
| 83 |
Some(".git" | ".pijul" | ".jj" | ".hg" | ".svn") |
| 84 |
) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 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 |
|
| 128 |
assert!(is_binary(b"text\0with nul")); |
| 129 |
|
| 130 |
assert!(is_binary(b"%PDF-1.7 ...")); |
| 131 |
assert!(is_binary(b"\x89PNG\r\n")); |
| 132 |
|
| 133 |
assert!(!is_binary(b"\xFF\xFEt\0e\0x\0t\0")); |
| 134 |
assert!(!is_binary(b"\x00\x00\xFE\xFFtext")); |
| 135 |
assert!(!is_binary(b"\xEF\xBB\xBFtext")); |
| 136 |
} |
| 137 |
} |