Skip to content

Commit 328bf37

Browse files
authored
feat(wasm): add feature "fancy-no-syscall" for wasm targets (#349)
Fixes: #346
1 parent 6ea86a2 commit 328bf37

File tree

6 files changed

+99
-36
lines changed

6 files changed

+99
-36
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ jobs:
4949
if: matrix.rust == '1.70.0'
5050
run: cargo test --all --verbose --features ${{matrix.features}} no-format-args-capture
5151

52+
wasm:
53+
name: Check Wasm build
54+
runs-on: ubuntu-latest
55+
steps:
56+
- uses: actions/checkout@v4
57+
- name: Install Rust
58+
uses: dtolnay/rust-toolchain@master
59+
with:
60+
toolchain: stable
61+
targets: wasm32-unknown-unknown
62+
- name: Check wasm target
63+
run: cargo check --target wasm32-unknown-unknown --features fancy-no-syscall
64+
5265
miri:
5366
name: Miri
5467
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ exclude = ["images/", "tests/", "miette-derive/"]
1616
thiserror = "1.0.56"
1717
miette-derive = { path = "miette-derive", version = "=7.1.0", optional = true }
1818
unicode-width = "0.1.11"
19+
cfg-if = "1.0.0"
1920

2021
owo-colors = { version = "4.0.0", optional = true }
2122
textwrap = { version = "0.16.0", optional = true }
@@ -47,9 +48,15 @@ strip-ansi-escapes = "0.2.0"
4748
default = ["derive"]
4849
derive = ["miette-derive"]
4950
no-format-args-capture = []
50-
fancy-no-backtrace = [
51+
fancy-base = [
5152
"owo-colors",
5253
"textwrap",
54+
]
55+
fancy-no-syscall = [
56+
"fancy-base",
57+
]
58+
fancy-no-backtrace = [
59+
"fancy-base",
5360
"terminal_size",
5461
"supports-hyperlinks",
5562
"supports-color",

src/eyreish/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ pub use ReportHandler as EyreContext;
2424
#[allow(unreachable_pub)]
2525
pub use WrapErr as Context;
2626

27-
#[cfg(not(feature = "fancy-no-backtrace"))]
27+
#[cfg(not(feature = "fancy-base"))]
2828
use crate::DebugReportHandler;
2929
use crate::Diagnostic;
30-
#[cfg(feature = "fancy-no-backtrace")]
30+
#[cfg(feature = "fancy-base")]
3131
use crate::MietteHandler;
3232

3333
use error::ErrorImpl;
@@ -102,9 +102,9 @@ fn capture_handler(error: &(dyn Diagnostic + 'static)) -> Box<dyn ReportHandler>
102102
}
103103

104104
fn get_default_printer(_err: &(dyn Diagnostic + 'static)) -> Box<dyn ReportHandler + 'static> {
105-
#[cfg(feature = "fancy-no-backtrace")]
105+
#[cfg(feature = "fancy-base")]
106106
return Box::new(MietteHandler::new());
107-
#[cfg(not(feature = "fancy-no-backtrace"))]
107+
#[cfg(not(feature = "fancy-base"))]
108108
return Box::new(DebugReportHandler::new());
109109
}
110110

src/handler.rs

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,15 @@ impl MietteHandlerOpts {
265265
let characters = match self.unicode {
266266
Some(true) => ThemeCharacters::unicode(),
267267
Some(false) => ThemeCharacters::ascii(),
268-
None if supports_unicode::on(supports_unicode::Stream::Stderr) => {
269-
ThemeCharacters::unicode()
270-
}
268+
None if syscall::supports_unicode() => ThemeCharacters::unicode(),
271269
None => ThemeCharacters::ascii(),
272270
};
273271
let styles = if self.color == Some(false) {
274272
ThemeStyles::none()
275-
} else if let Some(color) = supports_color::on(supports_color::Stream::Stderr) {
273+
} else if let Some(color_has_16m) = syscall::supports_color_has_16m() {
276274
match self.rgb_colors {
277275
RgbColors::Always => ThemeStyles::rgb(),
278-
RgbColors::Preferred if color.has_16m => ThemeStyles::rgb(),
276+
RgbColors::Preferred if color_has_16m => ThemeStyles::rgb(),
279277
_ => ThemeStyles::ansi(),
280278
}
281279
} else if self.color == Some(true) {
@@ -291,9 +289,7 @@ impl MietteHandlerOpts {
291289
#[cfg(feature = "syntect-highlighter")]
292290
let highlighter = if self.color == Some(false) {
293291
MietteHighlighter::nocolor()
294-
} else if self.color == Some(true)
295-
|| supports_color::on(supports_color::Stream::Stderr).is_some()
296-
{
292+
} else if self.color == Some(true) || syscall::supports_color() {
297293
match self.highlighter {
298294
Some(highlighter) => highlighter,
299295
None => match self.rgb_colors {
@@ -366,26 +362,13 @@ impl MietteHandlerOpts {
366362
if let Some(linkify) = self.linkify {
367363
linkify
368364
} else {
369-
supports_hyperlinks::on(supports_hyperlinks::Stream::Stderr)
365+
syscall::supports_hyperlinks()
370366
}
371367
}
372368

373-
#[cfg(not(miri))]
374-
pub(crate) fn get_width(&self) -> usize {
375-
self.width.unwrap_or_else(|| {
376-
terminal_size::terminal_size()
377-
.unwrap_or((terminal_size::Width(80), terminal_size::Height(0)))
378-
.0
379-
.0 as usize
380-
})
381-
}
382-
383-
#[cfg(miri)]
384-
// miri doesn't support a syscall (specifically ioctl)
385-
// performed by terminal_size, which causes test execution to fail
386-
// so when miri is running we'll just fallback to a constant
387369
pub(crate) fn get_width(&self) -> usize {
388-
self.width.unwrap_or(80)
370+
self.width
371+
.unwrap_or_else(|| syscall::terminal_width().unwrap_or(80))
389372
}
390373
}
391374

@@ -430,3 +413,63 @@ impl ReportHandler for MietteHandler {
430413
self.inner.debug(diagnostic, f)
431414
}
432415
}
416+
417+
mod syscall {
418+
use cfg_if::cfg_if;
419+
420+
#[inline]
421+
pub(super) fn terminal_width() -> Option<usize> {
422+
cfg_if! {
423+
if #[cfg(any(feature = "fancy-no-syscall", miri))] {
424+
None
425+
} else {
426+
terminal_size::terminal_size().map(|size| size.0 .0 as usize)
427+
}
428+
}
429+
}
430+
431+
#[inline]
432+
pub(super) fn supports_hyperlinks() -> bool {
433+
cfg_if! {
434+
if #[cfg(feature = "fancy-no-syscall")] {
435+
false
436+
} else {
437+
supports_hyperlinks::on(supports_hyperlinks::Stream::Stderr)
438+
}
439+
}
440+
}
441+
442+
#[cfg(feature = "syntect-highlighter")]
443+
#[inline]
444+
pub(super) fn supports_color() -> bool {
445+
cfg_if! {
446+
if #[cfg(feature = "fancy-no-syscall")] {
447+
false
448+
} else {
449+
supports_color::on(supports_color::Stream::Stderr).is_some()
450+
}
451+
}
452+
}
453+
454+
#[inline]
455+
pub(super) fn supports_color_has_16m() -> Option<bool> {
456+
cfg_if! {
457+
if #[cfg(feature = "fancy-no-syscall")] {
458+
None
459+
} else {
460+
supports_color::on(supports_color::Stream::Stderr).map(|color| color.has_16m)
461+
}
462+
}
463+
}
464+
465+
#[inline]
466+
pub(super) fn supports_unicode() -> bool {
467+
cfg_if! {
468+
if #[cfg(feature = "fancy-no-syscall")] {
469+
false
470+
} else {
471+
supports_unicode::on(supports_unicode::Stream::Stderr)
472+
}
473+
}
474+
}
475+
}

src/handlers/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ Reporters included with `miette`.
55
#[allow(unreachable_pub)]
66
pub use debug::*;
77
#[allow(unreachable_pub)]
8-
#[cfg(feature = "fancy-no-backtrace")]
8+
#[cfg(feature = "fancy-base")]
99
pub use graphical::*;
1010
#[allow(unreachable_pub)]
1111
pub use json::*;
1212
#[allow(unreachable_pub)]
1313
pub use narratable::*;
1414
#[allow(unreachable_pub)]
15-
#[cfg(feature = "fancy-no-backtrace")]
15+
#[cfg(feature = "fancy-base")]
1616
pub use theme::*;
1717

1818
mod debug;
19-
#[cfg(feature = "fancy-no-backtrace")]
19+
#[cfg(feature = "fancy-base")]
2020
mod graphical;
2121
mod json;
2222
mod narratable;
23-
#[cfg(feature = "fancy-no-backtrace")]
23+
#[cfg(feature = "fancy-base")]
2424
mod theme;

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ pub use miette_derive::*;
760760

761761
pub use error::*;
762762
pub use eyreish::*;
763-
#[cfg(feature = "fancy-no-backtrace")]
763+
#[cfg(feature = "fancy-base")]
764764
pub use handler::*;
765765
pub use handlers::*;
766766
pub use miette_diagnostic::*;
@@ -773,10 +773,10 @@ mod chain;
773773
mod diagnostic_chain;
774774
mod error;
775775
mod eyreish;
776-
#[cfg(feature = "fancy-no-backtrace")]
776+
#[cfg(feature = "fancy-base")]
777777
mod handler;
778778
mod handlers;
779-
#[cfg(feature = "fancy-no-backtrace")]
779+
#[cfg(feature = "fancy-base")]
780780
pub mod highlighters;
781781
#[doc(hidden)]
782782
pub mod macro_helpers;

0 commit comments

Comments
 (0)