clo4 / run-fig

Incredibly fast startup command line apps with JavaScript and TypeScript
https://doc.deno.land/https://denopkg.com/SeparateRecords/run-fig/mod.ts
MIT License
3 stars 0 forks source link

Add description dedenting for help #8

Closed clo4 closed 1 year ago

clo4 commented 2 years ago

Descriptions aren't "dedented" (un-indented) at the moment. There's a proposed String.dedent function, could implement this in the help system.

This would not apply actively, only when formatting the description for help.

clo4 commented 2 years ago

Until String.dedent is implemented, here's roughly what I'll do:

// If the string is empty, return the input.
// If the string doesn't start with a newline, return the input.
// Otherwise, trim the first line and the last line. Remove the same
// number of characters from the beginning of each line as the number
// of whitespace characters in front of the first line.
function dedent(str: string) {
  if (str[0] !== "\n") return str;
  const lines = str.split("\n").slice(1, -1);
  const margin = lines[0].length - lines[0].trimStart().length;
  return lines.map((line) => line.slice(margin)).join("\n");
}

(will benchmark implementations)