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

feat: improve Ux options #113

Merged
merged 1 commit into from
Oct 20, 2022
Merged
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
7 changes: 4 additions & 3 deletions src/sfCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,10 @@ export abstract class SfCommand<T> extends Command {

public constructor(argv: string[], config: Config) {
super(argv, config);
const outputEnabled = !this.jsonEnabled();
this.progress = new Progress(outputEnabled && envVars.getBoolean(EnvironmentVariable.SF_USE_PROGRESS_BAR, true));
this.ux = new Ux(outputEnabled);
this.ux = new Ux({ jsonEnabled: this.jsonEnabled() });
this.progress = new Progress(
this.ux.outputEnabled && envVars.getBoolean(EnvironmentVariable.SF_USE_PROGRESS_BAR, true)
);
this.spinner = this.ux.spinner;
this.prompter = this.ux.prompter;
this.lifecycle = Lifecycle.getInstance();
Expand Down
12 changes: 7 additions & 5 deletions src/ux/ux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ import { Spinner } from './spinner';
* ```
*/
export class Ux extends UxBase {
public spinner: Spinner;
public prompter: Prompter;
public readonly spinner: Spinner;
public readonly prompter: Prompter;
public readonly outputEnabled: boolean;

public constructor(public readonly outputEnabled: boolean) {
super(outputEnabled);
this.spinner = new Spinner(outputEnabled);
public constructor({ jsonEnabled } = { jsonEnabled: false }) {
super(!jsonEnabled);
this.outputEnabled = !jsonEnabled;
this.spinner = new Spinner(this.outputEnabled);
this.prompter = new Prompter();
}

Expand Down
20 changes: 10 additions & 10 deletions test/unit/ux/ux.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Ux', () => {

describe('table', () => {
it('should log a table', () => {
const ux = new Ux(true);
const ux = new Ux();
ux.table([{ key: 'foo', value: 'bar' }], { key: {}, value: {} }, { printLine: CliUx.ux.info });
expect(infoStub.args).to.deep.equal([
['\u001b[1m Key Value \u001b[22m'],
Expand All @@ -35,65 +35,65 @@ describe('Ux', () => {
});

it('should not log anything if output is not enabled', () => {
const ux = new Ux(false);
const ux = new Ux({ jsonEnabled: true });
ux.table([{ key: 'foo', value: 'bar' }], { key: {}, value: {} });
expect(infoStub.callCount).to.equal(0);
});
});

describe('url', () => {
it('should log a url', () => {
const ux = new Ux(true);
const ux = new Ux();
ux.url('Salesforce', 'https://developer.salesforce.com/');
expect(infoStub.firstCall.args).to.deep.equal(['https://developer.salesforce.com/']);
});

it('should not log anything if output is not enabled', () => {
const ux = new Ux(false);
const ux = new Ux({ jsonEnabled: true });
ux.url('Salesforce', 'https://developer.salesforce.com/');
expect(infoStub.callCount).to.equal(0);
});
});

describe('styledJSON', () => {
it('should log stylized json', () => {
const ux = new Ux(true);
const ux = new Ux();
ux.styledJSON({ foo: 'bar' });
expect(infoStub.firstCall.args).to.deep.equal([
'\x1B[97m{\x1B[39m\n \x1B[94m"foo"\x1B[39m\x1B[93m:\x1B[39m \x1B[92m"bar"\x1B[39m\n\x1B[97m}\x1B[39m',
]);
});

it('should not log anything if output is not enabled', () => {
const ux = new Ux(false);
const ux = new Ux({ jsonEnabled: true });
ux.styledJSON({ foo: 'bar' });
expect(infoStub.callCount).to.equal(0);
});
});

describe('styledObject', () => {
it('should log stylized object', () => {
const ux = new Ux(true);
const ux = new Ux();
ux.styledObject({ foo: 'bar' });
expect(infoStub.firstCall.args).to.deep.equal(['\u001b[34mfoo\u001b[39m: bar']);
});

it('should not log anything if output is not enabled', () => {
const ux = new Ux(false);
const ux = new Ux({ jsonEnabled: true });
ux.styledObject({ foo: 'bar' });
expect(infoStub.callCount).to.equal(0);
});
});

describe('styledHeader', () => {
it('should log stylized header', () => {
const ux = new Ux(true);
const ux = new Ux();
ux.styledHeader('A Stylized Header');
expect(infoStub.firstCall.args).to.deep.equal(['\u001b[2m=== \u001b[22m\u001b[1mA Stylized Header\u001b[22m\n']);
});

it('should not log anything if output is not enabled', () => {
const ux = new Ux(false);
const ux = new Ux({ jsonEnabled: true });
ux.styledHeader('A Stylized Header');
expect(infoStub.callCount).to.equal(0);
});
Expand Down