Skip to content

Commit

Permalink
feat: pretty label and github repo as description
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanouil committed Nov 3, 2024
1 parent 53ec6a9 commit 36cd90a
Showing 1 changed file with 52 additions and 27 deletions.
79 changes: 52 additions & 27 deletions vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import * as vscode from "vscode";
import * as https from "https";
import { IncomingMessage } from "http";

const RECENTLY_INSTALLED_KEY = "recentlyInstalledExtensions";

export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand(
"quartoExtensionInstaller.installExtension",
async () => {
const csvUrl =
"https://raw.githubusercontent.com/mcanouil/quarto-extensions/main/extensions/quarto-extensions.csv";
let extensionsList: string[] = [];
let recentlyInstalled: string[] = context.globalState.get(RECENTLY_INSTALLED_KEY, []);

try {
const data = await fetchCSVFromURL(csvUrl);
Expand All @@ -20,17 +23,31 @@ export function activate(context: vscode.ExtensionContext) {
return;
}

const selectedExtension = await vscode.window.showQuickPick(
extensionsList,
{
placeHolder: "Select a Quarto extension to install",
}
);
const groupedExtensions = [
{ label: "Recently Installed", kind: vscode.QuickPickItemKind.Separator },
...recentlyInstalled.map((ext) => ({
label: formatExtensionLabel(ext),
description: getGitHubLink(ext),
})),
{ label: "All Extensions", kind: vscode.QuickPickItemKind.Separator },
...extensionsList.map((ext) => ({
label: formatExtensionLabel(ext),
description: getGitHubLink(ext),
})),
];

const selectedExtension = await vscode.window.showQuickPick(groupedExtensions, {
placeHolder: "Select a Quarto extension to install",
});

if (selectedExtension) {
if (selectedExtension && selectedExtension.label !== "All Extensions" && selectedExtension.label !== "Recently Installed") {
const terminal = vscode.window.createTerminal("Quarto");
terminal.show();
terminal.sendText(`quarto add ${selectedExtension} --no-prompt`);
terminal.sendText(`quarto add ${selectedExtension.label} --no-prompt`);

// Update recently installed extensions
recentlyInstalled = [selectedExtension.label, ...recentlyInstalled.filter(ext => ext !== selectedExtension.label)].slice(0, 5);
context.globalState.update(RECENTLY_INSTALLED_KEY, recentlyInstalled);
}
}
);
Expand All @@ -40,27 +57,35 @@ export function activate(context: vscode.ExtensionContext) {

async function fetchCSVFromURL(url: string): Promise<string> {
return new Promise((resolve, reject) => {
https
.get(url, (res: IncomingMessage) => {
const data: Uint8Array[] = [];

res.on("data", (chunk: Uint8Array) => {
data.push(chunk);
});

res.on("end", () => {
const buffer = Buffer.concat(data);
resolve(buffer.toString("utf8"));
});

res.on("error", (err: Error) => {
reject(err);
});
})
.on("error", (err: Error) => {
reject(err);
https.get(url, (res: IncomingMessage) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
resolve(data);
});
}).on("error", (err) => {
reject(err);
});
});
}

function getGitHubLink(extension: string): string {
const [owner, repo] = extension.split("/").slice(0, 2);
return `https://github.com/${owner}/${repo}`;
}

function formatExtensionLabel(ext: string): string {
const parts = ext.split("/");
const repo = parts[1];
let formattedRepo = repo.replace(/[-_]/g, " ");
formattedRepo = formattedRepo.replace(/quarto/gi, "").trim();
formattedRepo = formattedRepo
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
return formattedRepo;
}

export function deactivate() {}

0 comments on commit 36cd90a

Please sign in to comment.