Skip to content

Commit

Permalink
health: Go back tracing (when markdown) so -q works
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Oct 16, 2024
1 parent dccab35 commit f3c398d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
28 changes: 16 additions & 12 deletions crates/nix_health/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(async_closure)]
//! Health checks for the user's Nix install

pub mod check;
Expand All @@ -12,7 +13,7 @@ use nix_rs::env::OS;
use nix_rs::flake::url::FlakeUrl;
use nix_rs::{command::NixCmd, info::NixInfo};
use omnix_common::config::{OmConfig, OmConfigError};
use omnix_common::markdown::print_markdown;
use omnix_common::markdown::render_markdown;
use serde::{Deserialize, Serialize};
use tracing::instrument;
use traits::Check;
Expand Down Expand Up @@ -85,27 +86,30 @@ impl NixHealth {

pub async fn print_report_returning_exit_code(checks: &[traits::Check]) -> anyhow::Result<i32> {
let mut res = AllChecksResult::new();
let pwd = std::env::current_dir().unwrap(); // FIXME
let pwd = std::env::current_dir().unwrap(); // FIXME: avoid unwrap
let md = async |s: &str| render_markdown(&pwd, s).await;
for check in checks {
match &check.result {
traits::CheckResult::Green => {
tracing::info!("✅ {}", check.title.green().bold());
print_markdown(&pwd, &format!("{}", check.info.dimmed())).await?;
tracing::info!("{}", md(&check.info).await?.dimmed());
}
traits::CheckResult::Red { msg, suggestion } => {
res.register_failure(check.required);
if check.required {
print_markdown(&pwd, &format!("❌ {}", check.title.red().bold())).await?;
tracing::error!("❌ {}", md(&check.title).await?.red().bold());
} else {
print_markdown(&pwd, &format!("🟧 {}", check.title.yellow().bold()))
.await?;
tracing::warn!("🟧 {}", md(&check.title).await?.yellow().bold());
}
print_markdown(&pwd, &format!("{}", check.info.dimmed())).await?;
print_markdown(
&pwd,
&format!("**Problem**: {}\\\n**Fix**: {}\n", msg, suggestion),
)
.await?;
tracing::info!("{}", md(&check.info).await?.dimmed());
tracing::error!(
"{}",
md(&format!(
"**Problem**: {}\\\n**Fix**: {}\n",
msg, suggestion
))
.await?
);
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions crates/omnix-common/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pulldown_cmark::{Options, Parser};
use pulldown_cmark_mdcat::{
resources::FileResourceHandler, Environment, Settings, TerminalProgram, TerminalSize, Theme,
};
use std::path::Path;
use std::{io::Write, path::Path};
use syntect::parsing::SyntaxSet;

lazy_static! {
Expand All @@ -21,6 +21,22 @@ lazy_static! {

/// Print Markdown using `mdcat` to STDERR
pub async fn print_markdown(base_dir: &Path, s: &str) -> anyhow::Result<()> {
print_markdown_to(base_dir, &mut std::io::stderr(), s).await
}

/// Render Markdown into a string to be printed to terminal
pub async fn render_markdown(base_dir: &Path, s: &str) -> anyhow::Result<String> {
let mut w = Vec::new();
print_markdown_to(base_dir, &mut w, s).await?;
let s = String::from_utf8(w)?;
// A trim is needed to remove unnecessary newlines at end (which can impact for single-line renders)
Ok(s.trim().to_string())
}

async fn print_markdown_to<W>(base_dir: &Path, w: &mut W, s: &str) -> anyhow::Result<()>
where
W: Write,
{
let env = Environment::for_local_directory(&base_dir)?;
let handler = FileResourceHandler::new(200000);
let parser = Parser::new_ext(
Expand All @@ -31,7 +47,7 @@ pub async fn print_markdown(base_dir: &Path, s: &str) -> anyhow::Result<()> {
| Options::ENABLE_GFM,
);

pulldown_cmark_mdcat::push_tty(&SETTINGS, &env, &handler, &mut std::io::stderr(), parser)?;
pulldown_cmark_mdcat::push_tty(&SETTINGS, &env, &handler, w, parser)?;

Ok(())
}

0 comments on commit f3c398d

Please sign in to comment.