Skip to content

Commit 53342a4

Browse files
authored
Merge pull request #4 from littletof/feat/theme
feat: accept theme override
2 parents 22e198b + 55970bf commit 53342a4

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

example.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ const res = await runPrompt();
3333

3434
await snap([
3535
{content: `\x1b[42m \x1b[1m\x1b[37mSnapper\x1b[39m\x1b[22m 📷 \x1b[49m`, imageSavePath: 'snapper.png', viewport: {width: 145, height: 35}},
36-
]);
36+
], /*{ puppeteerLaunchOptions: {headless: false}, theme: {background: "#ccc"}}*/);

server.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ export function buildServer(): Application {
1212
const body = await context.request.body({ type: 'json'});
1313
const formData = await body.value;
1414
const termtext = decodeURI(formData['text'])/* : context.request.url.searchParams.get('text') */;
15-
16-
context.response.body = new String(template).replace('\'##REPLACEME##\'', `\`${termtext.replace(/([`'"$])/g, '\\$1')}\``);
15+
const termTheme = {...defaultTheme, ...(formData['theme'] ? JSON.parse(decodeURI(formData['theme'])) : {}) };
16+
context.response.body = new String(template)
17+
.replace('\'##REPLACEME##\'', `\`${termtext.replace(/([`'"$])/g, '\\$1')}\``)
18+
.replace('\'##THEME##\'', `${JSON.stringify(termTheme)}`);
1719
})
1820

1921
const app = new Application();
@@ -38,4 +40,26 @@ export function startServer(opts?: {port?: number}) {
3840
listen: buildServer().listen({port: opts?.port || 7777, signal}),
3941
abort: () => controller.abort()
4042
};
41-
}
43+
}
44+
45+
export const defaultTheme = {
46+
"foreground": "#cccccc",
47+
"background": "#1e1e1e",
48+
"cursorColor": "#cccccc",
49+
"black": "#000000",
50+
"red": "#c62f37",
51+
"green": "#37be78",
52+
"yellow": "#e2e822",
53+
"blue": "#396ec7",
54+
"purple": "#b835bc",
55+
"cyan": "#3ba7cc",
56+
"white": "#e5e5e5",
57+
"brightBlack": "#666666",
58+
"brightRed": "#e94a51",
59+
"brightGreen": "#45d38a",
60+
"brightYellow": "#f2f84a",
61+
"brightBlue": "#4e8ae9",
62+
"brightPurple": "#d26ad6",
63+
"brightCyan": "#49b7da",
64+
"brightWhite": "#e5e5e5"
65+
};

snapper.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface SnapParams {
1010
// TODO Deno puppeteer types
1111
export type PuppeteerLaunchOptions = any;
1212

13-
export async function snap(snaps: SnapParams[], options?: {snapServerUrl?: string, puppeteerLaunchOptions?: PuppeteerLaunchOptions}) {
13+
export async function snap(snaps: SnapParams[], options?: {snapServerUrl?: string, theme?: any, puppeteerLaunchOptions?: PuppeteerLaunchOptions}) {
1414
const {listen, abort} = startServer();
1515

1616
const browser = await puppeteer.launch(options?.puppeteerLaunchOptions || {
@@ -23,14 +23,24 @@ export async function snap(snaps: SnapParams[], options?: {snapServerUrl?: strin
2323
const page = await browser.newPage();
2424

2525
for(let textCase of snaps) {
26-
page.setViewport({deviceScaleFactor: 3, width: 1080, height: 30, ...textCase.viewport});
26+
const viewPort = {deviceScaleFactor: 3, width: 1080, height: 30, ...textCase.viewport};
27+
// TODO handle headless looking different
28+
// if(typeof options?.puppeteerLaunchOptions?.headless === "boolean" && !options.puppeteerLaunchOptions.headless) {
29+
// viewPort.width += 15;
30+
// }
31+
page.setViewport(viewPort);
32+
33+
let postData: any = {text: encodeURI(textCase.content)};
34+
if(options?.theme) {
35+
postData.theme = JSON.stringify(options.theme);
36+
}
2737

2838
await page.setRequestInterception(true);
2939

3040
page.once("request", async interceptedRequest => {
3141
await interceptedRequest.continue({
3242
method: "POST",
33-
postData: `{"text": "${encodeURI(textCase.content)}"}`,
43+
postData: JSON.stringify(postData),
3444
headers: {
3545
...interceptedRequest.headers(),
3646
"Content-Type": "application/json"

template.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export const template = `
1818
var fitAddon = new this.fitAddon();
1919
term.loadAddon(fitAddon);
2020
21+
term.setOption('theme', '##THEME##');
22+
2123
term.open(document.getElementById('terminal'));
2224
fitAddon.fit();
2325
const text = '##REPLACEME##';

0 commit comments

Comments
 (0)