awesome-logging
Advanced tools
Comparing version 0.2.7 to 0.2.8
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AwesomeProgressLogger = void 0; | ||
const strip_ansi_1 = __importDefault(require("strip-ansi")); | ||
const logger_color_1 = require("../../utils/logger-color"); | ||
const terminal_size_1 = require("../../utils/terminal-size"); | ||
const logger_base_1 = require("../logger-base"); | ||
@@ -23,2 +28,5 @@ class AwesomeProgressLogger extends logger_base_1.AwesomeLoggerBase { | ||
}; | ||
if (this._options.text) { | ||
this._options.text += ' '; | ||
} | ||
} | ||
@@ -36,6 +44,7 @@ end() { } | ||
getNextLine() { | ||
const totalLength = Math.min(process.stdout.columns - 2, this._options.maxWidth); | ||
const totalLength = Math.min(terminal_size_1.TerminalSize.terminalWidth - 2, this._options.maxWidth) - strip_ansi_1.default(this._options.text).length; | ||
const finnishedLength = Math.round((this._currentProgress / this._options.totalProgress) * totalLength); | ||
const unFinnishedLength = totalLength - finnishedLength; | ||
const res = logger_color_1.colorize(this._options.borderColor)(this._options.borderChar) + | ||
const res = this._options.text + | ||
logger_color_1.colorize(this._options.borderColor)(this._options.borderChar) + | ||
logger_color_1.colorize(this._options.filledColor)(this._options.filledChar.repeat(finnishedLength)) + | ||
@@ -42,0 +51,0 @@ logger_color_1.colorize(this._options.unfilledColor)(this._options.unfilledChar.repeat(unFinnishedLength)) + |
@@ -8,2 +8,3 @@ "use strict"; | ||
const strip_ansi_1 = __importDefault(require("strip-ansi")); | ||
const awesome_logger_1 = require("../awesome-logger"); | ||
const ansi_utils_1 = require("../utils/ansi-utils"); | ||
@@ -17,31 +18,33 @@ const console_log_1 = require("../utils/console-log"); | ||
let lastLines = this.lastString && !ignoreLastLine ? this.lastString.split(/[\r\n]+/g) : []; | ||
if (this.lastTerminalHeight !== terminal_size_1.TerminalSize.terminalHeight) { | ||
this.lastTerminalHeight = terminal_size_1.TerminalSize.terminalHeight; | ||
// When the terminal is resized, we try do delete everything and re-print everything. | ||
// Deleting lines that are out of scroll view is not possible in some terminals (powershell, cmd, and more on windows for example) | ||
// We still try to do so, but when reducing the height in the terminal some lines will get "stuck" in the history | ||
for (let i = 0; i < lastLines.length; i++) { | ||
ansi_utils_1.DELETE_LINE(); | ||
ansi_utils_1.MOVE_UP(); | ||
} | ||
// resetting lastString & lastLine to make it reprint everything | ||
this.lastString = ''; | ||
lastLines = []; | ||
} | ||
this.lastString = interruptLog ? '' : val; | ||
const newLines = val.split(/[\r\n]+/g); | ||
const lengthDifference = newLines.length - lastLines.length; | ||
const lessLines = lengthDifference < 0 ? lengthDifference * -1 : 0; | ||
if (lengthDifference < 0) { | ||
for (let i = 0; i < lessLines; i++) { | ||
ansi_utils_1.DELETE_LINE(); | ||
ansi_utils_1.MOVE_UP(); | ||
if (!awesome_logger_1.AwesomeLogger.restrictedLogging) { | ||
if (this.lastTerminalHeight !== terminal_size_1.TerminalSize.terminalHeight) { | ||
this.lastTerminalHeight = terminal_size_1.TerminalSize.terminalHeight; | ||
// When the terminal is resized, we try do delete everything and re-print everything. | ||
// Deleting lines that are out of scroll view is not possible in some terminals (powershell, cmd, and more on windows for example) | ||
// We still try to do so, but when reducing the height in the terminal some lines will get "stuck" in the history | ||
for (let i = 0; i < lastLines.length; i++) { | ||
ansi_utils_1.DELETE_LINE(); | ||
ansi_utils_1.MOVE_UP(); | ||
} | ||
// resetting lastString & lastLine to make it reprint everything | ||
this.lastString = ''; | ||
lastLines = []; | ||
} | ||
const lengthDifference = newLines.length - lastLines.length; | ||
const lessLines = lengthDifference < 0 ? lengthDifference * -1 : 0; | ||
if (lengthDifference < 0) { | ||
for (let i = 0; i < lessLines; i++) { | ||
ansi_utils_1.DELETE_LINE(); | ||
ansi_utils_1.MOVE_UP(); | ||
} | ||
} | ||
let moveUpAmount = lastLines.length - lessLines - 1; | ||
if (moveUpAmount < 0) { | ||
moveUpAmount = 0; | ||
} | ||
ansi_utils_1.MOVE_UP(moveUpAmount); | ||
ansi_utils_1.MOVE_LEFT(); | ||
} | ||
let moveUpAmount = lastLines.length - lessLines - 1; | ||
if (moveUpAmount < 0) { | ||
moveUpAmount = 0; | ||
} | ||
ansi_utils_1.MOVE_UP(moveUpAmount); | ||
ansi_utils_1.MOVE_LEFT(); | ||
for (let i = 0; i < newLines.length; i++) { | ||
@@ -48,0 +51,0 @@ const oldLine = (_a = lastLines[i]) !== null && _a !== void 0 ? _a : ''; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const awesome_logger_1 = require("./awesome-logger"); | ||
awesome_logger_1.AwesomeLogger.log('this is awesome logging!'); | ||
awesome_logger_1.AwesomeLogger.log(''); | ||
awesome_logger_1.AwesomeLogger.log(''); | ||
awesome_logger_1.AwesomeLogger.log(''); | ||
awesome_logger_1.AwesomeLogger.log(''); | ||
awesome_logger_1.AwesomeLogger.log(''); | ||
awesome_logger_1.AwesomeLogger.log(''); | ||
// setTimeout(() => { | ||
// const textA = 'One line of text...'; | ||
// const textB = 'Multiple\nLines\nof Text!'; | ||
// let state = true; | ||
// const logControl = AwesomeLogger.log(textA); | ||
// setInterval(() => { | ||
// state = !state; | ||
// logControl.setText(state ? textA : textB); | ||
// }, 1000); | ||
// }, 3000); | ||
// const logProgressControl = AwesomeLogger.log('progress', { | ||
// totalProgress: 100, | ||
// text: 'Very important progress:' | ||
// }); | ||
// let i = 0; | ||
// const interval = setInterval(() => { | ||
// logProgressControl.setProgress(i++); | ||
// if (i === 100) { | ||
// clearInterval(interval); | ||
// } | ||
// }, 500); | ||
// setInterval(() => { | ||
@@ -49,9 +75,20 @@ // AwesomeLogger.interrupt('interrupt' + Math.random()); | ||
// }; | ||
// const line1 = AwesomeLogger.create('text', { text: chalk.green('awd\nawd2awd3') }); | ||
// const prog1 = AwesomeLogger.create('progress', { totalProgress: 100, filledColor: 'GREEN', maxWidth: 100 }); | ||
// const line0 = AwesomeLogger.create('text', { text: chalk.yellow('Multiple lines, all changing:') }); | ||
// const line1 = AwesomeLogger.create('text', { text: chalk.blue('Important Information') }); | ||
// const prog1 = AwesomeLogger.create('progress', { totalProgress: 100, filledColor: 'GREEN', maxWidth: 50 }); | ||
// const spin1 = AwesomeLogger.create('spinner', { | ||
// text: ' My text', | ||
// text: 'Loading Animation', | ||
// spinnerDelay: 75, | ||
// spinnerFrames: [chalk.magenta('▄'), chalk.magenta('■'), chalk.magenta('▀'), chalk.magenta('▀'), chalk.magenta('■')] | ||
// }); | ||
// let textState = true; | ||
// setInterval(() => { | ||
// textState = !textState; | ||
// line1.setText(textState ? chalk.blue('Important Information') : chalk.green('Green text, because why not?')); | ||
// }, 1000); | ||
// let i = 0; | ||
// setInterval(() => { | ||
// i += 1; | ||
// prog1.setProgress(i); | ||
// }, 300); | ||
// const line2 = AwesomeLogger.create('text', { text: chalk.green('awd\nawd2awd3') }); | ||
@@ -64,3 +101,3 @@ // const prog2 = AwesomeLogger.create('progress', { totalProgress: 100, filledColor: 'GREEN', maxWidth: 100 }); | ||
// }); | ||
// const multi1 = AwesomeLogger.create('multi', { children: [spin1, line1, line2, prog1] }); | ||
// const multi1 = AwesomeLogger.log('multi', { children: [line0, spin1, line1, prog1] }); | ||
// const multi2 = AwesomeLogger.create('multi', { children: [line2, prog2, spin2] }); | ||
@@ -67,0 +104,0 @@ // const multi = AwesomeLogger.log('multi', { children: [multi1, multi2] }); |
@@ -1,1 +0,5 @@ | ||
export {}; | ||
export declare class TerminalSize { | ||
private static get terminalSize(); | ||
static get terminalHeight(): number; | ||
static get terminalWidth(): number; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TerminalSize = void 0; | ||
// @internal | ||
class TerminalSize { | ||
@@ -6,0 +5,0 @@ static get terminalSize() { |
{ | ||
"name": "awesome-logging", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"description": "Advanced logging messages, interactive prompts, loading animations and more in TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
204
README.md
# Awesome Logging | ||
<b>awesome-logging</b> is a collection of fancy text in- and output tools for CLI tools written in NodeJS. | ||
**awesome-logging** is a collection of fancy text outputs and inputs for CLI tools written in NodeJS. | ||
No matter what you want to log to the terminal or what information you need from the user, **awesome-logging** will help you do so. | ||
- Written in TypeScript | ||
- Colored logging messages | ||
- Live-update logging messages in a reliable way | ||
- Multi-line logging | ||
- Loading animations and progress bars | ||
- Interactive prompts to get user input | ||
- Interrupt playing animations with regular log entries | ||
- You want to show the progress of a certain task while also logging messages to the terminal while keeping everythign nice and formatted? No problem. | ||
- You want the user to select one or multiple options of a given list? No problem! | ||
- You want to log messages in the background while showing a fancy animation or a prompt to the user? No problem! | ||
# Work In Progress. | ||
## Key functionality | ||
# Installation | ||
### yarn | ||
```console | ||
- Written in TypeScript (Strongly typed) | ||
- Live-update logging messages in a reliable way | ||
- Multi-line logging | ||
- Loading animations and progress bars | ||
- Interactive prompts to get user input | ||
- Interrupt currently playing animations with regular log entries | ||
- Extendable design (add your own loggers / prompts) | ||
![multiline example](https://user-images.githubusercontent.com/37637338/124401662-e6793480-dd2a-11eb-9a8d-c09328b19259.gif) | ||
## Installation | ||
```console | ||
yarn add awesome-logging | ||
// or | ||
npm i awesome-logging | ||
``` | ||
``` | ||
### npm | ||
```console | ||
npm i awesome-logging | ||
``` | ||
## Examples | ||
```typescript | ||
import { AwesomeLogger } from 'awesome-logger'; | ||
``` | ||
```typescript | ||
// Example: Simple text logging | ||
AwesomeLogger.log('Welcome to awesome-logging!'); | ||
const logControl = AwesomeLogger.log('Is this a logging library?'); | ||
setTimeout(() => logControl.setText('This is an awesome-logging library!'), 1500); | ||
setTimeout(() => logControl.setText('Cool!'), 3000); | ||
``` | ||
![simple text logging](https://user-images.githubusercontent.com/37637338/124401295-e8da8f00-dd28-11eb-8b46-4efbfba30008.gif) | ||
```typescript | ||
// Example: Simple text logging (with line breaks) | ||
const textA = 'One line of text...'; | ||
const textB = 'Multiple\nLines\nof Text!'; | ||
let state = true; | ||
const logControl = AwesomeLogger.log(textA); | ||
setInterval(() => { | ||
state = !state; | ||
logControl.setText(state ? textA : textB); | ||
}, 1000); | ||
``` | ||
![text linebreak logging](https://user-images.githubusercontent.com/37637338/124401381-78803d80-dd29-11eb-8407-f3c462a455fc.gif) | ||
```typescript | ||
// Example: Progress Bar | ||
const logProgressControl = AwesomeLogger.log('progress', { | ||
totalProgress: 100, | ||
text: 'Very important progress:' | ||
}); | ||
let i = 0; | ||
const interval = setInterval(() => { | ||
logProgressControl.setProgress(i++); | ||
if (i === 100) { | ||
clearInterval(interval); | ||
} | ||
}, 500); | ||
``` | ||
![progress logging](https://user-images.githubusercontent.com/37637338/124401404-a9607280-dd29-11eb-9956-8378f54ffe0f.gif) | ||
## AwesomeLogger Methods | ||
```typescript | ||
AwesomeLogger.create(loggerType, loggerOptions): LoggerControl | ||
AwesomeLogger.log(text): LoggerControl | ||
AwesomeLogger.log(logger): LoggerControl | ||
AwesomeLogger.log(loggerType, loggerOptions): LoggerControl | ||
AwesomeLogger.prompt(promptType, promptOptions): PromptControl | ||
AwesomeLogger.interrupt(text): void | ||
``` | ||
## Logger types | ||
### Text | ||
The simplest of all loggers; logs any printable string, no matter if one, or multiple lines. | ||
**LoggerOptions** | ||
| Option | type | description | default | | ||
| ------ | ------ | ------------------------------- | ------- | | ||
| text | string | The text that should be logged. | '' | | ||
**LoggerControl** | ||
| Method | returnType | description | | ||
| --------------------- | ---------- | ------------------------------ | | ||
| setText(text: string) | void | Sets a new text to the logger. | | ||
--- | ||
### Spinner | ||
Logs a loading spinner with a custom loading animation and text. | ||
**LoggerOptions** | ||
| Option | type | description | default | | ||
| ------------- | -------- | ----------------------------------------------------- | --------------------------- | | ||
| text | string | The description text of the loading spinner. | '' | | ||
| spinnerFrames | string[] | The array of strings that are used for the animation. | ['. ', '.. ', '...', '.. '] | | ||
| spinnerDelay | number | The amount of ms between each spinnerframe. | 500 | | ||
**LoggerControl** | ||
| Method | returnType | description | | ||
| --------------------------------------------------------------------------- | ---------- | ------------------------------------------------------------------------------ | | ||
| stop(options: { succeeded?: boolean; removeLine?: boolean; text?: string }) | void | Stops the spinner and changes the logging accordingly to the provided options. | | ||
--- | ||
### Progress | ||
Loggs a progressbar with optional text. | ||
**LoggerOptions** | ||
| Option | type | description | default | | ||
| ------------- | ------------------ | --------------------------------------------------------------------- | -------------- | | ||
| totalProgress | number | The maximum number the progress can reach. | 100 | | ||
| text | string | The text that is displayed in front of the bar. | '' | | ||
| completedText | string | The text that is displayed when the progress reaches 100%. | '' | | ||
| unfilledChar | string | The character that is pronted for the uncompleted section of the bar. | '·' | | ||
| filledChar | string | The character that is pronted for the completed section of the bar. | '■' | | ||
| borderChar | string | The caharter that is printed on the left and right of the bar. | '■' | | ||
| maxWidth | number | The maximum cound characters the progressbar can have can. | Terminal Width | | ||
| borderColor | AwesomeLoggerColor | The color of the border chars. | 'GRAY' | | ||
| unfilledColor | AwesomeLoggerColor | The color of the uncompleted part of the bar. | 'GRAY' | | ||
| filledColor | AwesomeLoggerColor | The color of the completed part of the bar. | 'WHITE' | | ||
**Note:** For performance reasons do not specify the colors of unfilled or filled characters directly in the string option, but by the dedicated color option. | ||
**LoggerControl** | ||
| Method | returnType | description | | ||
| -------------------------------------------- | ---------- | ---------------------------------------------------- | | ||
| setProgress(progress: number, text?: string) | void | Sets the current progress and optionally a new text. | | ||
--- | ||
### Checklist | ||
Logs a list of items that have a certain state like 'pending', 'inProgress', 'succeeded' and more. | ||
**LoggerOptions** | ||
| Option | type | description | default | | ||
| ------ | -------------------------- | ----------------------------------------- | ------- | | ||
| items | AwesomeChecklistLoggerItem | All items that are part of the checklist. | [] | | ||
**LoggerControl** | ||
| Method | returnType | description | | ||
| -------------------------------------------------------------------------------- | ---------- | ------------------------------------------------------------------------------------------- | | ||
| changeState(index: number, state: AwesomeChecklistLoggerState, newText?: string) | void | Changes the state of one item based on the index. Optionally sets a new text for that item. | | ||
--- | ||
### Multi | ||
Combines multiple loggers that were created previously and loggs them below each other. | ||
**LoggerOptions** | ||
| Option | type | description | default | | ||
| -------- | ------------------- | -------------------------------------------------- | ------- | | ||
| children | AwesomeLoggerBase[] | The array of loggers that were created previously. | [] | | ||
**LoggerControl** | ||
| Method | returnType | description | | ||
| ------------------------------------------------------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------ | | ||
| getChild\<T extends AwesomeLoggerBase\>(index: number) | T | Returns the child at the given index. Be cautious as you have to be sure what kind of logger is present at the provided index. | | ||
--- |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
135709
1854
195