Repository: helix-editor/helix
File: helix-term/src/health.rs (Rust)

Code before:

    let mut headings = vec!["Language", "Language servers", "Debug adapter", "Formatter"];

    for feat in TsFeature::all() {
        headings.push(feat.short_title())
    }

    let terminal_cols = crossterm::terminal::size().map(|(c, _)| c).unwrap_or(80);
    let column_width = terminal_cols as usize / headings.len();
    let is_terminal = std::io::stdout().is_tty();

    let column = |item: &str, color: Color| {
        let mut data = format!(
            "{:width$}",
            item.get(..column_width - 2)
                .map(|s| format!("{}…", s))
                .unwrap_or_else(|| item.to_string()),
            width = column_width,
        );
        if is_terminal {
            data = data.stylize().with(color).to_string();
        }

        // We can't directly use println!() because of
        // https://github.com/crossterm-rs/crossterm/issues/589
        let _ = crossterm::execute!(std::io::stdout(), Print(data));
    };

    for heading in headings {
        column(heading, Color::White);
    }
    writeln!(stdout)?;

    syn_loader_conf
        .language
        .sort_unstable_by_key(|l| l.language_id.clone());

    let check_binary = |cmd: Option<&str>| match cmd {
        Some(cmd) => match helix_stdx::env::which(cmd) {
            Ok(_) => column(&format!("✓ {}", cmd), Color::Green),
            Err(_) => column(&format!("✘ {}", cmd), Color::Red),
        },
        None => column("None", Color::Yellow),
    };

    for lang in &syn_loader_conf.language {
        column(&lang.language_id, Color::Reset);

        let mut cmds = lang.language_servers.iter().filter_map(|ls| {
            syn_loader_conf
                .language_server

Review comment (refactor):
Note to reviewers: this workaround was not actually necessary (as can be verified by changing this line to `write!(std::io::stdout(), "{}", data);`). The linked crossterm issue is about the `Display` implementation of crossterm types, which is not relevant because the `format!` call works on strings and styling is added later. This was probably an artifact of refactoring.

Code after:

    let mut headings = vec!["Language", "Language servers", "Debug adapter", "Formatter"];

    for feat in TsFeature::all() {
        headings.push(feat.short_title())
    }

    let terminal_cols = crossterm::terminal::size().map(|(c, _)| c).unwrap_or(80);
    let column_width = terminal_cols as usize / headings.len();
    let is_terminal = std::io::stdout().is_tty();

    let fit = |s: &str| -> StyledContent<String> {
        format!(
            "{:column_width$}",
            s.get(..column_width - 2)
                .map(|s| format!("{}…", s))
                .unwrap_or_else(|| s.to_string())
        )
        .stylize()
    };
    let color = |s: StyledContent<String>, c: Color| if is_terminal { s.with(c) } else { s };
    let bold = |s: StyledContent<String>| if is_terminal { s.bold() } else { s };

    for heading in headings {
        write!(stdout, "{}", bold(fit(heading)))?;
    }
    writeln!(stdout)?;

    syn_loader_conf
        .language
        .sort_unstable_by_key(|l| l.language_id.clone());

    let check_binary = |cmd: Option<&str>| match cmd {
        Some(cmd) => match helix_stdx::env::which(cmd) {
            Ok(_) => color(fit(&format!("✓ {}", cmd)), Color::Green),
            Err(_) => color(fit(&format!("✘ {}", cmd)), Color::Red),
        },
        None => color(fit("None"), Color::Yellow),
    };

    for lang in &syn_loader_conf.language {
        write!(stdout, "{}", fit(&lang.language_id))?;

        let mut cmds = lang.language_servers.iter().filter_map(|ls| {
            syn_loader_conf
                .language_server
                .get(&ls.name)
                .map(|config| config.command.as_str())
        });
        write!(stdout, "{}", check_binary(cmds.next()))?;