@orkestrel/console 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1002,10 +1002,10 @@ export declare const LEVELS: readonly LogLevel[];
1002
1002
  */
1003
1003
  export declare class Logger implements LoggerInterface {
1004
1004
  #private;
1005
+ readonly name?: string;
1005
1006
  constructor(options?: LoggerOptions);
1006
1007
  get emitter(): EmitterInterface<LoggerEventMap>;
1007
1008
  get level(): LogLevel;
1008
- get name(): string | undefined;
1009
1009
  debug(message: string, data?: Record<string, unknown>): void;
1010
1010
  info(message: string, data?: Record<string, unknown>): void;
1011
1011
  warn(message: string, data?: Record<string, unknown>): void;
@@ -1002,10 +1002,10 @@ export declare const LEVELS: readonly LogLevel[];
1002
1002
  */
1003
1003
  export declare class Logger implements LoggerInterface {
1004
1004
  #private;
1005
+ readonly name?: string;
1005
1006
  constructor(options?: LoggerOptions);
1006
1007
  get emitter(): EmitterInterface<LoggerEventMap>;
1007
1008
  get level(): LogLevel;
1008
- get name(): string | undefined;
1009
1009
  debug(message: string, data?: Record<string, unknown>): void;
1010
1010
  info(message: string, data?: Record<string, unknown>): void;
1011
1011
  warn(message: string, data?: Record<string, unknown>): void;
@@ -430,7 +430,7 @@ var ConsoleError = class extends Error {
430
430
  super(message);
431
431
  this.name = "ConsoleError";
432
432
  this.code = code;
433
- this.context = context;
433
+ if (context !== void 0) this.context = context;
434
434
  }
435
435
  };
436
436
  /**
@@ -754,20 +754,21 @@ function renderBox(options) {
754
754
  const inner = lines.reduce((max, line) => Math.max(max, width(line)), Math.max(0, titleRoom, budget));
755
755
  const gutter = " ".repeat(padding);
756
756
  const bar = paint(styler, chars.vertical);
757
- const boxTop = () => {
758
- const span = inner + padding * 2;
759
- if (options.title === void 0) return paint(styler, `${chars.topLeft}${repeatTo(chars.horizontal, span)}${chars.topRight}`);
757
+ const span = inner + padding * 2;
758
+ let top;
759
+ if (options.title === void 0) top = paint(styler, `${chars.topLeft}${repeatTo(chars.horizontal, span)}${chars.topRight}`);
760
+ else {
760
761
  const caption = ` ${options.title} `;
761
762
  const room = span - width(caption);
762
763
  const lead = paint(styler, repeatTo(chars.horizontal, 1));
763
764
  const rest = room - 1 <= 0 ? "" : paint(styler, repeatTo(chars.horizontal, room - 1));
764
- return `${paint(styler, chars.topLeft)}${lead}${paint(styler, caption)}${rest}${paint(styler, chars.topRight)}`;
765
- };
766
- const top = boxTop();
765
+ top = `${paint(styler, chars.topLeft)}${lead}${paint(styler, caption)}${rest}${paint(styler, chars.topRight)}`;
766
+ }
767
767
  const bottom = paint(styler, `${chars.bottomLeft}${repeatTo(chars.horizontal, inner + padding * 2)}${chars.bottomRight}`);
768
+ const body = lines.map((line) => `${bar}${gutter}${align(line, inner)}${gutter}${bar}`);
768
769
  return [
769
770
  top,
770
- ...lines.map((line) => `${bar}${gutter}${align(line, inner)}${gutter}${bar}`),
771
+ ...body,
771
772
  bottom
772
773
  ].join("\n");
773
774
  }
@@ -798,22 +799,20 @@ function renderTable(options) {
798
799
  const columns = options.columns;
799
800
  const widths = columns.map((column, index) => options.rows.reduce((max, row) => Math.max(max, width(cellAt(row, index))), width(column.label)));
800
801
  const aligns = columns.map((column) => column.align ?? "left");
801
- const tableRow = (cells) => {
802
+ const renderedRows = [columns.map((column) => paint(styler, column.label)), ...options.rows.map((row) => columns.map((_column, index) => cellAt(row, index)))].map((cells) => {
802
803
  const bar = paint(styler, chars.vertical);
803
804
  return `${bar}${cells.map((cell, index) => ` ${align(cell, widths[index] ?? width(cell), aligns[index] ?? "left")} `).join(bar)}${bar}`;
804
- };
805
- const tableEdge = (left, mid, right) => {
806
- const segments = widths.map((columnWidth) => repeatTo(chars.horizontal, columnWidth + 2));
807
- return paint(styler, `${left}${segments.join(mid)}${right}`);
808
- };
809
- const header = tableRow(columns.map((column) => paint(styler, column.label)));
810
- const body = options.rows.map((row) => tableRow(columns.map((_column, index) => cellAt(row, index))));
805
+ });
806
+ const segments = widths.map((columnWidth) => repeatTo(chars.horizontal, columnWidth + 2));
807
+ const top = paint(styler, `${chars.topLeft}${segments.join(chars.teeDown)}${chars.topRight}`);
808
+ const rule = paint(styler, `${chars.teeRight}${segments.join(chars.cross)}${chars.teeLeft}`);
809
+ const bottom = paint(styler, `${chars.bottomLeft}${segments.join(chars.teeUp)}${chars.bottomRight}`);
811
810
  return [
812
- tableEdge(chars.topLeft, chars.teeDown, chars.topRight),
813
- header,
814
- tableEdge(chars.teeRight, chars.cross, chars.teeLeft),
815
- ...body,
816
- tableEdge(chars.bottomLeft, chars.teeUp, chars.bottomRight)
811
+ top,
812
+ ...renderedRows.slice(0, 1),
813
+ rule,
814
+ ...renderedRows.slice(1),
815
+ bottom
817
816
  ].join("\n");
818
817
  }
819
818
  /**
@@ -1075,7 +1074,7 @@ var Styler = class Styler {
1075
1074
  * type assertion is used (AGENTS §1 / §14 — narrow, never assert).
1076
1075
  */
1077
1076
  get surface() {
1078
- const render = (text) => this.#enabled ? this.#renderer.render(this.#style, text) : text;
1077
+ const render = this.#render.bind(this);
1079
1078
  const descriptors = {
1080
1079
  style: {
1081
1080
  value: this.#style,
@@ -1087,17 +1086,26 @@ var Styler = class Styler {
1087
1086
  }
1088
1087
  };
1089
1088
  for (const color of COLORS) descriptors[color] = {
1090
- get: () => this.#foreground(color).surface,
1089
+ get: this.#foregroundSurface.bind(this, color),
1091
1090
  enumerable: true
1092
1091
  };
1093
1092
  for (const attribute of ATTRIBUTES) descriptors[attribute] = {
1094
- get: () => this.#attribute(attribute).surface,
1093
+ get: this.#attributeSurface.bind(this, attribute),
1095
1094
  enumerable: true
1096
1095
  };
1097
1096
  const surface = Object.defineProperties(render, descriptors);
1098
1097
  if (this.#isSurface(surface)) return surface;
1099
1098
  throw new ConsoleError("INVARIANT", "console: styler surface construction is incomplete");
1100
1099
  }
1100
+ #render(text) {
1101
+ return this.#enabled ? this.#renderer.render(this.#style, text) : text;
1102
+ }
1103
+ #foregroundSurface(color) {
1104
+ return this.#foreground(color).surface;
1105
+ }
1106
+ #attributeSurface(attribute) {
1107
+ return this.#attribute(attribute).surface;
1108
+ }
1101
1109
  #isSurface(value) {
1102
1110
  return typeof value === "function" && "style" in value && "enabled" in value && "red" in value && "bold" in value;
1103
1111
  }
@@ -1160,8 +1168,8 @@ var Capture = class {
1160
1168
  #active = false;
1161
1169
  constructor(options) {
1162
1170
  this.#emitter = new Emitter({
1163
- on: options?.on,
1164
- error: options?.error
1171
+ ...options?.on !== void 0 ? { on: options.on } : {},
1172
+ ...options?.error !== void 0 ? { error: options.error } : {}
1165
1173
  });
1166
1174
  this.#levels = options?.levels ?? DEFAULT_CAPTURE_LEVELS;
1167
1175
  this.#mirror = options?.mirror ?? false;
@@ -1183,7 +1191,7 @@ var Capture = class {
1183
1191
  const original = target[level];
1184
1192
  this.#originals.set(level, original);
1185
1193
  const mirror = original.bind(console);
1186
- target[level] = (...args) => this.#intercept(level, args, mirror);
1194
+ target[level] = this.#captureCall.bind(this, level, mirror);
1187
1195
  }
1188
1196
  this.#emitter.emit("start");
1189
1197
  }
@@ -1207,6 +1215,9 @@ var Capture = class {
1207
1215
  this.stop();
1208
1216
  this.#emitter.destroy();
1209
1217
  }
1218
+ #captureCall(level, mirror, ...args) {
1219
+ this.#intercept(level, args, mirror);
1220
+ }
1210
1221
  #intercept(level, args, mirror) {
1211
1222
  const message = this.#capture(level, args);
1212
1223
  this.#retain(message);
@@ -1285,11 +1296,11 @@ var LoggerManager = class {
1285
1296
  }
1286
1297
  register(name, options) {
1287
1298
  const logger = new Logger({
1288
- level: this.#level,
1289
- sink: this.#sink,
1290
- styler: this.#styler,
1291
- limit: this.#limit,
1292
- silent: this.#silent,
1299
+ ...this.#level !== void 0 ? { level: this.#level } : {},
1300
+ ...this.#sink !== void 0 ? { sink: this.#sink } : {},
1301
+ ...this.#styler !== void 0 ? { styler: this.#styler } : {},
1302
+ ...this.#limit !== void 0 ? { limit: this.#limit } : {},
1303
+ ...this.#silent !== void 0 ? { silent: this.#silent } : {},
1293
1304
  ...options,
1294
1305
  name
1295
1306
  });
@@ -1372,8 +1383,8 @@ var Progress = class {
1372
1383
  #completed = false;
1373
1384
  constructor(options) {
1374
1385
  this.#emitter = new Emitter({
1375
- on: options.on,
1376
- error: options.error
1386
+ ...options.on !== void 0 ? { on: options.on } : {},
1387
+ ...options.error !== void 0 ? { error: options.error } : {}
1377
1388
  });
1378
1389
  this.#total = options.total;
1379
1390
  this.#width = options.width ?? 30;
@@ -1571,8 +1582,8 @@ var Spinner = class {
1571
1582
  #index = 0;
1572
1583
  constructor(options) {
1573
1584
  this.#emitter = new Emitter({
1574
- on: options?.on,
1575
- error: options?.error
1585
+ ...options?.on !== void 0 ? { on: options.on } : {},
1586
+ ...options?.error !== void 0 ? { error: options.error } : {}
1576
1587
  });
1577
1588
  const frames = options?.frames ?? SPINNER_FRAMES;
1578
1589
  this.#frames = frames.length === 0 ? SPINNER_FRAMES : frames;
@@ -1909,21 +1920,25 @@ function createCapture(options) {
1909
1920
  function withCapture(fn, options) {
1910
1921
  const capture = new Capture(options);
1911
1922
  capture.start();
1912
- const settle = (value) => {
1913
- const messages = capture.messages();
1914
- capture.destroy();
1915
- return {
1916
- value,
1917
- messages
1918
- };
1919
- };
1920
1923
  try {
1921
1924
  const result = fn();
1922
- if (result instanceof Promise) return result.then((value) => settle(value), (error) => {
1925
+ if (result instanceof Promise) return result.then((value) => {
1926
+ const messages = capture.messages();
1927
+ capture.destroy();
1928
+ return {
1929
+ value,
1930
+ messages
1931
+ };
1932
+ }, (error) => {
1923
1933
  capture.destroy();
1924
1934
  throw error;
1925
1935
  });
1926
- return settle(result);
1936
+ const messages = capture.messages();
1937
+ capture.destroy();
1938
+ return {
1939
+ value: result,
1940
+ messages
1941
+ };
1927
1942
  } catch (error) {
1928
1943
  capture.destroy();
1929
1944
  throw error;
@@ -2028,19 +2043,19 @@ function createProgress(options) {
2028
2043
  var Logger = class {
2029
2044
  #emitter;
2030
2045
  #level;
2031
- #name;
2032
2046
  #sink;
2033
2047
  #styler;
2034
2048
  #limit;
2035
2049
  #silent;
2036
2050
  #entries = [];
2051
+ name;
2037
2052
  constructor(options) {
2038
2053
  this.#emitter = new Emitter({
2039
- on: options?.on,
2040
- error: options?.error
2054
+ ...options?.on !== void 0 ? { on: options.on } : {},
2055
+ ...options?.error !== void 0 ? { error: options.error } : {}
2041
2056
  });
2042
2057
  this.#level = options?.level ?? "info";
2043
- this.#name = options?.name;
2058
+ if (options?.name !== void 0) this.name = options.name;
2044
2059
  this.#sink = options?.sink ?? createConsoleSink();
2045
2060
  this.#styler = options?.styler ?? createStyler();
2046
2061
  this.#limit = options?.limit ?? 1e3;
@@ -2052,9 +2067,6 @@ var Logger = class {
2052
2067
  get level() {
2053
2068
  return this.#level;
2054
2069
  }
2055
- get name() {
2056
- return this.#name;
2057
- }
2058
2070
  debug(message, data) {
2059
2071
  this.#log("debug", message, data);
2060
2072
  }
@@ -2090,7 +2102,7 @@ var Logger = class {
2090
2102
  level,
2091
2103
  message,
2092
2104
  time: Date.now(),
2093
- ...this.#name === void 0 ? {} : { name: this.#name },
2105
+ ...this.name === void 0 ? {} : { name: this.name },
2094
2106
  ...data === void 0 ? {} : { data: Object.freeze({ ...data }) }
2095
2107
  });
2096
2108
  }