Skip to content

Commit

Permalink
Merge pull request #53 from ubiquity-whilefoo/comment-style
Browse files Browse the repository at this point in the history
Comment style
  • Loading branch information
ubiquity-os-beta[bot] authored Jan 24, 2025
2 parents 11f8e06 + 9c9479a commit 4e9d4f1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const COLORS = {
export const LOG_LEVEL = {
FATAL: "fatal",
ERROR: "error",
WARN: "warn",
INFO: "info",
VERBOSE: "verbose",
DEBUG: "debug",
Expand Down
54 changes: 27 additions & 27 deletions src/logs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LOG_LEVEL } from "./constants";
import { PrettyLogs } from "./pretty-logs";
import { LogParams, LogReturn, Metadata, LogLevel } from "./types/log-types";
import { LogParams, LogReturn, Metadata, LogLevel, LogLevelWithOk } from "./types/log-types";

export class Logs {
private _maxLevel = -1;
Expand Down Expand Up @@ -68,6 +68,17 @@ export class Logs {
});
}

public warn(log: string, metadata?: Metadata): LogReturn {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
level: LOG_LEVEL.WARN,
consoleLog: Logs.console.warn,
logMessage: log,
metadata,
type: "warn",
});
}

public error(log: string, metadata?: Metadata): LogReturn {
metadata = this._addDiagnosticInformation(metadata);
return this._log({
Expand Down Expand Up @@ -131,40 +142,27 @@ export class Logs {
Logs.console = new PrettyLogs();
}

private _diffColorCommentMessage(type: string, message: string) {
const diffPrefix = {
fatal: "-", // - text in red
ok: "+", // + text in green
error: "!", // ! text in orange
info: "#", // # text in gray
debug: "@@@@", // @@ text in purple (and bold)@@
private _diffColorCommentMessage(type: LogLevelWithOk, message: string) {
const diffPrefix: Record<LogLevelWithOk, string> = {
fatal: "> [!CAUTION]",
error: "> [!CAUTION]",
warn: "> [!WARNING]",
ok: "> [!TIP]",
info: "> [!NOTE]",
debug: "> [!IMPORTANT]",
verbose: "> [!NOTE]",
};
const selected = diffPrefix[type as keyof typeof diffPrefix];
const selected = diffPrefix[type];

if (selected) {
message = message
.trim()
.split("\n")
.map((line) => `${selected} ${line}`)
.join("\n");
} else if (type === "debug") {
// debug has special formatting
message = message
.split("\n")
.map((line) => `@@ ${line} @@`)
.join("\n"); // debug: "@@@@",
} else {
// default to gray
message = message
.split("\n")
.map((line) => `# ${line}`)
.map((line) => `> ${line}`)
.join("\n");
}

const diffHeader = "```diff";
const diffFooter = "```";

return [diffHeader, message, diffFooter].join("\n");
return [selected, message].join("\n");
}

private _getNumericLevel(level: LogLevel) {
Expand All @@ -173,8 +171,10 @@ export class Logs {
return 0;
case LOG_LEVEL.ERROR:
return 1;
case LOG_LEVEL.INFO:
case LOG_LEVEL.WARN:
return 2;
case LOG_LEVEL.INFO:
return 3;
case LOG_LEVEL.VERBOSE:
return 4;
case LOG_LEVEL.DEBUG:
Expand Down
7 changes: 7 additions & 0 deletions src/pretty-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class PrettyLogs {
this.info = this.info.bind(this);
this.error = this.error.bind(this);
this.fatal = this.fatal.bind(this);
this.warn = this.warn.bind(this);
this.debug = this.debug.bind(this);
this.verbose = this.verbose.bind(this);
}
Expand All @@ -18,6 +19,10 @@ export class PrettyLogs {
this._logWithStack(LOG_LEVEL.ERROR, message, metadata);
}

public warn(message: string, metadata?: Metadata | string) {
this._logWithStack(LOG_LEVEL.WARN, message, metadata);
}

public ok(message: string, metadata?: Metadata | string) {
this._logWithStack("ok", message, metadata);
}
Expand Down Expand Up @@ -99,6 +104,7 @@ export class PrettyLogs {
const defaultSymbols: Record<LogLevelWithOk, string> = {
fatal: "×",
ok: "✓",
warn: "⚠",
error: "⚠",
info: "›",
debug: "››",
Expand All @@ -124,6 +130,7 @@ export class PrettyLogs {
const colorMap: Record<LogLevelWithOk, [keyof typeof console, Colors]> = {
fatal: ["error", COLORS.fgRed],
ok: ["log", COLORS.fgGreen],
warn: ["warn", COLORS.fgYellow],
error: ["warn", COLORS.fgYellow],
info: ["info", COLORS.dim],
debug: ["debug", COLORS.fgMagenta],
Expand Down

0 comments on commit 4e9d4f1

Please sign in to comment.