Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: RTL support #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
*.sock
npm-debug.log
yarn.lock
.idea
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ These are keys in the options object you can pass to the progress bar along with
- `renderThrottle` minimum time between updates in milliseconds defaulting to 16
- `clear` option to clear the bar on completion defaulting to false
- `callback` optional function to call when the progress bar completes
- `isRtlOn` a boolean value that indicates if the bar's direction should progress from right to the left direction (default: `false`).

### Tokens

Expand Down
7 changes: 5 additions & 2 deletions lib/node-progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function ProgressBar(fmt, options) {
this.curr = options.curr || 0;
this.total = options.total;
this.width = options.width || this.total;
this.clear = options.clear
this.clear = options.clear;
this.chars = {
complete : options.complete || '=',
incomplete : options.incomplete || '-',
Expand All @@ -70,6 +70,7 @@ function ProgressBar(fmt, options) {
this.callback = options.callback || function () {};
this.tokens = {};
this.lastDraw = '';
this.isRtlOn = options.isRtlOn || false;
}

/**
Expand Down Expand Up @@ -165,7 +166,9 @@ ProgressBar.prototype.render = function (tokens, force) {
complete = complete.slice(0, -1) + this.chars.head;

/* fill in the actual progress bar */
str = str.replace(':bar', complete + incomplete);
var barDataLtr = complete + incomplete;
var barData = this.isRtlOn ? barDataLtr : [...barDataLtr].reverse().join('');
str = str.replace(':bar', barData);

/* replace the extra tokens */
if (this.tokens) for (var key in this.tokens) str = str.replace(':' + key, this.tokens[key]);
Expand Down