Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 2, 2020
1 parent a488df3 commit fefc18b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
34 changes: 17 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,43 @@ const main = (stream, options) => {
showCursor: false
}, options);

let prevLineCount = 0;
let prevWidth = getWidth(stream);
let prevOut = '';
let previousLineCount = 0;
let previousWidth = getWidth(stream);
let previousOutput = '';

const render = (...args) => {
if (!options.showCursor) {
cliCursor.hide();
}

let out = args.join(' ') + '\n';
let output = args.join(' ') + '\n';
const width = getWidth(stream);
if (out === prevOut && prevWidth === width) {
if (output === previousOutput && previousWidth === width) {
return;
}

prevOut = out;
prevWidth = width;
out = wrapAnsi(out, width, {
previousOutput = output;
previousWidth = width;
output = wrapAnsi(output, width, {
trim: false,
hard: true,
wordWrap: false
});
stream.write(ansiEscapes.eraseLines(prevLineCount) + out);
prevLineCount = out.split('\n').length;
stream.write(ansiEscapes.eraseLines(previousLineCount) + output);
previousLineCount = output.split('\n').length;
};

render.clear = () => {
stream.write(ansiEscapes.eraseLines(prevLineCount));
prevOut = '';
prevWidth = getWidth(stream);
prevLineCount = 0;
stream.write(ansiEscapes.eraseLines(previousLineCount));
previousOutput = '';
previousWidth = getWidth(stream);
previousLineCount = 0;
};

render.done = () => {
prevOut = '';
prevWidth = getWidth(stream);
prevLineCount = 0;
previousOutput = '';
previousWidth = getWidth(stream);
previousLineCount = 0;

if (!options.showCursor) {
cliCursor.show();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"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",
Expand Down
9 changes: 2 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
![](screenshot.gif)


## Install

```
$ npm install log-update
```


## Usage

```js
Expand All @@ -34,7 +32,6 @@ setInterval(() => {
}, 80);
```


## API

### logUpdate(text…)
Expand Down Expand Up @@ -63,11 +60,11 @@ Get a `logUpdate` method that logs to the specified stream.

#### options

Type: `Object`
Type: `object`

##### showCursor

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Show the cursor. This can be useful when a CLI accepts input from a user.
Expand All @@ -81,14 +78,12 @@ const log = logUpdate.create(process.stdout, {
});
```


## 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">
Expand Down

0 comments on commit fefc18b

Please sign in to comment.