MCPcopy Create free account
hub / github.com/Snapchat/Valdi / formatMessage

Function formatMessage

valdi/vscode_debugger/src/adapter/messageFormat.ts:63–112  ·  view source on GitHub ↗
(
  format: string,
  substitutions: ReadonlyArray<T>,
  formatters: Formatters<T>,
)

Source from the content-addressed store, hash-verified

61}
62
63export function formatMessage<T>(
64 format: string,
65 substitutions: ReadonlyArray<T>,
66 formatters: Formatters<T>,
67): { result: string; usedAllSubs: boolean } {
68 const tokens = tokenizeFormatString(format, Array.from(formatters.keys()));
69 const usedSubstitutionIndexes = new Set<number>();
70 const defaultFormatter = formatters.get('');
71 if (!defaultFormatter) {
72 throw new Error('Expected to hav a default formatter');
73 }
74
75 const builder = new BudgetStringBuilder(maxMessageFormatLength);
76 let cssFormatApplied = false;
77 for (let i = 0; builder.checkBudget() && i < tokens.length; ++i) {
78 const token = tokens[i];
79 if (token.type === 'string') {
80 builder.append(token.value);
81 continue;
82 }
83
84 const index = token.substitutionIndex;
85 if (index >= substitutions.length) {
86 // If there are not enough substitutions for the current substitutionIndex
87 // just output the format specifier literally and move on.
88 builder.append('%' + (token.precision || '') + token.specifier);
89 continue;
90 }
91 usedSubstitutionIndexes.add(index);
92 if (token.specifier === 'c') cssFormatApplied = true;
93 const formatter = formatters.get(token.specifier) || defaultFormatter;
94 builder.append(formatter(substitutions[index], { budget: builder.budget(), quoted: false }));
95 }
96
97 if (cssFormatApplied) builder.append('\x1b[0m'); // clear format
98
99 for (let i = 0; builder.checkBudget() && i < substitutions.length; ++i) {
100 if (usedSubstitutionIndexes.has(i)) continue;
101 usedSubstitutionIndexes.add(i);
102 if (format || i)
103 // either we are second argument or we had format.
104 builder.append(' ');
105 builder.append(defaultFormatter(substitutions[i], { budget: builder.budget(), quoted: false }));
106 }
107
108 return {
109 result: builder.build(),
110 usedAllSubs: usedSubstitutionIndexes.size === substitutions.length,
111 };
112}
113
114function escapeAnsiColor(colorString: string): number | undefined {
115 try {

Callers

nothing calls this directly

Calls 11

checkBudgetMethod · 0.95
appendMethod · 0.95
budgetMethod · 0.95
buildMethod · 0.95
tokenizeFormatStringFunction · 0.85
defaultFormatterFunction · 0.85
keysMethod · 0.65
getMethod · 0.65
addMethod · 0.65
hasMethod · 0.65
fromMethod · 0.45

Tested by

no test coverage detected