This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+80
View File
@@ -0,0 +1,80 @@
/// <reference types="node"/>
declare namespace logUpdate {
interface LogUpdate {
/**
Log to `stdout` by overwriting the previous output in the terminal.
@param text - The text to log to `stdout`.
@example
```
import logUpdate = require('log-update');
const frames = ['-', '\\', '|', '/'];
let i = 0;
setInterval(() => {
const frame = frames[i = ++i % frames.length];
logUpdate(
`
♥♥
${frame} unicorns ${frame}
♥♥
`
);
}, 80);
```
*/
(...text: string[]): void;
/**
Clear the logged output.
*/
clear(): void;
/**
Persist the logged output. Useful if you want to start a new log session below the current one.
*/
done(): void;
}
interface Options {
/**
Show the cursor. This can be useful when a CLI accepts input from a user.
@example
```
import logUpdate = require('log-update');
// Write output but don't hide the cursor
const log = logUpdate.create(process.stdout, {
showCursor: true
});
```
*/
readonly showCursor?: boolean;
}
}
declare const logUpdate: logUpdate.LogUpdate & {
/**
Log to `stderr` by overwriting the previous output in the terminal.
@param text - The text to log to `stderr`.
*/
readonly stderr: logUpdate.LogUpdate;
/**
Get a `logUpdate` method that logs to the specified stream.
@param stream - The stream to log to.
*/
readonly create: (
stream: NodeJS.WritableStream,
options?: logUpdate.Options
) => logUpdate.LogUpdate;
};
export = logUpdate;
+84
View File
@@ -0,0 +1,84 @@
'use strict';
const ansiEscapes = require('ansi-escapes');
const cliCursor = require('cli-cursor');
const wrapAnsi = require('wrap-ansi');
const sliceAnsi = require('slice-ansi');
const defaultTerminalHeight = 24;
const getWidth = stream => {
const {columns} = stream;
if (!columns) {
return 80;
}
return columns;
};
const fitToTerminalHeight = (stream, text) => {
const terminalHeight = stream.rows || defaultTerminalHeight;
const lines = text.split('\n');
const toRemove = lines.length - terminalHeight;
if (toRemove <= 0) {
return text;
}
return sliceAnsi(
text,
lines.slice(0, toRemove).join('\n').length + 1,
text.length);
};
const main = (stream, {showCursor = false} = {}) => {
let previousLineCount = 0;
let previousWidth = getWidth(stream);
let previousOutput = '';
const render = (...args) => {
if (!showCursor) {
cliCursor.hide();
}
let output = args.join(' ') + '\n';
output = fitToTerminalHeight(stream, output);
const width = getWidth(stream);
if (output === previousOutput && previousWidth === width) {
return;
}
previousOutput = output;
previousWidth = width;
output = wrapAnsi(output, width, {
trim: false,
hard: true,
wordWrap: false
});
stream.write(ansiEscapes.eraseLines(previousLineCount) + output);
previousLineCount = output.split('\n').length;
};
render.clear = () => {
stream.write(ansiEscapes.eraseLines(previousLineCount));
previousOutput = '';
previousWidth = getWidth(stream);
previousLineCount = 0;
};
render.done = () => {
previousOutput = '';
previousWidth = getWidth(stream);
previousLineCount = 0;
if (!showCursor) {
cliCursor.show();
}
};
return render;
};
module.exports = main(process.stdout);
module.exports.stderr = main(process.stderr);
module.exports.create = main;
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+54
View File
@@ -0,0 +1,54 @@
{
"name": "log-update",
"version": "4.0.0",
"description": "Log by overwriting the previous output in the terminal. Useful for rendering progress bars, animations, etc.",
"license": "MIT",
"repository": "sindresorhus/log-update",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"log",
"logger",
"logging",
"cli",
"terminal",
"term",
"console",
"shell",
"update",
"refresh",
"overwrite",
"output",
"stdout",
"progress",
"bar",
"animation"
],
"dependencies": {
"ansi-escapes": "^4.3.0",
"cli-cursor": "^3.1.0",
"slice-ansi": "^4.0.0",
"wrap-ansi": "^6.2.0"
},
"devDependencies": {
"@types/node": "^13.7.4",
"ava": "^3.3.0",
"terminal.js": "^1.0.10",
"tsd": "^0.11.0",
"xo": "^0.26.1"
}
}
+97
View File
@@ -0,0 +1,97 @@
# log-update [![Build Status](https://travis-ci.org/sindresorhus/log-update.svg?branch=master)](https://travis-ci.org/sindresorhus/log-update)
> Log by overwriting the previous output in the terminal.<br>
> Useful for rendering progress bars, animations, etc.
![](screenshot.gif)
## Install
```
$ npm install log-update
```
## Usage
```js
const logUpdate = require('log-update');
const frames = ['-', '\\', '|', '/'];
let i = 0;
setInterval(() => {
const frame = frames[i = ++i % frames.length];
logUpdate(
`
♥♥
${frame} unicorns ${frame}
♥♥
`
);
}, 80);
```
## API
### logUpdate(text…)
Log to stdout.
### logUpdate.clear()
Clear the logged output.
### logUpdate.done()
Persist the logged output.<br>
Useful if you want to start a new log session below the current one.
### logUpdate.stderr(text…)
Log to stderr.
### logUpdate.stderr.clear()
### logUpdate.stderr.done()
### logUpdate.create(stream, options?)
Get a `logUpdate` method that logs to the specified stream.
#### options
Type: `object`
##### showCursor
Type: `boolean`\
Default: `false`
Show the cursor. This can be useful when a CLI accepts input from a user.
```js
const logUpdate = require('log-update');
// Write output but don't hide the cursor
const log = logUpdate.create(process.stdout, {
showCursor: true
});
```
## Examples
- [listr](https://github.com/SamVerschueren/listr) - Uses this module to render an interactive task list
- [ora](https://github.com/sindresorhus/ora) - Uses this module to render awesome spinners
- [speed-test](https://github.com/sindresorhus/speed-test) - Uses this module to render a [spinner](https://github.com/sindresorhus/elegant-spinner)
---
<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-log-update?utm_source=npm-log-update&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>