import { colors } from "@cliffy/ansi/colors"; import { Command } from "@cliffy/command"; import { print } from "../utils.ts"; import { cloneNotebook } from "../git.ts"; import { Hsh } from "../hsh.ts"; function listNotebooks(hsh: Hsh) { const current = hsh.getNotebook(); for (const notebook of hsh.allNotebooks()) { print( current.root === notebook.root ? colors.underline.cyan(notebook.name()) : notebook.name(), ); } } function register(cmd: Command, hsh: Hsh) { cmd.command("use", "use notebook") .arguments("") .action((_opts, notebook) => { hsh.setCurrentNotebook(notebook); }); cmd.command("notebook", getNotebookCommands(hsh)).alias("nb"); } function getNotebookCommands(hsh: Hsh) { return new Command() .description("perform various actions on notebooks") .action(() => listNotebooks(hsh)) // clone .command("clone", "clone a notebook") .arguments(" [name:string]") .option("--use", "switch to the notebook on clone", { default: false }) .action(async ({ use }, url, name) => { name = name ?? url.split("/").at(-1)!; await cloneNotebook(url, hsh.getNotebook(name)); if (use || hsh.allNotebooks().length === 1) hsh.setCurrentNotebook(name); }) // list .command("list", "list notebooks") .alias("ls") .action(() => listNotebooks(hsh)) // new .command("new", "create a new notebook") .alias("init") .arguments("") .option("--use", "switch to notebook on creation", { default: false }) .action(async ({ use }, name) => { await hsh.initNotebook(name); if (use || hsh.allNotebooks().length === 1) hsh.setCurrentNotebook(name); }) // path .command("path", "print out a notebook's path") .arguments("[name:string]") .action((_options, name) => { const notebook = hsh.getNotebook(name); print(notebook.root); }) // remove .command("remove", "remove a notebook") .alias("rm") .arguments("") .action((_options, name) => { hsh.removeNotebook(name); }) // use .command("use", "switch default notebook") .arguments("") .action((_options, notebook) => { hsh.setCurrentNotebook(notebook); }); } export default { register };