Skip to content

Commit

Permalink
Add support for environmental variables (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
Katsute authored Aug 31, 2023
1 parent 658f566 commit 61633db
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Type `Background: Configuration` in the command pallette or press the **Backgrou
<img alt="configuration menu" src="https://raw.githubusercontent.com/KatsuteDev/Background/main/assets/configuration.gif">
</div>

#### ✱ Glob and URL Support
#### ✱ Glob, URL, and Environment Variable Support

Add background images by file, folder, [glob](https://github.com/isaacs/node-glob#glob-primer), or URL.

Expand Down Expand Up @@ -115,10 +115,18 @@ The order settings are saved in is:
| Advanced | Type | Description |
|---|:-:|---|
|`background.useWindowOptionsForAllBackgrounds`|`boolean`|If enabled, all background images will use the options set for the windows background. This does not include the backgrounds, you still need to add background images separately.|
|`background.renderContentAboveBackground`|`boolean`|If enabled, content like images, pdfs, and markdown previews will render above the background.|
|`background.renderContentAboveBackground`|`boolean`|If enabled, content like images, PDFs, and markdown previews will render above the background.|
|`background.smoothImageRendering`|`boolean`|If enabled, use smooth image rendering rather than pixelated rendering when resizing images.|
|`background.CSS`|`string`|Apply raw CSS to VSCode.|

## 💻 Environment Variables

| Variable | Description |
|---|---|
|`${vscode:workspace}`|Current VSCode project folder|
|`${user:home}`|Current user's home directory|
|`${...}`|System environment variable|

## ⚠️ Known Issues

#### ⚠️ (Mac) read-only file system
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
"default": false
},
"background.renderContentAboveBackground": {
"markdownDescription": "If enabled, content like images, pdfs, and markdown previews will render above the background.",
"markdownDescription": "If enabled, content like images, PDFs, and markdown previews will render above the background.",
"order": 13,
"type": "boolean",
"default": false
Expand Down
35 changes: 35 additions & 0 deletions src/lib/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2023 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 * as os from "os";
import * as vscode from "vscode";

const home: string = os.homedir();

export const resolve: (str: string) => string = (str: string) =>
str.replace(/\${(.*)}/g, (_, envvar) => {
if(envvar == "vscode:workspace" && vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 && vscode.workspace.workspaceFolders[0].uri){
return vscode.workspace.workspaceFolders[0].uri.fsPath.toString();
}else if(envvar == "user:home"){
return home;
}else if(envvar in process.env){
return process.env[envvar] || '';
}else{
return '';
}
});
24 changes: 15 additions & 9 deletions src/lib/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { GlobOptions, globSync } from "glob";

import * as path from "path";

import * as env from "../lib/env";

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

Expand All @@ -43,21 +45,25 @@ export const count: (glob: string | string[]) => number = (glob: string | string
for(const g of (Array.isArray(glob) ? glob.filter(unique) : [glob]))
if(g.startsWith("https://"))
i++;
else
globs.push(g);
else // need to normalize '/' ↓ so glob works properly
globs.push(env.resolve(g).replace(/\\/g, '/'));

return i + (globSync(globs, options) as string[]).filter(filter).filter(unique).length;
}

export const resolve: (glob: string | string[]) => string[] = (glob: string | string[]) => {
let p: string[] = [];
let urls: string[] = [];
let globs: string[] = [];

(Array.isArray(glob) ? glob.filter(unique) : [glob]).forEach(g => (g.startsWith("https://") ? p : globs).push(g));
for(const g of (Array.isArray(glob) ? glob.filter(unique) : [glob]))
if(g.startsWith("https://"))
urls.push(g);
else // need to normalize '/' ↓ so glob works properly
globs.push(env.resolve(g).replace(/\\/g, '/'));

return p.concat((globSync(globs, options) as string[])
.filter(filter)
.map(path => `vscode-file://vscode-app/${path.replace(/\\/gm, '/').replace(/^\/+/gm, "")}`))
.filter(unique)
.map(path => '"' + path + '"');
return urls.concat((globSync(globs, options) as string[])
.filter(filter) // need to normalize '/' again ↓ because glob uses the wrong slash
.map(path => `vscode-file://vscode-app/${path.replace(/\\/g, '/').replace(/^\/+/g, "")}`))
.filter(unique)
.map(path => '"' + path + '"');
}
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const vscode = require("vscode");

const file = path.join(__dirname, "__testno__");

const images = path.join(__dirname, "*.png").replace(/\\/gm, '/');
const images = path.join(__dirname, "*.png").replace(/\\/g, '/');

module.exports = {
run: () => new Promise(async () => {
Expand Down

0 comments on commit 61633db

Please sign in to comment.