Skip to content

Commit

Permalink
refactor: fix eslint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
keindev committed Nov 25, 2021
1 parent 0dabd11 commit 164f375
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 234 deletions.
123 changes: 62 additions & 61 deletions src/ProgressBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ export enum TemplateToken {

/** ProgressBar display options */
export interface IProgressBarOptions {
/** Option to add badge */
badges?: boolean;
/** Option to clear the bar on completion */
clear?: boolean;
/** Completion character */
completeChar?: string;
/** Current completed index */
current?: number;
/** Option to add gradient to pending bar */
gradient?: boolean;
/** Incomplete character */
incompleteChar?: string;
/** Total number of ticks to complete */
total?: number;
/** The displayed width of the progress bar defaulting to total */
width?: number;
/** Completion character */
completeChar?: string;
/** Incomplete character */
incompleteChar?: string;
/** Option to clear the bar on completion */
clear?: boolean;
/** Option to add badge */
badges?: boolean;
/** Option to add gradient to pending bar */
gradient?: boolean;
}

/** User template tokens values */
Expand All @@ -62,41 +62,42 @@ export interface IProgressBarToken {
* ```
*/
export class ProgressBar implements Required<Omit<IProgressBarOptions, 'current'>> {
static readonly TICK = 1;
static readonly TIME_DIMENSION = 1000;
static readonly MAX_PERCENT = 100;
static readonly MAX_POINT_POSITION = 1;
static readonly MIN_POINT_POSITION = 0;
static readonly MAX_RATIO = 1;
static readonly MIN_PERCENT = 0;
static readonly MAX_PERCENT = 100;
static readonly MIN_POINT_POSITION = 0;
static readonly MIN_RATIO = 0;
static readonly MAX_RATIO = 1;
static readonly TICK = 1;
static readonly TIME_DIMENSION = 1000;

#current = Progress.Start;
#end: number | undefined;
#start = new Date().getTime();
#status = TaskStatus.Pending;
#tokens: Map<TemplateToken | string, string> = new Map();

/** Total number of ticks to complete */
readonly total = Progress.End;
/** Completion character */
readonly completeChar = figures.square;
/** Incomplete character */
readonly incompleteChar = figures.square;
/** The displayed width of the progress bar defaulting to total */
readonly width: number = 20;
/** Option to clear the bar on completion */
readonly clear: boolean = false;
/** Option to add badge */
readonly badges: boolean = true;
/** Option to clear the bar on completion */
readonly clear: boolean = false;
/** Completion character */
readonly completeChar = figures.square;
/** Option to add gradient to pending bar */
readonly gradient: boolean = true;
/** Incomplete character */
readonly incompleteChar = figures.square;
/**
* Output template
*
* @default `:bar :rate/bps :percent :eta/s`
*/
readonly template: string;
/** Total number of ticks to complete */
readonly total = Progress.End;

#current = Progress.Start;
#start = new Date().getTime();
#end: number | undefined;
#status = TaskStatus.Pending;
#tokens: Map<TemplateToken | string, string> = new Map();
/** The displayed width of the progress bar defaulting to total */
readonly width: number = 20;

constructor(template?: string, options?: IProgressBarOptions) {
this.template =
Expand Down Expand Up @@ -162,50 +163,22 @@ export class ProgressBar implements Required<Omit<IProgressBarOptions, 'current'
return this.#current >= this.total || !!this.#end;
}

/**
* Increases current progress on step value
* @param step - Value by which the current progress will increase
* @param tokens - Add custom tokens by adding a `{'name': value}` object parameter to your method
* @example
* ```javascript
* const bar = new Progress(':bar template with custom :token');
*
* bat.tick(10, { token: 100 });
* ```
*/
public tick(step?: number, tokens?: IProgressBarToken): ProgressBar {
this.#current = Math.min(this.total, this.#current + (step || ProgressBar.TICK));
this.#tokens = typeof tokens === 'object' ? new Map(Object.entries(tokens)) : new Map();

if (this.isCompleted) this.complete();

return this;
}

/** Completes progress and marks it as successful */
public complete(): void {
complete(): void {
this.#current = this.total;
this.#status = TaskStatus.Completed;
this.#end = new Date().getTime();
}

/** Stops the progress and marks it as skipped */
public skip(): void {
if (!this.isCompleted) {
this.#status = TaskStatus.Skipped;
this.#end = new Date().getTime();
}
}

/** Stops the progress and marks it as failed */
public fail(): void {
fail(): void {
if (!this.isCompleted) {
this.#status = TaskStatus.Failed;
this.#end = new Date().getTime();
}
}

public render(theme: Theme): string {
render(theme: Theme): string {
const length = Math.round(this.width * this.ratio);
const type = Theme.type(this.#status);

Expand Down Expand Up @@ -235,6 +208,34 @@ export class ProgressBar implements Required<Omit<IProgressBarOptions, 'current'
return this.badges ? Theme.join(TextSeparator.Space, result, theme.badge(type)) : result;
}

/** Stops the progress and marks it as skipped */
skip(): void {
if (!this.isCompleted) {
this.#status = TaskStatus.Skipped;
this.#end = new Date().getTime();
}
}

/**
* Increases current progress on step value
* @param step - Value by which the current progress will increase
* @param tokens - Add custom tokens by adding a `{'name': value}` object parameter to your method
* @example
* ```javascript
* const bar = new Progress(':bar template with custom :token');
*
* bat.tick(10, { token: 100 });
* ```
*/
tick(step?: number, tokens?: IProgressBarToken): ProgressBar {
this.#current = Math.min(this.total, this.#current + (step || ProgressBar.TICK));
this.#tokens = typeof tokens === 'object' ? new Map(Object.entries(tokens)) : new Map();

if (this.isCompleted) this.complete();

return this;
}

private getBlocks(theme: Theme, type: IndicationType, length: number): string {
const str = TextSeparator.Empty.padStart(length, this.completeChar);

Expand Down
72 changes: 36 additions & 36 deletions src/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ export interface ITaskOptions {

/** Entity for managing a task - includes all child objects (informational messages, errors, progress bars, and tasks) */
export class Task {
#id: number;
#text: string;
#status: TaskStatus;
#autoClear: boolean;
#bars: ProgressBar[] = [];
#subtasks: Task[] = [];
#logs = new Set<string>();
#errors: string[] = [];
#id: number;
#logs = new Set<string>();
#status: TaskStatus;
#subtasks: Task[] = [];
#text: string;
#warnings = new Set<string>();

public constructor(text: string, { status, autoClear }: ITaskOptions = {}) {
constructor(text: string, { status, autoClear }: ITaskOptions = {}) {
this.#id = ++uid;
this.#text = text;
this.#autoClear = !!autoClear;
Expand Down Expand Up @@ -84,7 +84,7 @@ export class Task {
return !!this.#subtasks.length;
}

public add(text: string, { status, autoClear }: ITaskOptions = {}): Task {
add(text: string, { status, autoClear }: ITaskOptions = {}): Task {
const isCompleted = !this.isPending;
const task = new Task(text, {
status: isCompleted ? TaskStatus.Failed : status,
Expand All @@ -98,15 +98,8 @@ export class Task {
return task;
}

/** Update task text */
public update(text: string): Task {
if (this.isPending) this.#text = text;

return this;
}

/** Adds a new progress bar. Returns a progress bar object */
public bar(template?: string, options?: IProgressBarOptions): ProgressBar {
bar(template?: string, options?: IProgressBarOptions): ProgressBar {
const bar = new ProgressBar(template, this.isPending ? options : { total: Progress.End });

this.#bars.push(bar);
Expand All @@ -117,12 +110,12 @@ export class Task {
}

/** Removes all subtasks and progress bars */
public clear(): void {
clear(): void {
this.#subtasks = [];
this.#bars = [];
}

public complete(text?: string, clear = this.#autoClear): Task {
complete(text?: string, clear = this.#autoClear): Task {
if (this.havePendingSubtasks) this.fail('Subtasks is not complete.');

this.setStatus(TaskStatus.Completed, text, clear);
Expand All @@ -135,41 +128,29 @@ export class Task {
return this;
}

public skip(text?: string, clear = this.#autoClear): Task {
this.setStatus(TaskStatus.Skipped, text, clear);
error(error?: string | Error, fail?: boolean): Task {
if (typeof error === 'string') this.#errors.push(error);
if (error instanceof Error && error.stack) this.#errors.push(error.stack);
if (fail) this.fail(error);

return this;
}

public fail(error?: string | Error, clear = this.#autoClear): never {
fail(error?: string | Error, clear = this.#autoClear): never {
const text = error instanceof Error ? error.name : error;

this.setStatus(TaskStatus.Failed, text, clear);

return TaskTree.tree().exit(ExitCode.Error, error) as never;
}

public error(error?: string | Error, fail?: boolean): Task {
if (typeof error === 'string') this.#errors.push(error);
if (error instanceof Error && error.stack) this.#errors.push(error.stack);
if (fail) this.fail(error);

return this;
}

public log(text: string): Task {
log(text: string): Task {
if (this.isPending) this.#logs.add(text);

return this;
}

public warn(text: string): Task {
if (this.isPending) this.#warnings.add(text);

return this;
}

public render(theme: Theme, level = 0): string[] {
render(theme: Theme, level = 0): string[] {
const type = level ? IndicationType.Dim : IndicationType.Default;
const rows = [
theme.title(this, level),
Expand All @@ -186,6 +167,25 @@ export class Task {
return rows.map((row): string => theme.paint(row, type));
}

skip(text?: string, clear = this.#autoClear): Task {
this.setStatus(TaskStatus.Skipped, text, clear);

return this;
}

/** Update task text */
update(text: string): Task {
if (this.isPending) this.#text = text;

return this;
}

warn(text: string): Task {
if (this.isPending) this.#warnings.add(text);

return this;
}

private setStatus(status: TaskStatus, text?: string, clear?: boolean): void {
if (this.isPending) {
if (text) this.#text = text;
Expand Down
Loading

0 comments on commit 164f375

Please sign in to comment.