@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
65 lines • 2.68 kB
JavaScript
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
import { specOf } from "../../spec/builtins.js";
import { FlagView } from "../../spec/flag_view.js";
import { IOResult } from "../../../io/types.js";
import { stdinStream } from "../utils/stream.js";
import { perOperand } from "../utils/operands.js";
import { StreamRefusal } from "../../../rs/errors.js";
import { refused, scan } from "../../../rs/scan.js";
import { runStream } from "../../../rs/stream.js";
import { byteView } from "../../../shell/bytes.js";
/**
* fmt's options, -w and -g read by mirage-rs before any operand is resolved:
* the flags, or fmt's refusal of a width. Mirrors Python's `parse_flags`.
*/
export function parseFlags(bag) {
const fl = new FlagView(bag, specOf('fmt'));
let values;
try {
values = scan('fmt', fl.texts('width', 'goal'));
}
catch (err) {
if (!(err instanceof StreamRefusal))
throw err;
return refused(err);
}
const { width, goal } = values;
return {
width: typeof width === 'number' ? width : null,
goal: typeof goal === 'number' ? goal : null,
prefix: fl.asStr('prefix') ?? null,
splitOnly: fl.asBool('split_only'),
tagged: fl.asBool('tagged_paragraph'),
crown: fl.asBool('crown_margin'),
uniform: fl.asBool('uniform_spacing'),
};
}
// Each operand is reflowed on its own. Mirrors Python's fmt_generic.
export function fmtGeneric(parsed, paths, ctx, stream) {
stream = stdinStream(stream, ctx.stdin);
const config = {
width: parsed.width,
goal: parsed.goal,
prefix: parsed.prefix === null ? null : byteView(parsed.prefix),
split_only: parsed.splitOnly,
tagged: parsed.tagged,
crown: parsed.crown,
uniform: parsed.uniform,
};
const io = new IOResult();
const each = (source) => runStream('fmt', config, source, ctx.signal);
return [perOperand(paths, stream, ctx.stdin, io, 'fmt', each), io];
}
//# sourceMappingURL=fmt.js.map