refactor: centralize home path shortening

This commit is contained in:
Peter Steinberger
2026-01-03 12:42:27 +01:00
parent 1ec3512925
commit 7a80e8fe77
3 changed files with 35 additions and 20 deletions

View File

@@ -1,5 +1,4 @@
import fs from "node:fs";
import os from "node:os";
import { lookupContextTokens } from "../agents/context.js";
import {
@@ -19,6 +18,7 @@ import {
type SessionEntry,
type SessionScope,
} from "../config/sessions.js";
import { shortenHomePath } from "../utils.js";
import type { ThinkLevel, VerboseLevel } from "./thinking.js";
type AgentConfig = NonNullable<ClawdisConfig["agent"]>;
@@ -53,13 +53,6 @@ const formatAge = (ms?: number | null) => {
const formatKTokens = (value: number) =>
`${(value / 1000).toFixed(value >= 10_000 ? 0 : 1)}k`;
const abbreviatePath = (p?: string) => {
if (!p) return undefined;
const home = os.homedir();
if (p.startsWith(home)) return p.replace(home, "~");
return p;
};
const formatTokens = (
total: number | null | undefined,
contextTokens: number | null,
@@ -187,7 +180,7 @@ export function buildStatusMessage(args: StatusArgs): string {
entry?.updatedAt
? `updated ${formatAge(now - entry.updatedAt)}`
: "no activity",
args.storePath ? `store ${abbreviatePath(args.storePath)}` : undefined,
args.storePath ? `store ${shortenHomePath(args.storePath)}` : undefined,
]
.filter(Boolean)
.join(" • ");
@@ -214,7 +207,7 @@ export function buildStatusMessage(args: StatusArgs): string {
const agentLine = `Agent: embedded pi • ${modelLabel}`;
const workspaceLine = args.workspaceDir
? `Workspace: ${abbreviatePath(args.workspaceDir)}`
? `Workspace: ${shortenHomePath(args.workspaceDir)}`
: undefined;
const helpersLine = "Shortcuts: /new reset | /restart relink";

View File

@@ -1,3 +1,5 @@
import { shortenHomeInString, shortenHomePath } from "../utils.js";
export const TOOL_RESULT_DEBOUNCE_MS = 500;
export const TOOL_RESULT_FLUSH_COUNT = 5;
@@ -23,17 +25,8 @@ function resolveToolEmoji(toolName?: string): string {
return "🧩";
}
function shortenHomeInString(input: string): string {
const home = process.env.HOME;
if (!home) return input;
return input.split(home).join("~");
}
export function shortenPath(p: string): string {
const home = process.env.HOME;
if (home && (p === home || p.startsWith(`${home}/`)))
return p.replace(home, "~");
return p;
return shortenHomePath(p);
}
export function shortenMeta(meta: string): string {

View File

@@ -104,5 +104,34 @@ export function resolveUserPath(input: string): string {
return path.resolve(trimmed);
}
export function resolveHomeDir(): string | undefined {
const envHome = process.env.HOME?.trim();
if (envHome) return envHome;
const envProfile = process.env.USERPROFILE?.trim();
if (envProfile) return envProfile;
try {
const home = os.homedir();
return home?.trim() ? home : undefined;
} catch {
return undefined;
}
}
export function shortenHomePath(input: string): string {
if (!input) return input;
const home = resolveHomeDir();
if (!home) return input;
if (input === home) return "~";
if (input.startsWith(`${home}/`)) return `~${input.slice(home.length)}`;
return input;
}
export function shortenHomeInString(input: string): string {
if (!input) return input;
const home = resolveHomeDir();
if (!home) return input;
return input.split(home).join("~");
}
// Fixed configuration root; legacy ~/.clawdis is no longer used.
export const CONFIG_DIR = path.join(os.homedir(), ".clawdis");