cli-block
Version:
Create nice looking CLI Blocks
224 lines • 10.5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.file = exports.blockLine = exports.blockInfo = exports.blockWarn = exports.blockError = exports.blockSuccess = exports.line = exports.info = exports.warn = exports.error = exports.success = exports.createLine = void 0;
const settings_1 = require("../settings");
const util_1 = require("../util");
const icons_1 = require("../icons");
/**
* Creates a formatted line or multiple lines of text with optional prefix and indentation.
* @param {string | string[] | null} msg - The message to display. Can be a single string, array of strings, or null for an empty line.
* @param {Object} options - Configuration options for the line.
* @param {boolean} [options.useBlock=false] - Whether to use block formatting.
* @param {number} [options.indent=0] - Number of spaces to indent the line.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @returns {string[]} Array of formatted lines.
*/
const createLine = (msg = null, options = {}) => {
var _a, _b;
const cfg = (0, settings_1.useSettings)(options);
const useBlock = (_a = options.useBlock) !== null && _a !== void 0 ? _a : false;
const indent = (_b = options.indent) !== null && _b !== void 0 ? _b : 0;
let lines = [];
if (msg !== null) {
if (typeof msg === "string") {
msg = (0, util_1.breakText)(msg, (0, settings_1.getContentWidth)(cfg));
}
if (Array.isArray(msg)) {
msg.forEach((txt, i) => {
if (i === 0) {
txt = `${(0, util_1.spaces)(indent)}${cfg.prefix ? cfg.prefix + " " : ""}${txt}`;
}
else {
txt = `${(0, util_1.spaces)(indent)}${(0, util_1.spaces)(3)}${txt}`;
}
lines.push(txt);
});
}
}
else {
lines = [(0, util_1.spaces)(indent) + (useBlock ? " " : "")];
}
return lines;
};
exports.createLine = createLine;
/**
* Displays a success message with a green checkmark (✔) prefix.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {boolean} [options.useBlock=false] - Whether to use block formatting.
* @param {number} [options.indent=0] - Number of spaces to indent the line.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* success("Operation completed successfully");
* // Output: ✔ Operation completed successfully
*/
const success = (msg, options = {}) => {
const lines = (0, exports.createLine)(msg, Object.assign(Object.assign({}, options), { prefix: (0, util_1.green)("✔") }));
lines.forEach(txt => (0, util_1.logger)(txt, options));
};
exports.success = success;
/**
* Displays an error message with a red cross (×) prefix.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {boolean} [options.useBlock=false] - Whether to use block formatting.
* @param {number} [options.indent=0] - Number of spaces to indent the line.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* error("Operation failed");
* // Output: × Operation failed
*/
const error = (msg, options = {}) => {
const lines = (0, exports.createLine)(msg, Object.assign(Object.assign({}, options), { prefix: (0, util_1.red)("×") }));
lines.forEach(txt => (0, util_1.logger)(txt, options));
};
exports.error = error;
/**
* Displays a warning message with a yellow exclamation mark (!) prefix.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {boolean} [options.useBlock=false] - Whether to use block formatting.
* @param {number} [options.indent=0] - Number of spaces to indent the line.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* warn("Deprecated feature used");
* // Output: ! Deprecated feature used
*/
const warn = (msg, options = {}) => {
const lines = (0, exports.createLine)(msg, Object.assign(Object.assign({}, options), { prefix: (0, util_1.yellow)("!") }));
lines.forEach(txt => (0, util_1.logger)(txt, options));
};
exports.warn = warn;
/**
* Displays an info message with a blue info (i) prefix.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {boolean} [options.useBlock=false] - Whether to use block formatting.
* @param {number} [options.indent=0] - Number of spaces to indent the line.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* info("Processing files...");
* // Output: i Processing files...
*/
const info = (msg, options = {}) => {
const lines = (0, exports.createLine)(msg, Object.assign(Object.assign({}, options), { prefix: (0, util_1.blue)('i') }));
lines.forEach(txt => (0, util_1.logger)(txt, options));
};
exports.info = info;
/**
* Displays a plain text message without any prefix.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {boolean} [options.useBlock=false] - Whether to use block formatting.
* @param {number} [options.indent=0] - Number of spaces to indent the line.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* line("Simple text message");
* // Output: Simple text message
*/
const line = (msg, options = {}) => {
const lines = (0, exports.createLine)(msg, options);
lines.forEach(txt => (0, util_1.logger)(txt, options));
};
exports.line = line;
/**
* Displays a success message within a bordered block.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {number} [options.indent=0] - Number of spaces to indent the block.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* blockSuccess("Operation completed successfully");
* // Output: │ ✔ Operation completed successfully │
*/
const blockSuccess = (msg, options = {}) => {
(0, exports.success)(msg, Object.assign(Object.assign({}, options), { useBlock: true }));
};
exports.blockSuccess = blockSuccess;
/**
* Displays an error message within a bordered block.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {number} [options.indent=0] - Number of spaces to indent the block.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* blockError("Operation failed");
* // Output: │ × Operation failed │
*/
const blockError = (msg, options = {}) => {
(0, exports.error)(msg, Object.assign(Object.assign({}, options), { useBlock: true }));
};
exports.blockError = blockError;
/**
* Displays a warning message within a bordered block.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {number} [options.indent=0] - Number of spaces to indent the block.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* blockWarn("Deprecated feature used");
* // Output: │ ! Deprecated feature used │
*/
const blockWarn = (msg, options = {}) => {
(0, exports.warn)(msg, Object.assign(Object.assign({}, options), { useBlock: true }));
};
exports.blockWarn = blockWarn;
/**
* Displays an info message within a bordered block.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {number} [options.indent=0] - Number of spaces to indent the block.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* blockInfo("Processing files...");
* // Output: │ i Processing files... │
*/
const blockInfo = (msg, options = {}) => {
(0, exports.info)(msg, Object.assign(Object.assign({}, options), { useBlock: true }));
};
exports.blockInfo = blockInfo;
/**
* Displays a plain text message within a bordered block.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {number} [options.indent=0] - Number of spaces to indent the block.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* blockLine("Simple text message");
* // Output: │ Simple text message │
*/
const blockLine = (msg, options = {}) => {
(0, exports.line)(msg, Object.assign(Object.assign({}, options), { useBlock: true }));
};
exports.blockLine = blockLine;
/**
* Display a file typed message with a file icon prefix.
* @param {string | string[]} msg - The message to display. Can be a single string or array of strings.
* @param {Object} options - Configuration options for the line.
* @param {boolean} [options.useBlock=false] - Whether to use block formatting.
* @param {Partial<LoggerSettings>} [options] - Additional logger settings.
* @example
* file("index.ts");
* // Output: 📄 index.ts
*/
const file = (msg, options = {}) => {
const { pathDepth } = (0, settings_1.useSettings)(options);
if (typeof msg === 'string') {
msg = [msg];
}
msg.map((file) => {
if (pathDepth > 0) {
const path = file.split('/');
if (pathDepth === 1) {
file = path[path.length - 1];
}
else if (path.length > pathDepth) {
file = path.slice(-pathDepth).join('/');
}
}
const lines = (0, exports.createLine)(file, Object.assign(Object.assign({}, options), { prefix: (0, icons_1.getFileIcon)(file) }));
lines.forEach(txt => (0, util_1.logger)(txt, options));
});
};
exports.file = file;
//# sourceMappingURL=lines.js.map
;