Skip to content

Commit

Permalink
feat(cli): beautify global log
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Jan 25, 2024
1 parent 682790f commit f36e72a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
7 changes: 0 additions & 7 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getConfig } from "./src/config.ts";
import {
printListRegistrys,
printListRegistrysWithNetworkDelay,
printRegistry,
} from "./src/registrys.ts";
Expand All @@ -17,12 +16,6 @@ async function runMain() {
Deno.exit(0);
}

if (args[0] === "ls") {
const { configRegistry } = await getConfig();
printListRegistrys(configRegistry);
Deno.exit(0);
}

if (args[0] === "test") {
const { configRegistry } = await getConfig();
await printListRegistrysWithNetworkDelay(configRegistry);
Expand Down
12 changes: 10 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import { version } from "./version.ts";
export async function action() {
const optionalRegistry = new EnumType(registryKeys);

const ls = new Command()
.description(`列出源`).option("-g, --global", `全局`).action(
async ({ global }) => {
const { configRegistry } = await getConfig(!global);
printListRegistrys(configRegistry, global);
},
);

const use = new Command()
.description(`使用源`)
.usage(`[${registryKeys.join("|")}]`)
Expand Down Expand Up @@ -48,7 +56,7 @@ export async function action() {
if (newConfigText) {
await Deno.writeTextFile(configPath, newConfigText);
}
printListRegistrys(newRegistry);
printListRegistrys(newRegistry, global);
},
);

Expand All @@ -57,7 +65,7 @@ export async function action() {
.name("dnrm")
.version(version)
.description("deno 实现的 nrm,每次切换源都在 100ms 内,速度超级快")
.command("ls", "列出源")
.command("ls", ls)
.command("test", "测试源")
.command("use", use)
.parse(Deno.args);
Expand Down
17 changes: 13 additions & 4 deletions src/registrys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { line, registryKeys, registrys, SECOND } from "./constant.ts";

export function listRegistrys(
configRegistry: string,
format?: (v: string) => string,
format?: (v: string, isSelected?: boolean) => string,
) {
const hasFormat = typeof format === "function";

Expand All @@ -23,10 +23,11 @@ export function listRegistrys(

function selector(k: string) {
const v = registrys[k];
const text = configRegistry === k
const isSelected = configRegistry === k;
const text = isSelected
? `${brightGreen(`${k}${v}`)}`
: `${k}${gray(` → ${v}`)}`;
return hasFormat ? format(text) : text;
return hasFormat ? format(text, isSelected) : text;
}
}

Expand All @@ -53,7 +54,15 @@ export async function getRegistrysNetworkDelay(ms = 2 * SECOND) {
}
}

export function printListRegistrys(registry: string) {
export function printListRegistrys(registry: string, global = false) {
if (global) {
console.log(
listRegistrys(registry, (text, isSelected) => {
return isSelected ? `${text} ${gray("(global)")}` : text;
}),
);
return;
}
console.log(listRegistrys(registry));
}

Expand Down

0 comments on commit f36e72a

Please sign in to comment.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy