refactor(cli): share runtime status color rendering
This commit is contained in:
16
src/cli/daemon-cli/shared.test.ts
Normal file
16
src/cli/daemon-cli/shared.test.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { theme } from "../../terminal/theme.js";
|
||||
import { resolveRuntimeStatusColor } from "./shared.js";
|
||||
|
||||
describe("resolveRuntimeStatusColor", () => {
|
||||
it("maps known runtime states to expected theme colors", () => {
|
||||
expect(resolveRuntimeStatusColor("running")).toBe(theme.success);
|
||||
expect(resolveRuntimeStatusColor("stopped")).toBe(theme.error);
|
||||
expect(resolveRuntimeStatusColor("unknown")).toBe(theme.muted);
|
||||
});
|
||||
|
||||
it("falls back to warning color for unexpected states", () => {
|
||||
expect(resolveRuntimeStatusColor("degraded")).toBe(theme.warn);
|
||||
expect(resolveRuntimeStatusColor(undefined)).toBe(theme.muted);
|
||||
});
|
||||
});
|
||||
@@ -27,6 +27,17 @@ export function createCliStatusTextStyles() {
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveRuntimeStatusColor(status: string | undefined): (value: string) => string {
|
||||
const runtimeStatus = status ?? "unknown";
|
||||
return runtimeStatus === "running"
|
||||
? theme.success
|
||||
: runtimeStatus === "stopped"
|
||||
? theme.error
|
||||
: runtimeStatus === "unknown"
|
||||
? theme.muted
|
||||
: theme.warn;
|
||||
}
|
||||
|
||||
export function parsePortFromArgs(programArguments: string[] | undefined): number | null {
|
||||
if (!programArguments?.length) {
|
||||
return null;
|
||||
|
||||
@@ -12,13 +12,14 @@ import {
|
||||
import { isWSLEnv } from "../../infra/wsl.js";
|
||||
import { getResolvedLoggerSettings } from "../../logging.js";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import { colorize, theme } from "../../terminal/theme.js";
|
||||
import { colorize } from "../../terminal/theme.js";
|
||||
import { shortenHomePath } from "../../utils.js";
|
||||
import { formatCliCommand } from "../command-format.js";
|
||||
import {
|
||||
createCliStatusTextStyles,
|
||||
filterDaemonEnv,
|
||||
formatRuntimeStatus,
|
||||
resolveRuntimeStatusColor,
|
||||
renderRuntimeHints,
|
||||
safeDaemonEnv,
|
||||
} from "./shared.js";
|
||||
@@ -165,15 +166,7 @@ export function printDaemonStatus(status: DaemonStatus, opts: { json: boolean })
|
||||
|
||||
const runtimeLine = formatRuntimeStatus(service.runtime);
|
||||
if (runtimeLine) {
|
||||
const runtimeStatus = service.runtime?.status ?? "unknown";
|
||||
const runtimeColor =
|
||||
runtimeStatus === "running"
|
||||
? theme.success
|
||||
: runtimeStatus === "stopped"
|
||||
? theme.error
|
||||
: runtimeStatus === "unknown"
|
||||
? theme.muted
|
||||
: theme.warn;
|
||||
const runtimeColor = resolveRuntimeStatusColor(service.runtime?.status);
|
||||
defaultRuntime.log(`${label("Runtime:")} ${colorize(rich, runtimeColor, runtimeLine)}`);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user