alacritty-theme-switch
Version:
CLI utility for switching Alacritty color themes
63 lines (62 loc) • 2.27 kB
JavaScript
/**
* Download themes command implementation.
*
* This module handles downloading Alacritty theme files from GitHub repositories
* to the local themes directory.
*/
import { ProgressBar, } from "../../deps/jsr.io/@std/cli/1.0.25/unstable_progress_bar.js";
import { createGitHubClient } from "../theme-manager/github/client.js";
/**
* Execute the download-themes command.
*
* Downloads all theme files from a GitHub repository to the local themes directory.
* Uses the GitHubClient's downloadAllThemes method to download all TOML files sequentially.
* Displays a progress bar to provide visual feedback during the download process.
*
* @param options - Command options
* @returns A ResultAsync containing the downloaded themes or an error
*
* @example
* ```typescript
* const result = await downloadThemesCommand({
* repositoryUrl: "https://github.com/alacritty/alacritty-theme",
* outputPath: "~/.config/alacritty/themes",
* ref: "master"
* }).toResult();
*
* if (result.isOk()) {
* console.log(`Downloaded ${result.data.length} themes`);
* } else {
* console.error("Download failed:", result.error);
* }
* ```
*/
export function downloadThemesCommand(options) {
const ghClientResult = createGitHubClient(options.repositoryUrl, options.ref);
return ghClientResult.asyncAndThen((ghClient) => {
return ghClient.listThemes()
.andThen((themes) => {
const progressBar = new ProgressBar({
max: themes.length,
formatter: progressBarFormatter,
});
return ghClient.downloadAllThemes(themes, options.outputPath, (current, _total) => progressBar.value = current)
.andTee(() => progressBar.stop()) // clean up
.orTee(() => progressBar.stop());
});
});
}
/**
* Custom progress bar formatter that displays file count instead of data size.
*
* @param formatter - The formatter object provided by ProgressBar
* @returns Formatted progress bar string showing files downloaded
*
* @example
* ```
* [00:05] [####------] [4/10 files]
* ```
*/
function progressBarFormatter(formatter) {
return `[${formatter.styledTime}] [${formatter.progressBar}] [${formatter.value}/${formatter.max} files]`;
}