```ts
/**
 * Cuts a line down to at most a given number of characters.
 * A line that had to be cut ends in three dots, and those dots count toward the limit.
 * A line already short enough is left alone.
 * Rejects a limit under four with an error.
 *
 * @param line - The line to trim.
 * @param limit - The maximum length of the line after trimming.
 * @returns The trimmed line.
 */
export function trimTo(line: string, limit: number): string {
  if (limit < 4) {
    throw new Error('Limit must be at least 4 characters.');
  }

  if (line.length <= limit) {
    return line;
  }

  const lastSpaceIndex = line.lastIndexOf(' ', limit - 3);
  if (lastSpaceIndex !== -1) {
    return line.substring(0, lastSpaceIndex) + '...';
  } else {
    return line.substring(0, limit - 3) + '...';
  }
}
```