本文整理汇总了TypeScript中@sirian/common.Str.padRight方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Str.padRight方法的具体用法?TypeScript Str.padRight怎么用?TypeScript Str.padRight使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@sirian/common.Str
的用法示例。
在下文中一共展示了Str.padRight方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: renderError
public renderError(e: Error) {
const stack = ErrorStackParser.parse(e);
const output = this.errorOutput;
let len = 0;
let title = "";
const message = Var.stringify(e.message).trim();
if ("" === message || output.isVerbose()) {
title = ` [${e.name || e.constructor.name}] `;
len = StrUtil.width(title);
}
const width = output.getWidth();
const lines: Array<[string, number]> = [];
if (message) {
for (const line of message.split(/\r?\n/)) {
for (const chunk of StrUtil.splitByWidth(line, width - 4)) {
const lineLength = StrUtil.width(chunk) + 4;
lines.push([chunk, lineLength]);
if (lineLength > len) {
len = lineLength;
}
}
}
}
const messages = [];
const emptyLine = `<error>${StrUtil.spaces(len)}</error>`;
messages.push(emptyLine);
if ("" === message || output.isVerbose()) {
messages.push(`<error>${Str.padRight(title, len)}</error>`);
}
for (const [line, lineLength] of lines) {
messages.push(`<error> ${Formatter.escape(line)} ${StrUtil.spaces(len - lineLength)}</error>`);
}
messages.push(emptyLine);
messages.push("");
if (output.isVerbose() && stack.length) {
messages.push("<comment>Error trace:</comment>");
for (const frame of stack) {
const fn = frame.functionName || "{anonymous}";
const args = (frame.args || []).join(", ");
const file = frame.fileName || "";
const line = frame.lineNumber;
const col = frame.columnNumber;
messages.push(
Formatter.format(` %s(%s) at <info>%s</info> <comment>%d:%d</comment>`, fn, args, file, line, col),
);
}
messages.push("");
messages.push("");
}
output.writeln(messages, {verbosity: OutputVerbosity.QUIET});
}
示例2: find
public async find(name: string) {
const commands = this.commands;
const commandNames = [...commands.keys()];
if (this.commandLoader) {
commandNames.push(...this.commandLoader.getNames());
}
const expr = name.replace(/([^:]+|)/, (text) => Rgx.escape(text) + "[^:]*");
const re = new RegExp("^" + expr);
const rei = new RegExp("^" + expr, "i");
let found = commandNames.filter((c) => re.test(c));
if (!found.length) {
found.push(...commandNames.filter((c) => rei.test(c)));
}
// if no found matched or we just matched namespaces
if (!found.length || !found.filter((c) => rei.test(c)).length) {
if (name.includes(":")) {
// check if a namespace exists and contains found
const pos = name.indexOf(":");
this.findNamespace(name.substr(0, pos));
}
let message = `Command "${name}" is not defined.`;
const alternatives = this.findAlternatives(name, commandNames);
if (alternatives.length) {
if (1 === alternatives.length) {
message += "\n\nDid you mean this?\n ";
} else {
message += "\n\nDid you mean one of these?\n ";
}
message += alternatives.join("\n ");
}
throw new CommandNotFoundError(message, alternatives);
}
const aliases = new Map();
// filter out aliases for found which are already on the list
if (found.length > 1) {
const filteredCommands = new Set<string>();
for (const nameOrAlias of found) {
let commandName = nameOrAlias;
if (commands.has(nameOrAlias)) {
const command = commands.get(nameOrAlias)!;
commandName = command.getDefinition().getName();
}
aliases.set(nameOrAlias, commandName);
if (commandName === nameOrAlias || !found.includes(commandName)) {
filteredCommands.add(nameOrAlias);
}
}
found = [...filteredCommands];
}
const exact = found.includes(name) || aliases.has(name);
if (found.length > 1 && !exact) {
const widths = found.map((abbr) => StrUtil.width(abbr));
const maxLen = Math.max(...widths);
const abbrevs = [];
for (const cmdName of found) {
const command = await this.getCommand(cmdName);
const description = command.getDefinition().getDescription();
const abbr = sprintf("%s %s", Str.padRight(cmdName, maxLen), description);
abbrevs.push(abbr);
}
const suggestions = this.getAbbreviationSuggestions(abbrevs);
throw new CommandNotFoundError(
`Command "${name}" is ambiguous.\nDid you mean one of these?\n${suggestions}`,
found,
);
}
return this.getCommand(exact ? name : found[0]);
}
示例3:
.map((a) => Str.padRight(a.join(""), width, " "));
示例4: render
public render(messages: string | string[]) {
messages = Arr.cast(messages);
const options = this.options;
const padding = options.padding || [];
const padTop = padding.length > 0 ? padding[0] : 0;
const padRight = padding.length > 1 ? padding[1] : padTop;
const padBottom = padding.length > 2 ? padding[2] : padTop;
const padLeft = padding.length > 3 ? padding[3] : padRight;
const lines: string[] = [];
const output = this.output;
const formatter = output.getFormatter();
const type = Var.stringify(options.type);
const typeWidth = type ? StrUtil.width(type) + 1 : 0;
const maxMessageWidth = Math.max(
options.minWidth,
...messages.map((msg) => formatter.widthWithoutDecoration(msg)),
);
const maxWidth = Math.min(
output.getWidth(),
options.maxWidth,
maxMessageWidth + typeWidth + padRight + padLeft,
);
const chunkWidth = maxWidth - typeWidth - padLeft - padRight;
for (const message of messages) {
const chunks = StrUtil.splitByWidth(message, chunkWidth);
for (const chunk of chunks) {
const prefix = 0 === lines.length ? type : "";
const parts = [
StrUtil.spaces(padLeft),
Str.padRight(prefix, typeWidth),
Str.padRight(chunk, chunkWidth),
StrUtil.spaces(padRight),
];
lines.push(parts.join(""));
}
}
const emptyLine = StrUtil.spaces(maxWidth);
if (output.isDecorated()) {
for (let i = 0; i < padTop; i++) {
lines.unshift(emptyLine);
}
for (let i = 0; i < padBottom; i++) {
lines.push(emptyLine);
}
}
for (const line of lines) {
const text = Formatter.formatText(line, options.style);
output.writeln(text);
}
}