A note-taking app inspired by nb
at main 75 lines 2.3 kB view raw
1import { colors } from "@cliffy/ansi/colors"; 2import { Command } from "@cliffy/command"; 3import { print } from "../utils.ts"; 4import { cloneNotebook } from "../git.ts"; 5import { Hsh } from "../hsh.ts"; 6 7function listNotebooks(hsh: Hsh) { 8 const current = hsh.getNotebook(); 9 for (const notebook of hsh.allNotebooks()) { 10 print( 11 current.root === notebook.root 12 ? colors.underline.cyan(notebook.name()) 13 : notebook.name(), 14 ); 15 } 16} 17 18function register(cmd: Command, hsh: Hsh) { 19 cmd.command("use", "use notebook") 20 .arguments("<notebook:string>") 21 .action((_opts, notebook) => { 22 hsh.setCurrentNotebook(notebook); 23 }); 24 cmd.command("notebook", getNotebookCommands(hsh)).alias("nb"); 25} 26 27function getNotebookCommands(hsh: Hsh) { 28 return new Command() 29 .description("perform various actions on notebooks") 30 .action(() => listNotebooks(hsh)) 31 // clone 32 .command("clone", "clone a notebook") 33 .arguments("<url:string> [name:string]") 34 .option("--use", "switch to the notebook on clone", { default: false }) 35 .action(async ({ use }, url, name) => { 36 name = name ?? url.split("/").at(-1)!; 37 await cloneNotebook(url, hsh.getNotebook(name)); 38 if (use || hsh.allNotebooks().length === 1) hsh.setCurrentNotebook(name); 39 }) 40 // list 41 .command("list", "list notebooks") 42 .alias("ls") 43 .action(() => listNotebooks(hsh)) 44 // new 45 .command("new", "create a new notebook") 46 .alias("init") 47 .arguments("<name:string>") 48 .option("--use", "switch to notebook on creation", { default: false }) 49 .action(async ({ use }, name) => { 50 await hsh.initNotebook(name); 51 if (use || hsh.allNotebooks().length === 1) hsh.setCurrentNotebook(name); 52 }) 53 // path 54 .command("path", "print out a notebook's path") 55 .arguments("[name:string]") 56 .action((_options, name) => { 57 const notebook = hsh.getNotebook(name); 58 print(notebook.root); 59 }) 60 // remove 61 .command("remove", "remove a notebook") 62 .alias("rm") 63 .arguments("<name:string>") 64 .action((_options, name) => { 65 hsh.removeNotebook(name); 66 }) 67 // use 68 .command("use", "switch default notebook") 69 .arguments("<notebook:string>") 70 .action((_options, notebook) => { 71 hsh.setCurrentNotebook(notebook); 72 }); 73} 74 75export default { register };