Skip to content

Commit ec21dd2

Browse files
refactor(hm-plugin-cloud): render_prefs returns a (bool, bool) tuple whose two fields are trivially swappable (#118)
1 parent 50f3b97 commit ec21dd2

3 files changed

Lines changed: 34 additions & 16 deletions

File tree

crates/hm-plugin-cloud/src/settings.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,36 @@ pub fn anon_client() -> Result<(HarmontClient, String)> {
7070

7171
/// Render preferences for cloud commands that stream through `hm-render`.
7272
///
73-
/// Returns `(color, logs)`, both derived from `hm-render`'s shared TTY/color
74-
/// helpers (the single source of truth, also used by `hm/src/context.rs`):
75-
/// - `color` — ANSI enabled when `NO_COLOR` is unset and stderr is a TTY.
76-
/// The plugin verbs have no `--no-color` flag, so we pass `false` for the
77-
/// flag; the `--no-color` asymmetry vs. the host `hm run` path is explicit
78-
/// here at the call site.
79-
/// - `logs` — force the streaming `HumanRenderer` over the live
80-
/// `ProgressRenderer`. True when stdout is **not** an interactive terminal
81-
/// (CI / pipe / log file), so nothing animates into a non-TTY sink.
82-
#[must_use]
83-
pub fn render_prefs() -> (bool, bool) {
84-
(hm_render::color_enabled(false), hm_render::stdout_piped())
73+
/// Both fields are derived from `hm-render`'s shared TTY/color helpers (the
74+
/// single source of truth, also used by `hm/src/context.rs`).
75+
#[derive(Debug, Clone, Copy)]
76+
pub struct RenderPrefs {
77+
/// ANSI enabled when `NO_COLOR` is unset and stderr is a TTY.
78+
///
79+
/// The plugin verbs have no `--no-color` flag, so we pass `false` for the
80+
/// flag; the `--no-color` asymmetry vs. the host `hm run` path is explicit
81+
/// here at the call site.
82+
pub color: bool,
83+
/// Force the streaming `HumanRenderer` over the live `ProgressRenderer`.
84+
///
85+
/// True when stdout is **not** an interactive terminal (CI / pipe / log
86+
/// file), so nothing animates into a non-TTY sink.
87+
pub logs: bool,
88+
}
89+
90+
impl RenderPrefs {
91+
/// Detect render preferences from the live environment via `hm-render`'s
92+
/// shared TTY/color helpers — the single source of truth, also used by
93+
/// `hm/src/context.rs`. This inspects `NO_COLOR` and the stdout/stderr
94+
/// TTY state at call time, so it is a deliberate observation of the
95+
/// environment rather than a constant default.
96+
#[must_use]
97+
pub fn detect() -> Self {
98+
Self {
99+
color: hm_render::color_enabled(false),
100+
logs: hm_render::stdout_piped(),
101+
}
102+
}
85103
}
86104

87105
/// Map a raw generated-client error into an `anyhow` error with a readable

crates/hm-plugin-cloud/src/verbs/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ async fn watch(client: &HarmontClient, org: &str, pipe: &str, number: i64) -> Re
5656
// Render the live build through the shared `hm-render` renderers (the same
5757
// ones a local `hm run` uses), driven by the `BuildEvent`s `watch_build`
5858
// emits over an mpsc channel.
59-
let (color, logs) = crate::settings::render_prefs();
60-
let renderer = hm_render::renderer_for("human", color, logs)?;
59+
let prefs = crate::settings::RenderPrefs::detect();
60+
let renderer = hm_render::renderer_for("human", prefs.color, prefs.logs)?;
6161
let (tx, rx) = tokio::sync::mpsc::channel(1024);
6262
let driver = tokio::spawn(hm_render::drive(renderer, rx));
6363

crates/hm-plugin-cloud/src/verbs/job.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ async fn log_cmd(
6969
let token = client.log_token(org, pipe, build).await?;
7070
let log_base = client.base_url().to_string();
7171

72-
let (color, _logs) = settings::render_prefs();
72+
let prefs = settings::RenderPrefs::detect();
7373
// A single-job tail is always a flat log stream, so force the streaming
7474
// HumanRenderer (logs = true) regardless of TTY.
75-
let renderer = hm_render::renderer_for("human", color, true)?;
75+
let renderer = hm_render::renderer_for("human", prefs.color, true)?;
7676
let (tx, rx) = tokio::sync::mpsc::channel(1024);
7777
let driver = tokio::spawn(hm_render::drive(renderer, rx));
7878

0 commit comments

Comments
 (0)