use-term 1.0.0 → 1.1.0

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.
package/README.md CHANGED
@@ -49,25 +49,29 @@ import term from "use-term";
49
49
  // --- A. Using inside a class (automatically gets class name) ---
50
50
  class AuthService {
51
51
  login() {
52
- term.info(this, "Starting login process...");
52
+ term.info("Starting login process...", undefined, this);
53
53
 
54
54
  const user = { id: 42, name: "Alex", roles: ["admin", "billing"] };
55
- term.success(this, "Credentials validated successfully", user);
55
+ term.success("Credentials validated successfully", user, this);
56
56
  }
57
57
  }
58
58
 
59
59
  // --- B. Using inside a function (automatically gets function name) ---
60
60
  function processPayment() {
61
- term.warn(processPayment, "Payment gateway is responding slowly", {
62
- delayMs: 1500,
63
- });
64
- term.error(processPayment, "Payment gateway connection timeout", {
65
- code: "ETIMEDOUT",
66
- });
61
+ term.warn(
62
+ "Payment gateway is responding slowly",
63
+ { delayMs: 1500 },
64
+ processPayment,
65
+ );
66
+ term.error(
67
+ "Payment gateway connection timeout",
68
+ { code: "ETIMEDOUT" },
69
+ processPayment,
70
+ );
67
71
  }
68
72
 
69
73
  // --- C. Using custom string scopes ---
70
- term.info("Server", "Server initialized on port 3000", { env: "production" });
74
+ term.info("Server initialized on port 3000", { env: "production" }, "Server");
71
75
 
72
76
  // --- D. Visual titles ---
73
77
  term.title("Authentication Flow");
@@ -88,13 +92,13 @@ For traditional Node.js applications:
88
92
  const term = require("use-term");
89
93
 
90
94
  // Easily log with CommonJS:
91
- term.info("CJS", "Logging using standard require() syntax!");
95
+ term.info("Logging using standard require() syntax!", undefined, "CJS");
92
96
 
93
97
  class AuthService {
94
98
  login() {
95
- term.info(this, "Starting login process...");
99
+ term.info("Starting login process...", undefined, this);
96
100
  const user = { id: 42, name: "Alex", roles: ["admin", "billing"] };
97
- term.success(this, "Credentials validated successfully", user);
101
+ term.success("Credentials validated successfully", user, this);
98
102
  }
99
103
  }
100
104
 
@@ -129,23 +133,23 @@ When you run your scripts, your terminal will light up with premium, high-visibi
129
133
 
130
134
  ## đŸ› ī¸ API Reference
131
135
 
132
- ### `term.info(context, message, [data])`
136
+ ### `term.info(message, [data], [context])`
133
137
 
134
138
  Logs an informational message.
135
139
 
136
- - `context` `(any)`: `this` inside a class, a function name, or a plain string.
137
140
  - `message` `(string)`: The description text of the log.
138
141
  - `data` `(any, optional)`: Any object, array, or metadata to inspect and format below.
142
+ - `context` `(any, optional)`: `this` inside a class, a function name, or a plain string.
139
143
 
140
- ### `term.error(context, message, [data])`
144
+ ### `term.error(message, [data], [context])`
141
145
 
142
146
  Logs an error message. Same signature as `info()`.
143
147
 
144
- ### `term.success(context, message, [data])`
148
+ ### `term.success(message, [data], [context])`
145
149
 
146
150
  Logs a success message. Same signature as `info()`.
147
151
 
148
- ### `term.warn(context, message, [data])`
152
+ ### `term.warn(message, [data], [context])`
149
153
 
150
154
  Logs a warning message. Same signature as `info()`.
151
155
 
@@ -164,7 +168,7 @@ Autocompletion works out of the box in VS Code and modern IDEs. If you are using
164
168
  ```typescript
165
169
  import term from "use-term";
166
170
 
167
- term.info("TypeScript", "Fully typed logger out of the box!");
171
+ term.info("Fully typed logger out of the box!", undefined, "TypeScript");
168
172
  ```
169
173
 
170
174
  ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-term",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "A zero-dependency, ultra-lightweight, and beautiful terminal logging library for Node.js with smart context detection, colored outputs, and automatic timestamps.",
5
5
  "main": "src/term.js",
6
6
  "types": "src/term.d.ts",
package/src/term.d.ts CHANGED
@@ -2,43 +2,47 @@
2
2
  * A beautiful, simple, and dependency-free terminal logger for Node.js.
3
3
  */
4
4
  declare class Term {
5
- /**
6
- * Logs an informational message.
7
- * @param context The context of the log (e.g. `this`, a class instance, a function reference, or a string).
8
- * @param msg The message to log.
9
- * @param data Optional metadata or object to inspect.
10
- */
11
- info(context: any, msg: string, data?: any): void;
5
+ /**
6
+ * Logs an informational message.
7
+ * @param msg The message to log.
8
+ * @param data Optional metadata or object to inspect.
9
+ * @param context Optional scope or location for the log (e.g. `this`, a function, or a string).
10
+ */
11
+ info(msg: string, data?: any, context?: any): void;
12
+ info(context: any, msg: string, data?: any): void;
12
13
 
13
- /**
14
- * Logs an error message.
15
- * @param context The context of the log (e.g. `this`, a class instance, a function reference, or a string).
16
- * @param msg The message to log.
17
- * @param data Optional metadata or object to inspect.
18
- */
19
- error(context: any, msg: string, data?: any): void;
14
+ /**
15
+ * Logs an error message.
16
+ * @param msg The message to log.
17
+ * @param data Optional metadata or object to inspect.
18
+ * @param context Optional scope or location for the log (e.g. `this`, a function, or a string).
19
+ */
20
+ error(msg: string, data?: any, context?: any): void;
21
+ error(context: any, msg: string, data?: any): void;
20
22
 
21
- /**
22
- * Logs a success message.
23
- * @param context The context of the log (e.g. `this`, a class instance, a function reference, or a string).
24
- * @param msg The message to log.
25
- * @param data Optional metadata or object to inspect.
26
- */
27
- success(context: any, msg: string, data?: any): void;
23
+ /**
24
+ * Logs a success message.
25
+ * @param msg The message to log.
26
+ * @param data Optional metadata or object to inspect.
27
+ * @param context Optional scope or location for the log (e.g. `this`, a function, or a string).
28
+ */
29
+ success(msg: string, data?: any, context?: any): void;
30
+ success(context: any, msg: string, data?: any): void;
28
31
 
29
- /**
30
- * Logs a warning message.
31
- * @param context The context of the log (e.g. `this`, a class instance, a function reference, or a string).
32
- * @param msg The message to log.
33
- * @param data Optional metadata or object to inspect.
34
- */
35
- warn(context: any, msg: string, data?: any): void;
32
+ /**
33
+ * Logs a warning message.
34
+ * @param msg The message to log.
35
+ * @param data Optional metadata or object to inspect.
36
+ * @param context Optional scope or location for the log (e.g. `this`, a function, or a string).
37
+ */
38
+ warn(msg: string, data?: any, context?: any): void;
39
+ warn(context: any, msg: string, data?: any): void;
36
40
 
37
- /**
38
- * Prints a bold, beautifully stylized title separator in the console.
39
- * @param msg The section title to display.
40
- */
41
- title(msg: string): void;
41
+ /**
42
+ * Prints a bold, beautifully stylized title separator in the console.
43
+ * @param msg The section title to display.
44
+ */
45
+ title(msg: string): void;
42
46
  }
43
47
 
44
48
  declare const term: Term;
package/src/term.js CHANGED
@@ -1,66 +1,109 @@
1
- const util = require('util');
1
+ const util = require("util");
2
2
 
3
3
  const COLORS = {
4
- info: '\x1b[36m',
5
- error: '\x1b[31m',
6
- success: '\x1b[32m',
7
- warn: '\x1b[33m',
8
- blue: '\x1b[34m',
9
- gray: '\x1b[90m',
10
- magenta: '\x1b[35m',
11
- reset: '\x1b[0m',
12
- bold: '\x1b[1m'
4
+ info: "\x1b[36m",
5
+ error: "\x1b[31m",
6
+ success: "\x1b[32m",
7
+ warn: "\x1b[33m",
8
+ blue: "\x1b[34m",
9
+ gray: "\x1b[90m",
10
+ magenta: "\x1b[35m",
11
+ reset: "\x1b[0m",
12
+ bold: "\x1b[1m",
13
13
  };
14
14
 
15
15
  class Term {
16
- _timestamp() {
17
- const now = new Date();
18
- const time = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`;
19
- return `${COLORS.gray}${time}${COLORS.reset}`;
20
- }
16
+ _timestamp() {
17
+ const now = new Date();
18
+ const time = `${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, "0")}:${now.getSeconds().toString().padStart(2, "0")}`;
19
+ return `${COLORS.gray}${time}${COLORS.reset}`;
20
+ }
21
21
 
22
- _getContextName(ctx) {
23
- if (!ctx) return 'App';
24
- if (typeof ctx === 'string') return ctx;
25
- if (typeof ctx === 'function') return ctx.name || 'AnonymousFn';
26
- if (typeof ctx === 'object' && ctx.constructor) return ctx.constructor.name;
27
- return 'Unknown';
28
- }
22
+ _getContextName(ctx) {
23
+ if (!ctx) return "App";
24
+ if (typeof ctx === "string") return ctx;
25
+ if (typeof ctx === "function") return ctx.name || "AnonymousFn";
26
+ if (typeof ctx === "object" && ctx.constructor) return ctx.constructor.name;
27
+ return "Unknown";
28
+ }
29
29
 
30
- _log(type, color, icon, context, msg, data) {
31
- const ctxName = this._getContextName(context);
32
- const timestamp = this._timestamp();
33
- const tag = `${color}${icon} ${type}${COLORS.reset}`;
34
- const scope = `${COLORS.blue}[${ctxName}]${COLORS.reset}`;
35
-
36
- console.log(`${timestamp} ${tag} ${scope} ${msg}`);
37
-
38
- if (data !== undefined && data !== null) {
39
- const formatted = util.inspect(data, { colors: true, depth: null });
40
- const indented = formatted.split('\n').map(l => l).join('\n');
41
- console.log(indented);
42
- }
30
+ _resolveLogArgs(first, second, third) {
31
+ if (typeof first === "string" && typeof second !== "string") {
32
+ const msg = first;
33
+ if (third !== undefined) {
34
+ return { context: third, msg, data: second };
35
+ }
36
+ if (second === undefined) {
37
+ return { context: undefined, msg, data: undefined };
38
+ }
39
+ if (typeof second === "function" || typeof second === "string") {
40
+ return { context: second, msg, data: undefined };
41
+ }
42
+ return { context: undefined, msg, data: second };
43
43
  }
44
44
 
45
- info(context, msg, data) {
46
- this._log('INFO', COLORS.info, 'â„šī¸ ', context, msg, data);
47
- }
45
+ return { context: first, msg: second, data: third };
46
+ }
48
47
 
49
- error(context, msg, data) {
50
- this._log('ERROR', COLORS.error, '❌', context, msg, data);
51
- }
52
-
53
- success(context, msg, data) {
54
- this._log('SUCCESS', COLORS.success, '✅', context, msg, data);
55
- }
48
+ _log(type, color, icon, context, msg, data) {
49
+ const ctxName = this._getContextName(context);
50
+ const timestamp = this._timestamp();
51
+ const tag = `${color}${icon} ${type}${COLORS.reset}`;
52
+ const scope = `${COLORS.blue}[${ctxName}]${COLORS.reset}`;
56
53
 
57
- warn(context, msg, data) {
58
- this._log('WARN', COLORS.warn, 'âš ī¸ ', context, msg, data);
59
- }
54
+ console.log(`${timestamp} ${tag} ${scope} ${msg}`);
60
55
 
61
- title(msg) {
62
- console.log(`\n${COLORS.magenta}${COLORS.bold}━━━ ${msg.toUpperCase()} ━━━${COLORS.reset}`);
56
+ if (data !== undefined && data !== null) {
57
+ const formatted = util.inspect(data, { colors: true, depth: null });
58
+ const indented = formatted
59
+ .split("\n")
60
+ .map((l) => l)
61
+ .join("\n");
62
+ console.log(indented);
63
63
  }
64
+ }
65
+
66
+ info(msgOrContext, dataOrMsg, context) {
67
+ const {
68
+ context: ctx,
69
+ msg,
70
+ data,
71
+ } = this._resolveLogArgs(msgOrContext, dataOrMsg, context);
72
+ this._log("INFO", COLORS.info, "â„šī¸ ", ctx, msg, data);
73
+ }
74
+
75
+ error(msgOrContext, dataOrMsg, context) {
76
+ const {
77
+ context: ctx,
78
+ msg,
79
+ data,
80
+ } = this._resolveLogArgs(msgOrContext, dataOrMsg, context);
81
+ this._log("ERROR", COLORS.error, "❌", ctx, msg, data);
82
+ }
83
+
84
+ success(msgOrContext, dataOrMsg, context) {
85
+ const {
86
+ context: ctx,
87
+ msg,
88
+ data,
89
+ } = this._resolveLogArgs(msgOrContext, dataOrMsg, context);
90
+ this._log("SUCCESS", COLORS.success, "✅", ctx, msg, data);
91
+ }
92
+
93
+ warn(msgOrContext, dataOrMsg, context) {
94
+ const {
95
+ context: ctx,
96
+ msg,
97
+ data,
98
+ } = this._resolveLogArgs(msgOrContext, dataOrMsg, context);
99
+ this._log("WARN", COLORS.warn, "âš ī¸ ", ctx, msg, data);
100
+ }
101
+
102
+ title(msg) {
103
+ console.log(
104
+ `\n${COLORS.magenta}${COLORS.bold}━━━ ${msg.toUpperCase()} ━━━${COLORS.reset}`,
105
+ );
106
+ }
64
107
  }
65
108
 
66
109
  module.exports = new Term();