Skip to content

Commit e2197b2

Browse files
authored
Merge pull request #4360 from ci4ic4/fix/browser-open-bsd
Fix/browser open on bsd systems
2 parents 1ec2808 + f484177 commit e2197b2

2 files changed

Lines changed: 43 additions & 15 deletions

File tree

crates/tui/src/tui/clipboard.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use std::path::{Path, PathBuf};
1818
))]
1919
use std::process::{Command, Stdio};
2020
#[cfg(any(
21-
test,
2221
target_os = "macos",
2322
target_os = "windows",
2423
all(target_os = "linux", not(target_env = "ohos"))
@@ -27,15 +26,13 @@ use std::time::{SystemTime, UNIX_EPOCH};
2726

2827
use anyhow::{Context, Result, bail};
2928
#[cfg(any(
30-
test,
3129
target_os = "macos",
3230
target_os = "windows",
3331
all(target_os = "linux", not(target_env = "ohos"))
3432
))]
3533
use arboard::{Clipboard, ImageData};
3634
use base64::Engine as _;
3735
#[cfg(any(
38-
test,
3936
target_os = "macos",
4037
target_os = "windows",
4138
all(target_os = "linux", not(target_env = "ohos"))
@@ -71,7 +68,10 @@ impl PastedImage {
7168

7269
/// Clipboard payloads supported by the TUI.
7370
#[cfg_attr(
74-
all(any(target_env = "ohos", target_os = "android"), not(test)),
71+
all(
72+
any(target_env = "ohos", target_os = "android", target_os = "netbsd"),
73+
not(test)
74+
),
7575
allow(dead_code)
7676
)]
7777
pub enum ClipboardContent {
@@ -82,14 +82,12 @@ pub enum ClipboardContent {
8282
/// Clipboard reader/writer helper.
8383
pub struct ClipboardHandler {
8484
#[cfg(any(
85-
test,
8685
target_os = "macos",
8786
target_os = "windows",
8887
all(target_os = "linux", not(target_env = "ohos"))
8988
))]
9089
clipboard: Option<Clipboard>,
9190
#[cfg(any(
92-
test,
9391
target_os = "macos",
9492
target_os = "windows",
9593
all(target_os = "linux", not(target_env = "ohos"))
@@ -108,14 +106,12 @@ impl ClipboardHandler {
108106
pub fn new() -> Self {
109107
Self {
110108
#[cfg(any(
111-
test,
112109
target_os = "macos",
113110
target_os = "windows",
114111
all(target_os = "linux", not(target_env = "ohos"))
115112
))]
116113
clipboard: None,
117114
#[cfg(any(
118-
test,
119115
target_os = "macos",
120116
target_os = "windows",
121117
all(target_os = "linux", not(target_env = "ohos"))
@@ -135,7 +131,6 @@ impl ClipboardHandler {
135131
/// handler stays in fallback/no-op mode and `read`/`write_text` fall
136132
/// through to their OSC 52 and pbcopy/powershell fallbacks.
137133
#[cfg(any(
138-
test,
139134
target_os = "macos",
140135
target_os = "windows",
141136
all(target_os = "linux", not(target_env = "ohos"))
@@ -167,7 +162,6 @@ impl ClipboardHandler {
167162
}
168163

169164
#[cfg(any(
170-
test,
171165
target_os = "macos",
172166
target_os = "windows",
173167
all(target_os = "linux", not(target_env = "ohos"))
@@ -378,7 +372,6 @@ fn clipboard_images_dir_for_home(workspace: &Path, home: Option<&Path>) -> PathB
378372
/// Encode an RGBA `ImageData` from arboard as PNG and persist it. Returns
379373
/// the resulting path along with metadata used to render the paste hint.
380374
#[cfg(any(
381-
test,
382375
target_os = "macos",
383376
target_os = "windows",
384377
all(target_os = "linux", not(target_env = "ohos"))
@@ -390,7 +383,6 @@ fn save_image_as_png(workspace: &Path, image: &ImageData) -> Result<PastedImage>
390383
/// Lower-level variant that writes into an explicit directory. Exposed so the
391384
/// unit tests don't have to scribble inside the user's real home directory.
392385
#[cfg(any(
393-
test,
394386
target_os = "macos",
395387
target_os = "windows",
396388
all(target_os = "linux", not(target_env = "ohos"))
@@ -439,10 +431,21 @@ fn save_image_as_png_in(dir: &Path, image: &ImageData) -> Result<PastedImage> {
439431
#[cfg(test)]
440432
mod tests {
441433
use super::*;
434+
// ImageData from arboard is only available on these platforms.
435+
#[cfg(any(
436+
target_os = "macos",
437+
target_os = "windows",
438+
all(target_os = "linux", not(target_env = "ohos"))
439+
))]
442440
use std::borrow::Cow;
443441
#[cfg(unix)]
444442
use std::os::unix::fs::PermissionsExt;
445443

444+
#[cfg(any(
445+
target_os = "macos",
446+
target_os = "windows",
447+
all(target_os = "linux", not(target_env = "ohos"))
448+
))]
446449
fn solid_rgba(width: u16, height: u16, rgba: [u8; 4]) -> ImageData<'static> {
447450
let mut bytes = Vec::with_capacity((width as usize) * (height as usize) * 4);
448451
for _ in 0..(width as usize * height as usize) {
@@ -456,6 +459,11 @@ mod tests {
456459
}
457460

458461
#[test]
462+
#[cfg(any(
463+
target_os = "macos",
464+
target_os = "windows",
465+
all(target_os = "linux", not(target_env = "ohos"))
466+
))]
459467
fn save_image_as_png_writes_valid_png() {
460468
let dir = tempfile::tempdir().unwrap();
461469
let img = solid_rgba(8, 4, [255, 0, 0, 255]);

crates/tui/src/utils.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ pub fn flush_and_sync(writer: &mut std::io::BufWriter<std::fs::File>) -> std::io
292292
///
293293
/// Dispatches to the platform-appropriate opener:
294294
/// - macOS: `open`
295-
/// - Linux: `xdg-open`
295+
/// - Linux / BSD: `xdg-open`
296296
/// - Windows: `cmd /C start ""`
297297
/// - Other: returns an error.
298298
///
@@ -321,7 +321,13 @@ fn browser_open_command(url: &str) -> Result<Command> {
321321
Ok(command)
322322
}
323323

324-
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
324+
#[cfg(any(
325+
all(target_os = "linux", not(target_env = "ohos")),
326+
target_os = "netbsd",
327+
target_os = "freebsd",
328+
target_os = "openbsd",
329+
target_os = "dragonfly"
330+
))]
325331
{
326332
let mut command = Command::new("xdg-open");
327333
command.arg(url);
@@ -338,7 +344,11 @@ fn browser_open_command(url: &str) -> Result<Command> {
338344
#[cfg(not(any(
339345
target_os = "macos",
340346
all(target_os = "linux", not(target_env = "ohos")),
341-
target_os = "windows"
347+
target_os = "windows",
348+
target_os = "netbsd",
349+
target_os = "freebsd",
350+
target_os = "openbsd",
351+
target_os = "dragonfly"
342352
)))]
343353
Err(anyhow::anyhow!(
344354
"browser opening is unsupported on this platform"
@@ -967,6 +977,16 @@ mod project_mapping_tests {
967977
);
968978
}
969979

980+
#[cfg(any(
981+
target_os = "netbsd",
982+
target_os = "freebsd",
983+
target_os = "openbsd",
984+
target_os = "dragonfly"
985+
))]
986+
{
987+
assert_eq!(command.get_program(), "xdg-open");
988+
}
989+
970990
#[cfg(all(target_os = "linux", not(target_env = "ohos")))]
971991
{
972992
assert_eq!(command.get_program(), "xdg-open");

0 commit comments

Comments
 (0)