Skip to content

Commit

Permalink
✨ Adds line highlighting option, shipping #50
Browse files Browse the repository at this point in the history
  • Loading branch information
mixn committed Apr 27, 2023
1 parent 4226c51 commit 2655817
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/config/cli/default-settings.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For my own (and others) sanity,
// this is commented JS, instead of uncommented JSON
// Keys here are based on the corresponding Carbon query params,
// so we can avoid further string mapping, e.g., `dropShadow` ➝ `ds`
const defaultSettings: CarbonCLIPresetInterface = {
// Theme
t: 'seti',
Expand All @@ -22,6 +22,8 @@ const defaultSettings: CarbonCLIPresetInterface = {
dsyoff: '20px',
// Drop shadow blur
dsblur: '68px',
// Selected lines aka. highlighted lines
sl: '*',
// Auto adjust width
wa: true,
// Line height
Expand Down
13 changes: 13 additions & 0 deletions src/config/cli/prompt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ export default [
default: '68px',
when: (answers: CarbonCLIPromptAnswersInterface) => answers.ds,
},
{
type: 'confirm',
name: 'highlight',
message: 'Highlight certain lines?',
default: false,
},
{
type: 'input',
name: 'sl',
message: 'Comma-separated list of lines to highlight, e.g., 4,5,6,7',
default: '*',
when: (answers: CarbonCLIPromptAnswersInterface) => answers.highlight,
},
{
type: 'confirm',
name: 'si',
Expand Down
11 changes: 6 additions & 5 deletions src/modules/preset-handler.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
import presetMissingView from '../views/preset-missing.view.js';

export default class PresetHandler {
private readonly ignoredSettings = ['save', 'preset', 'l', 'highlight'];

constructor(private readonly configPath: string = CONFIG_PATH) {}

private async writeConfig(
Expand Down Expand Up @@ -53,11 +55,10 @@ export default class PresetHandler {
preset = CONFIG_LATEST_PRESET,
presetSettings = {}
): Promise<void> {
const whiteListedSettings = lodash.omit(presetSettings, [
'save',
'preset',
'l',
]);
const whiteListedSettings = lodash.omit(
presetSettings,
this.ignoredSettings
);
const currentConfig = await this.readConfig();
const upcomingConfig = {
...currentConfig,
Expand Down
2 changes: 2 additions & 0 deletions src/types/cli/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface CarbonCLIPresetInterface {
ds: boolean;
dsyoff: string;
dsblur: string;
sl: string;
wa: boolean;
lh: string;
pv: string;
Expand All @@ -59,6 +60,7 @@ declare interface CarbonCLIPromptAnswersInterface
extends CarbonCLIPresetInterface {
dsyoff?: string;
dsblur?: string;
highlight: string;
t: CarbonCLIThemeType;
wt: 'None' | 'Sharp' | 'Black & White';
fm: CarbonFontFamilyType;
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DUMMY_INPUT,
DUMMY_FILE,
DUMMY_TARGET,
DUMMY_CONFIG,
} from '../helpers/constants.helper.js';
import readFileAsync from '../../src/utils/read-file-async.util.js';

Expand All @@ -16,7 +17,6 @@ const DEFAULT_SCRIPT = `${SCRIPT} ${DUMMY_FILE}`;
const DUMMY_LOCATION = 'location';
const DUMMY_SAVED_FILE_NAME = `${DUMMY_TARGET}.png`;
const ABSENT_DUMMY_CONFIG = './non-existent.json';
const PRESENT_DUMMY_CONFIG = './test/test-dummies/_config.json';

afterEach(async () => {
await del([DUMMY_SAVED_FILE_NAME], {
Expand Down Expand Up @@ -48,11 +48,11 @@ describe('Running `carbon-now` command', () => {
});

it('shouldn’t modify local --config, but instead treat it as read-only', async () => {
const CONFIG_BEFORE = await readFileAsync(PRESENT_DUMMY_CONFIG);
const CONFIG_BEFORE = await readFileAsync(DUMMY_CONFIG);
await execa.command(
`${DEFAULT_SCRIPT} --config=${PRESENT_DUMMY_CONFIG} -t=${DUMMY_TARGET}`
`${DEFAULT_SCRIPT} --config=${DUMMY_CONFIG} -t=${DUMMY_TARGET}`
);
const CONFIG_AFTER = await readFileAsync(PRESENT_DUMMY_CONFIG);
const CONFIG_AFTER = await readFileAsync(DUMMY_CONFIG);
expect(CONFIG_BEFORE).toBe(CONFIG_AFTER);
});

Expand Down
1 change: 1 addition & 0 deletions test/helpers/constants.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HOMEDIR } from '../../src/helpers/cli/constants.helper.js';

const CONFIG_DUMMY = '.carbon-now-dummy.json';
export const DUMMY_FILE = './test/test-dummies/_unfold.js';
export const DUMMY_CONFIG = './test/test-dummies/_config.json';
export const DUMMY_INPUT = '<owen-wilson says="Wow" />';
export const DUMMY_TARGET = 'target';
export const DUMMY_LOCATION = '~/something';
Expand Down
6 changes: 3 additions & 3 deletions test/modules/preset-handler.module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CONFIG_LATEST_PRESET } from '../../src/helpers/cli/constants.helper.js'
import {
CONFIG_DUMMY_PATH,
CONFIG_MISSING_PRESET,
DUMMY_CONFIG,
} from '../helpers/constants.helper.js';
import presetMissingView from '../../src/views/preset-missing.view.js';

Expand Down Expand Up @@ -101,14 +102,13 @@ describe('PresetHandlerModule', () => {

it('should handle local configs correctly', async () => {
expect(
await new PresetHandlerModule(
'./test/test-dummies/_config.json'
).getPreset(DUMMY_PRESET_NAME_1)
await new PresetHandlerModule(DUMMY_CONFIG).getPreset(DUMMY_PRESET_NAME_1)
).toEqual({
bg: 'white',
ds: true,
dsblur: '5px',
dsyoff: '5px',
sl: '*',
es: '2x',
fm: 'Inconsolata',
fs: '16px',
Expand Down
1 change: 1 addition & 0 deletions test/test-dummies/_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ds": true,
"dsyoff": "5px",
"dsblur": "5px",
"sl": "*",
"wa": true,
"lh": "133%",
"pv": "20px",
Expand Down

0 comments on commit 2655817

Please sign in to comment.