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

Optimize glob #71

Merged
merged 2 commits into from
Nov 29, 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
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"theme": "dark"
},
"publisher": "Katsute",
"version": "2.3.0",
"version": "2.3.1",
"private": true,
"engines": {
"vscode": "^1.73.0"
Expand Down
35 changes: 10 additions & 25 deletions src/command/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import * as vscode from "vscode";

import { glob } from "glob";

import { get, UI } from "../vs/vsconfig";
import { CommandQuickPickItem, quickPickItem, separator, showQuickPick } from "../vs/quickpick";

Expand All @@ -31,9 +29,8 @@ import * as repeat from "./config/repeat";
import * as size from "./config/size";
import * as time from "./config/time";

import { capitalize, s, sn } from "../lib/str";
import { unique } from "../lib/unique";
import { extensions } from "../extension";
import * as str from "../lib/str";
import * as glob from "../lib/glob";

// interface

Expand All @@ -42,7 +39,7 @@ export const config: vscode.Disposable = vscode.commands.registerCommand("backgr
// background types
quickPickItem({
label: "$(window) Window",
description: `${s(get("windowBackgrounds"), "Glob")} (${sn(globCount(get("windowBackgrounds")), "Background")})`,
description: `${str.s(get("windowBackgrounds"), "Glob")} (${str.s(glob.count(get("windowBackgrounds")), "Background")})`,
detail: `${get("backgroundAlignment", "window")} Alignment` + ` • ` +
`${get("backgroundBlur", "window")} Blur` + ` • ` +
`${get("backgroundOpacity", "window")} Opacity` + ` • ` +
Expand All @@ -54,7 +51,7 @@ export const config: vscode.Disposable = vscode.commands.registerCommand("backgr
}),
quickPickItem({
label: "$(multiple-windows) Editor",
description: `${s(get("editorBackgrounds"), "Glob")} (${sn(globCount(get("editorBackgrounds")), "Background")})`,
description: `${str.s(get("editorBackgrounds"), "Glob")} (${str.s(glob.count(get("editorBackgrounds")), "Background")})`,
detail: `${get("backgroundAlignment", "editor")} Alignment` + ` • ` +
`${get("backgroundBlur", "editor")} Blur` + ` • ` +
`${get("backgroundOpacity", "editor")} Opacity` + ` • ` +
Expand All @@ -66,7 +63,7 @@ export const config: vscode.Disposable = vscode.commands.registerCommand("backgr
}),
quickPickItem({
label: "$(layout-sidebar-left) Sidebar",
description: `${s(get("sidebarBackgrounds"), "Glob")} (${sn(globCount(get("sidebarBackgrounds")), "Background")})`,
description: `${str.s(get("sidebarBackgrounds"), "Glob")} (${str.s(glob.count(get("sidebarBackgrounds")), "Background")})`,
detail: `${get("backgroundAlignment", "sidebar")} Alignment` + ` • ` +
`${get("backgroundBlur", "sidebar")} Blur` + ` • ` +
`${get("backgroundOpacity", "sidebar")} Opacity` + ` • ` +
Expand All @@ -78,7 +75,7 @@ export const config: vscode.Disposable = vscode.commands.registerCommand("backgr
}),
quickPickItem({
label: "$(layout-panel) Panel",
description: `${s(get("panelBackgrounds"), "Glob")} (${sn(globCount(get("panelBackgrounds")), "Background")})`,
description: `${str.s(get("panelBackgrounds"), "Glob")} (${str.s(glob.count(get("panelBackgrounds")), "Background")})`,
detail: `${get("backgroundAlignment", "panel")} Alignment` + ` • ` +
`${get("backgroundBlur", "panel")} Blur` + ` • ` +
`${get("backgroundOpacity", "panel")} Opacity` + ` • ` +
Expand Down Expand Up @@ -123,15 +120,15 @@ export const options: vscode.QuickPickOptions = {
matchOnDescription: true
}

export const title: (s: string, ui?: UI) => string = (s: string, ui?: UI) => ui ? `${capitalize(ui)} ${options.title} - ${s}` : `${options.title} - ${s}`;
export const title: (s: string, ui?: UI) => string = (s: string, ui?: UI) => ui ? `${str.capitalize(ui)} ${options.title} - ${s}` : `${options.title} - ${s}`;

// menu

export const menu: (item: CommandQuickPickItem) => void = (item: CommandQuickPickItem) => {
showQuickPick([
quickPickItem({
label: "$(file-media) File",
description: `${s(get(`${item.ui!}Backgrounds`), "Glob")} (${sn(globCount(get(`${item.ui!}Backgrounds`)), "Background")})`,
description: `${str.s(get(`${item.ui!}Backgrounds`), "Glob")} (${str.s(glob.count(get(`${item.ui!}Backgrounds`)), "Background")})`,
detail: "Select background image files",
ui: item.ui!,
handle: file.menu
Expand Down Expand Up @@ -182,18 +179,6 @@ export const menu: (item: CommandQuickPickItem) => void = (item: CommandQuickPic
],
{
...options,
title: `${capitalize(item.ui!)} ${options.title}`,
title: `${str.capitalize(item.ui!)} ${options.title}`,
});
};

// glob

export const globCount: (globs: string[]) => number = (globs: string[]) => {
let i = 0;
for(const g of globs.filter(unique))
if(g.startsWith("https://"))
i++;
else // use glob
i += glob.sync(g).filter(extensions).length;
return i;
}
};
7 changes: 4 additions & 3 deletions src/command/config/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import { showInputBox } from "../../vs/inputbox";
import { get, UI, update } from "../../vs/vsconfig";
import { CommandQuickPickItem, quickPickItem, separator, showQuickPick } from "../../vs/quickpick";

import * as str from "../../lib/str";
import * as glob from "../../lib/glob";
import { unique } from "../../lib/unique";

import { globCount, menu as cm, options, title as t } from "../config";
import { menu as cm, options, title as t } from "../config";
import { notify } from "../install";
import { sn } from "../../lib/str";

// config

Expand Down Expand Up @@ -90,7 +91,7 @@ export const menu: (item: CommandQuickPickItem) => void = (item: CommandQuickPic
label: file.replace(/(\${\w+})/g, "\\$1"),
value: file,
ui: item.ui,
description: `${sn(globCount([file]), "matching file")}`,
description: `${str.s(glob.count(file), "matching file")}`,
handle: (item: CommandQuickPickItem) => updateItem(item.ui!, item)
}));

Expand Down
24 changes: 7 additions & 17 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import { ConfigKey } from "./vs/package";

import * as vscode from "vscode";

import { css, cssValue, get } from "./vs/vsconfig";

import { glob } from "glob";

import * as fs from "fs";
import * as path from "path";
import * as crypto from "crypto";

import * as fse from "./lib/file";
import * as glob from "./lib/glob";
import { round } from "./lib/round";
import { unique } from "./lib/unique";

import * as reload from "./command/reload";
import * as install from "./command/install";
Expand Down Expand Up @@ -161,20 +157,12 @@ const getJS: () => string = () => {
// populate images

const images: {[key: string]: string[]} = { // include start and end quotes
window: [],
editor: [],
sidebar: [],
panel: []
window: glob.resolve(get(`windowBackgrounds`)),
editor: glob.resolve(get(`editorBackgrounds`)),
sidebar: glob.resolve(get(`sidebarBackgrounds`)),
panel: glob.resolve(get(`panelBackgrounds`))
};

for(const s of ["window", "editor", "sidebar", "panel"])
for(const g of (get(`${s}Backgrounds` as ConfigKey) as string[]).filter(unique))
if(g.startsWith("https://")) // use literal URL
images[s].push('"' + g + '"');
else // use glob
for(const f of glob.sync(g).filter(extensions))
fse.unlock(f) && images[s].push('"' + `data:image/${path.extname(f).substring(1)};base64,${fs.readFileSync(f, "base64")}` + '"');

const after: boolean = get(`renderContentAboveBackground`);

return `/* ${identifier}-start */` + '\n' + `(() => {` +
Expand Down Expand Up @@ -217,6 +205,8 @@ bk_global.appendChild(document.createTextNode(\`

transition: opacity 1s ease-in-out;

image-rendering: pixelated;

}
\`));
`
Expand Down
58 changes: 58 additions & 0 deletions src/lib/glob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2022 Katsute <https://github.com/Katsute>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import { glob, IOptions } from "glob";

import * as fs from "fs";
import * as path from "path";

import { unlock } from "./file";
import { extensions } from "../command/config/file";
import { unique } from "./unique";

const filter = (v: string) => { // images only
const ext: string = path.extname(v);
for(const m of extensions())
if(`.${m}` === ext)
return true;
return false;
}

const options: IOptions = {
absolute: true,
nodir: true,
nosort: true
}

export const count: (globs: string | string[]) => number = (globs: string | string[]) => {
let i = 0;
for(const g of (Array.isArray(globs) ? globs : [globs]).filter(unique))
i += +g.startsWith("https://") || glob.sync(g, options).filter(filter).length;
return i;
}

export const resolve: (globs: string | string[]) => string[] = (globs: string | string[]) => {
let p: string[] = [];
for(const g of (Array.isArray(globs) ? globs : [globs]).filter(unique))
if(g.startsWith("https://"))
p.push('"' + g + '"');
else
for(const f of glob.sync(g, options).filter(filter))
unlock(f) && p.push('"' + `data:image/${path.extname(f).substring(1)};base64,${fs.readFileSync(f, "base64")}` + '"');
return p.filter(unique);
}
4 changes: 1 addition & 3 deletions src/lib/str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

export const s: (arr: any[], s: string) => string = (arr: any[], s: string) => sn(arr.length, s);

export const sn: (num: number, s: string) => string = (num: number, s: string) => `${num} ${s}${num != 1 ? 's' : ''}`;
export const s: (obj: any[] | number, str: string) => string = (obj: any[] | number, str: string) => Array.isArray(obj) ? s(obj.length, str) : `${obj} ${str}${obj != 1 ? 's' : ''}`;

export const capitalize: (s: string) => string = (s: string) => `${(s[0] ?? "").toUpperCase() + (s ?? "").substring(1)}`;

Expand Down