import { UntrustedText } from "@effected/kit"; import { Option } from "effect"; import { sanitize } from "./Fmt.js"; import { relative, resolve } from "./internal/paths.js"; /** * How {@link CliPath} reads the separators of the paths it is given. * * @public */ export interface CliPathOptions { /** * The separator of the platform the paths came from, as `Path.sep` gives it. * * @remarks * `"/"` reads the paths as POSIX: `/` is the only separator and a backslash is a character of a name. `"\\"` reads * them as Windows: `/` and `\` both separate, a drive (`C:\`) or a share (`\\server\share`) is a root, and two * drives that differ only in case are the same drive. Omitted, or any other value, the paths decide: they are read * as Windows when one of them is spelled as only Windows spells an absolute path (a drive letter, a colon and a * separator, or two leading backslashes), and as POSIX otherwise. The host is never read. */ readonly separator?: string | undefined; } /** * The options of {@link CliPath.relative} and {@link CliPath.shown}. * * @public */ export interface CliPathRelativeOptions extends CliPathOptions { /** * The most `..` segments the relative path may start with. `0` admits only `base` itself and what lies under it. * A negative number, or one that is not a number, is `0`. * * @remarks * The two functions default differently. {@link CliPath.relative} has no limit when this is omitted, as a * platform's `Path.relative` has none: it answers where the path is. {@link CliPath.shown} defaults to `0`: it * answers what to print, and prints a path outside `base` as given. Pass `climb` to either when the two must * agree. */ readonly climb?: number | undefined; } /** * The options of {@link CliPath.shown}. * * @public */ export interface CliPathShownOptions extends CliPathRelativeOptions { /** * Show the path as one line of another party's text: on top of what `shown` always removes, private-use code * points are removed and every space look-alike and blank filler becomes an ordinary space. `false` by default. */ readonly strict?: boolean | undefined; /** * With `strict`, the longest result in UTF-16 code units, its ellipsis counted; unlimited by default. Without * `strict` it has no effect. */ readonly limit?: number | undefined; } /** * Paths as a report shows and links them: relative to a directory for the reader, absolute for the link. * * @remarks * Every member is a synchronous function of its arguments. None needs a `Path` service, reads the working directory * or touches the filesystem, so a pure function that maps a result to a view calls them directly, and none throws, * whatever text it is given. The comparison is of names: a symbolic link is not followed, and apart from a drive * letter, case matters. * * `.`, `..`, repeated separators and a trailing separator are resolved before anything is compared or returned. A * `..` at the root of an absolute path stays at the root; a relative path keeps the `..` it starts with. * * @example * ```ts * import { CliPath, Doc } from "@effected/cli-kit"; * * CliPath.shown("/repo", "/repo/okf/index.md"); // => "okf/index.md" * CliPath.shown("/repo", "/repo"); // => "." * CliPath.shown("/repo", "/etc/hosts"); // => "/etc/hosts" * CliPath.resolve("/", "okf/index.md"); // => "/okf/index.md" * * // A link to a file a report names relative to a directory: * Doc.link({ file: "okf/index.md", base: "/repo", line: 3 }, "okf/index.md:3"); * ``` * * @public */ export class CliPath { private constructor() {} /** * The path from `base` to `path`, joined with `/` on every platform: `.` when they are the same place, and `None` * when there is no such path or it climbs further than `options.climb` allows. * * @remarks * The result is data, with every character of every name kept: for a JSON field, a GitHub annotation's `file`, or a * key. Use {@link CliPath.shown} for text a person reads. * * There is no relative path between two roots: two drives, a drive and a share, or an absolute path and a relative * one. Two relative paths are taken as relative to the same directory, and the answer is `None` when it would * depend on a directory name neither path gives (`relative("../a", "b")`, `relative("a", "../b")`). * * **With no `climb` the result climbs as far as it must**, as a platform's `Path.relative` does: * `relative("/repo", "/etc/hosts")` is `Some("../etc/hosts")`. {@link CliPath.shown} defaults the other way, to * `climb: 0`, and gives `/etc/hosts` for the same two paths. So the two disagree about a path outside `base` * unless both are given the same `climb`: a JSON field built with `relative` and a line printed with `shown` * name such a path differently. * * With `climb: 0` the answer is the containment test: `Some` exactly when `path` is `base` or lies under it. * Pass it whenever a `None` is meant to say "outside `base`". * * @param base - the directory to start from * @param path - the path to reach * @param options - the separator the paths use, and how far the result may climb (no limit by default) */ static relative(base: string, path: string, options?: CliPathRelativeOptions): Option.Option { return relative(base, path, options); } /** * `path` as a report shows it from `base`: relative, joined with `/`, when it is `base` or lies under it, with `.` * for `base` itself, and `path` as it was given otherwise. * * @remarks * A path outside `base` stays as given because a chain of `..` says less than the absolute path does. Pass * `options.climb` to show a path up to that many directories above `base` relative too. * * **`climb` is `0` here by default, and unlimited in {@link CliPath.relative}.** `shown("/repo", "/etc/hosts")` * is `/etc/hosts` where `relative` gives `Some("../etc/hosts")`. Give both the same `climb` when a path a program * reads and the path a person is shown must be the same path. * * The result is made safe for a terminal, as `Fmt.sanitize` makes it and without its line breaks: escape * sequences, control characters, line breaks, bidi controls and invisible characters are removed and a tab * becomes a space, so it is always one line, and a name cannot move the cursor, forge a line or show its * extension somewhere else. It is for display only, and a name that held a removed character no longer names the * file. Use {@link CliPath.relative} or {@link CliPath.resolve} for a path a program will read back. * * `options.strict` is for a path the program does not control, such as the path of a checkout another party * names: the result then also goes through `UntrustedText.line` from `@effected/kit`, the form made for one line * of another party's text. Private-use code points are removed, the braille blank and the Hangul fillers become * a space, and every space that is not U+0020 (a no-break space, U+3000) becomes U+0020, so no look-alike of a * space or an icon glyph is drawn as part of a name. Ordinary spaces stay where they stand, runs of them * included, and nothing is trimmed: a path with two spaces still shows two. Nothing is cut unless `options.limit` * gives a length. * * @param base - the directory the reader is in, usually the working directory * @param path - the path to show * @param options - the separator the paths use, how far a relative result may climb (`0` by default), and * `strict` with its `limit` */ static shown(base: string, path: string, options?: CliPathShownOptions): string { const shown = Option.getOrElse(relative(base, path, { ...options, climb: options?.climb ?? 0 }), () => path); // Line breaks go first: a carriage return left alone would otherwise be sanitised to a space. const safe = sanitize(shown.replace(/[\r\n]/g, "")); return options?.strict === true ? UntrustedText.line(safe, { whitespace: "preserve", limit: options.limit ?? Number.POSITIVE_INFINITY }) : safe; } /** * Where `path` is from `base`: `path` itself when it is absolute, and `path` under `base` otherwise, normalized. * * @remarks * The two are joined with one separator whatever `base` ends in, so a `base` of `/` gives `/a` and never `//a`. The * result uses the separator of the reading: `/` for POSIX paths, `\` for Windows ones. A relative `base` gives a * relative result (`.` when nothing is left): the working directory is never read. * * Nothing confines the result to `base`: a `path` that climbs with `..` resolves outside it. To link a file only * when it stays under a directory, give the link target a `base` (see {@link LinkTarget}); to refuse an untrusted * path before reading or writing it, use `confineRelativeResult` from `@effected/walker`. * * @param base - the directory a relative `path` is under * @param path - an absolute path, or one relative to `base` * @param options - the separator the paths use */ static resolve(base: string, path: string, options?: CliPathOptions): string { return resolve(base, path, options); } }